打开具有不同注释的不同地图

时间:2013-10-07 20:07:38

标签: ios mapkit mkannotation

我正在使用mapview上的多个注释制作应用。我成功地在mapview上显示了注释。

我想使用该应用在一个国家/地区查找不同的商店。所以我有所有坐标,我希望在点击注释时,地图应用程序会启动,用户可以从当前位置获取路线。

我的问题是,当我使用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个以上的注释。但是当我选择注释时,会显示coordinate1提供的数据。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

到目前为止工作还不错。问题出在-mapView:annotationView:calloutAccessoryControlTapped:delegate方法中。点击地图视图上的注释时会调用此方法。您正尝试为此委托方法中的两个示例注释配置和打开地图应用程序,您不应该这样做。您应该使用您可以访问的注释视图打开地图应用。无论是什么原因,显示第一个注释的详细信息的原因

NSArray *mapPoints = @[mapItem];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];
每次点击注释时都会先调用

以下是我建议你做的一些事情。

您应该修改MyLocation类,使其具有一个包含地址的NSDictionary属性和一个用于保存URL的NSURL属性。

所以你的-plotAnnotations方法现在看起来像这样:

-(void)plotAnnotations
{
    CLLocationCoordinate2D coordinate1;
    coordinate1.latitude = 52.511917;
    coordinate1.longitude = 4.994776;
    MyLocation *annotation = [[MyLocation alloc] initWithCoordinate:coordinate1 title:@"Basic-Fit Purmerend"];
    // Set the address for this annotation
    annotation.address = @{(NSString*)kABPersonAddressStreetKey:@"Grotenhuysweg 100, 1447 Purmerend, Nederland"};
    annotation.url = [NSURL URLWithString:@"http://www.basic-fit.nl/sportschool/Purmerend"];

    CLLocationCoordinate2D coordinate2;
    coordinate2.latitude = 51.972618;
    coordinate2.longitude = 5.310799;
    MyLocation *annotation2 = [[MyLocation alloc] initWithCoordinate:coordinate2 title:@"Basic-Fit Aalsmeer"];
    // Set the address for this annotation
    annotation2.address = @{(NSString*)kABPersonAddressStreetKey:@"Molenvliet 18A, Aalsmeer, Nederland"};
    annotation.url = [NSURL URLWithString:@"http://www.basic-fit.nl/sportschool/aalsmeer"];

    [self.mapView addAnnotation:annotation];
    [self.mapView addAnnotation:annotation2];
}

现在你的-mapView:annotationView:calloutAccessoryControlTapped:delegate方法将如下所示:

-(void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
    // You can retrieve your annotation using the annotation property of MKAnnotationView
    MyLocation *annotation = (MyLocation *)view.annotation;

    // Then you configure everything like you were doing
    MKPlacemark *placeMark = [[MKPlacemark alloc] initWithCoordinate:annotation.coordinate addressDictionary:annotation.address];

    MKMapItem *mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
    [mapItem setName:annotation.title];
    [mapItem setUrl:annotation.url];

    [MKMapItem openMapsWithItems:@[mapItem] launchOptions:nil];
}

更好的办法是创建一个名为Place的类,该类具有存储所有数据片段的属性。然后,您将修改MyLocation类以使用Place类进行初始化。然后,每次有权访问注释时,您都可以访问Place类。