0

我正在制作一个在地图视图上带有多个注释的应用程序。我成功地在地图视图上显示了注释。

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

我的问题是我在使用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提供的数据。

我该如何解决这个问题?

4

1 回答 1

2

到目前为止做得很好。问题出在 -mapView:annotationView:calloutAccessoryControlTapped: 委托方法中。当点击地图视图上的注释时调用此方法。您正在尝试在此委托方法中为您的两个示例注释配置和打开地图应用程序,您不应该这样做。您应该使用您有权访问的注释视图打开地图应用程序。无论如何都显示第一个注释的详细信息的原因是因为

NSArray *mapPoints = @[mapItem];
[MKMapItem openMapsWithItems:mapPoints launchOptions:nil];

每当您点击注释时,都会首先调用。

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

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

所以你的 -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: 委托方法将如下所示:

-(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 类。

于 2013-10-08T02:36:01.910 回答