问题
I have used apple maps in my application for implementing maps.I used the following code for that.
My problem is that,It is showing the pins only when it is loading for the first time.
I have written the code like this
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = @"Locate Engineer";
[worldView setMapType:MKMapTypeStandard];
[worldView setShowsUserLocation:YES];
worldView.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
}
-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self)
{
i=0;
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
return self;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@",newLocation);
}
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
pinView = nil;
if(annotation != mapView.userLocation)
{
NSString * defaultPinID = @"aa";
//removed autorelease from the code below by coder
pinView = (MKPinAnnotationView *) [worldView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if(pinView == nil)
{
pinView .tag =i;
pinView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:defaultPinID] ;
//pinView.image = [UIImage imageNamed:@"serviceengineer.png"];
NSLog(@"pnID is %d",i);
NSLog(@"value of i is %d",i);
if ([[pinView.annotation title] isEqualToString:@"Levitton"]) {
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.draggable = NO;
} else {
pinView.pinColor = MKPinAnnotationColorGreen;
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
[rightButton addTarget:self action:@selector(mapAction:) forControlEvents:UIControlEventTouchUpInside];
rightButton.tag =i;
pinView.rightCalloutAccessoryView = rightButton;
// pinView.animatesDrop = YES;
pinView.canShowCallout = YES;
pinView.draggable = NO;
}
}
else {
[worldView.userLocation setTitle:@"Title here"]; }
i++;
}
return pinView;
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
ServiceEngineersList *engList = [ServiceEngineers findAll];
MKCoordinateSpan spans;
spans.latitudeDelta = 0.5;
spans.longitudeDelta = 0.5;
ServiceEngineers *obj1= [engList objectAtIndex:0];
// location1.latitude = 40.728224000000;
//location1.longitude =-73.794852000000;
location1.latitude = [obj1.LATITUDE doubleValue];
location1.longitude =[obj1.LONGITUDE doubleValue];
ad1 = [[annotationTest alloc] init];
ad1.pinColor = MKPinAnnotationColorGreen;
MKCoordinateRegion regions =
MKCoordinateRegionMakeWithDistance(location1, 250,250);
regions.span = spans;
[worldView setRegion:regions animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad1];
ServiceEngineers *obj2= [engList objectAtIndex:1];
ad2 = [[annotationTest2 alloc] init];
ad2.pinColor = MKPinAnnotationColorGreen;
//location2.latitude = 40.710841000000;
//location2.longitude=-73.897769000000;
location2.latitude = [obj2.LATITUDE doubleValue];
location2.longitude =[obj2.LONGITUDE doubleValue];
MKCoordinateRegion region1 =
MKCoordinateRegionMakeWithDistance(location2, 250, 250);
region1.span = spans;
[worldView setRegion:region1 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad2];
ServiceEngineers *obj3= [engList objectAtIndex:2];
ad3 = [[annotationTest3 alloc] init];
ad3.pinColor = MKPinAnnotationColorGreen;
//location3.latitude = 40.726768000000;
//location3.longitude=-73.634295000000;
location3.latitude = [obj3.LATITUDE doubleValue];
location3.longitude =[obj3.LONGITUDE doubleValue];
MKCoordinateRegion region2 =
MKCoordinateRegionMakeWithDistance(location3, 250, 250);
region2.span = spans;
[worldView setRegion:region2 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad3];
ServiceEngineers *obj4= [engList objectAtIndex:3];
ad4 = [[annotationTest4 alloc] init];
ad4.pinColor = MKPinAnnotationColorGreen;
//location4.latitude = 40.702677000000;
//location4.longitude=-73.788969000000;
location4.latitude = [obj4.LATITUDE doubleValue];
location4.longitude =[obj4.LONGITUDE doubleValue];
MKCoordinateRegion region3 =
MKCoordinateRegionMakeWithDistance(location4, 250, 250);
region3.span = spans;
[worldView setRegion:region3 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad4];
ad5 = [[annotationTest5 alloc] init];
ad5.pinColor = MKPinAnnotationColorRed;
location5.latitude = 40.7258;
location5.longitude= -73.5147;
MKCoordinateRegion region4 =
MKCoordinateRegionMakeWithDistance(location5, 250, 250);
region4.span = spans;
[worldView setRegion:region4 animated:YES];
[worldView setZoomEnabled:YES];
[worldView addAnnotation:ad5];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Couldnot find location: %@",error);
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
return YES;
return NO;
}
Please help me to find out the issue.I put breakpoints.Second time it is coming only upto
worldView.delegate = self;
回答1:
Just place in the viewWillAppear
and remove in the viewDidLoad
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
locationManager = [[CLLocationManager alloc]init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[locationManager startUpdatingLocation];
}
来源:https://stackoverflow.com/questions/16297158/annotated-pins-are-not-getting-loaded-during-reloading-of-map