1

First of all there is only user location in MKMapView. After some actions I call method: [self mapView:self.mapView didAddAnnotationViews:self.pointersArray]; My didAddAnnotationViews method:

-(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
    if (views.count == 1) {
        MKAnnotationView *annotationView = [views objectAtIndex:0];
        id<MKAnnotation>mp = [annotationView annotation];
        MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([mp coordinate], 500, 500);
        [mapView setRegion:region animated:YES]; 
    }
    else {
        [mapView addAnnotations:views];
    } 
}

Application is not crashing until zoom is not used. But when zoom used more then 10 times (about) I get error in this [mapView addAnnotations:views]; or sometimes in return UIApplicationMain(argc, argv, nil, NSStringFromClass([BIDAppDelegate class]));. Error - EXC_BAD_ACCESS. There is my problem?

EDIT

Changed to [self.mapView setRegion:region animated:YES]; But now I have error in main thread MKNormalizedPointForLayer EXC_BAD_ACCESS. In generally zoom is working, applications crashes after using zoom for example 7 or more times.. My button's action:

- (void)showKantorsOnMap {
    if (self.kantorsData.count != self.pointersArray.count) {
        NSLog(@"need to wait more");
    }
    NSMutableArray *toRemove = [[NSMutableArray alloc] init];
    for (id annotation in self.mapView.annotations)
    if (annotation != self.mapView.userLocation)
        [toRemove addObject:annotation];
    [self.mapView removeAnnotations:toRemove];
    [self.mapView addAnnotations:self.pointersArray];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.mapView.userLocation.coordinate,6500, 6500);
    [self.mapView setRegion:region animated:YES]; 
}

SOLUTION Problem was in didAddAnnotationViews method [mapView addAnnotations:views]; called recursion.

RomanHouse
  • 2,472
  • 3
  • 19
  • 44

2 Answers2

2

instead of using above code you can try these codes..This worked for me...

create new class under NSObject and name as MapClass in mapclass.h

 #import <UIKit/UIKit.h>
 #import <MapKit/MapKit.h>

@interface MapClass : NSObject <MKAnnotation>{
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle; 
@end

in MapClass.m file

#import "MapClass.h"

@implementation MapClass
@synthesize coordinate,title,subtitle;
@end

insert this in .h file

#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController

{

MKMapView *mapview;

}

@property (nonatomic, retain) IBOutlet MKMapView *mapview;
@end

insert this in .m file

[mapview setMapType:MKMapTypeStandard];
    [mapview setZoomEnabled:YES];
    [mapview setScrollEnabled:YES];

    MKCoordinateRegion region = { {0.0, 0.0 }, {0.0, 0.0 } };
    region.center.latitude = xxx;//your longitude
    region.center.longitude = xxx;//your latitude
    region.span.longitudeDelta = 0.01f;
    region.span.latitudeDelta = 0.01f;
    [mapview setRegion:region animated:YES];

    MapClass *ann = [[MapClass alloc] init];
    ann.title = @"Title";
    ann.subtitle = @"Subtitle.";
    ann.coordinate = region.center;
    [mapview addAnnotation:ann];

//mapview is variable for MKmapview which is declared in .h file.

Vishnu
  • 2,254
  • 2
  • 19
  • 44
2

First, you should not be calling the didAddAnnotationViews delegate method yourself. The map view itself will call it after it has actually shown annotations that you added to the map (using addAnnotation or addAnnotations).

Second, in that method this line:

[mapView addAnnotations:views];

is wrong for at least two reasons:

  • The views parameter is an NSArray of MKAnnotationViews (not id<MKAnnotation> objects). The addAnnotations method expects an NSArray of id<MKAnnotation> objects.
  • Calling addAnnotations within the didAddAnnotationViews delegate method is probably not a good idea (may cause the delegate method to be called recursively resulting in stack overflow)

Finally, if you want to zoom the map so that it shows all the annotations, there are many answers already with possible solutions (eg. search for "mkmapview annotations region fit zoom" or something similar). Here are a few of the answers you can try:

Basically, loop through the mapView.annotations array to find the minimum and maximum lat/long values. Then create an MKCoordinateRegion from that (center is the midpoint and delta is the difference). Then call setRegion.

Community
  • 1
  • 1
  • Sorry, based on your nickname :). I edited my question. Maybe need some another my code? – RomanHouse Jul 09 '12 at 15:37
  • Where is setRegion being called from? Are you still calling the delegate method directly? Are you adding any annotations and if so where and how? –  Jul 09 '12 at 15:43
  • `setRegion` called when my view is loaded (in delegate method) and in my button's action. – RomanHouse Jul 09 '12 at 16:39
  • Check that the coordinates of any annotations you add are valid (in the range -90 to 90 for lat and -180 to 180 for long). That's what [this question](http://stackoverflow.com/questions/9766272/error-in-main-thread-mknormalizedpointforlayer) claims was the cause of the MKNormalizedPointForLayer error. Also, are you doing any map-related work in background threads? –  Jul 09 '12 at 17:25
  • Coordinates are fine. To get coordinates of my annotations I used google api. – RomanHouse Jul 09 '12 at 17:39
  • Sorry but I'm not able to make that error happen (I've never experienced that specific error either). Starting with a new project, see if you can reproduce the error by adding a little bit of the actual project at a time to the new project. –  Jul 09 '12 at 17:50
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13629/discussion-between-romanhouse-and-anna-karenina) – RomanHouse Jul 09 '12 at 17:53