我正在制作一个在地图视图上带有多个注释的应用程序。我成功地在地图视图上显示了注释。
我想使用该应用程序在一个国家/地区查找不同的商店。所以我有所有的坐标,我希望当点击注释时,地图应用程序会启动,用户可以从他当前的位置获取路线。
我的问题是我在使用calloutAccessoryControlTapped
函数的时候,每一个注解都会显示我填写的第一个坐标的信息。
这是我的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
self.mapView.delegate = self;
[self plotAnnotations];
}
-(void)plotAnnotations
{
CLLocationCoordinate2D coordinate1;
coordinate1.latitude = 52.511917;
coordinate1.longitude = 4.994776;
MyLocation *annotation = [[MyLocation alloc] initWithCoordinate:coordinate1 title:@"Basic-Fit Purmerend"];
CLLocationCoordinate2D coordinate2;
coordinate2.latitude = 51.972618;
coordinate2.longitude = 5.310799;
MyLocation *annotation2 = [[MyLocation alloc] initWithCoordinate:coordinate2 title:@"Basic-Fit Aalsmeer"];
[self.mapView addAnnotation:annotation];
[self.mapView addAnnotation:annotation2];
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
}
-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
CLLocationCoordinate2D coordinate1;
coordinate1.latitude = 52.511917;
coordinate1.longitude = 4.994776;
NSDictionary *addressDict1 = @{(NSString*)kABPersonAddressStreetKey:@"Grotenhuysweg 100, 1447 Purmerend, Nederland"};
MKPlacemark *placeMark1 = [[MKPlacemark alloc]initWithCoordinate:coordinate1 addressDictionary:addressDict1];
MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark1];
[mapItem setName:@"Basic-Fit Purmerend"];
[mapItem setUrl:[NSURL URLWithString:@"http://www.basic-fit.nl/sportschool/Purmerend"]];
CLLocationCoordinate2D coordinate2;
coordinate2.latitude = 51.972618;
coordinate2.longitude = 5.310799;
NSDictionary *addressDict2 = @{(NSString*)kABPersonAddressStreetKey:@"Molenvliet 18A, Aalsmeer, Nederland"};
MKPlacemark *placeMark2 = [[MKPlacemark alloc]initWithCoordinate:coordinate2 addressDictionary:addressDict2];
MKMapItem *mapItem2 = [[MKMapItem alloc] initWithPlacemark:placeMark2];
[mapItem2 setName:@"Basic-Fit Aalsmeer"];
[mapItem2 setUrl:[NSURL URLWithString:@"http://www.basic-fit.nl/sportschool/aalsmeer"]];
NSArray *mapPoints = @[mapItem];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];
NSArray *mapPoints1 =@[mapItem2];
[MKMapItem openMapsWithItems:mapPoints1 launchOptions:nil];
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
static NSString *identifier = @"MyLocation";
MKPinAnnotationView *annotationView = (MKPinAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if (!annotationView)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
annotationView.enabled = YES;
annotationView.canShowCallout = YES;
annotationView.image = [UIImage imageNamed:@"arrest.png"];//here we use a nice image instead of the default pins
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
} else {
annotationView.annotation = annotation;
}
return annotationView;
}
我最终想在地图上显示超过 2 个注释。但是当我选择一个注释时,会显示坐标1提供的数据。
我该如何解决这个问题?