简体   繁体   English

如何从iOS中的地图图钉获取位置

[英]How to get location from map pin in iOS

I am using the MKMapView object and I am able to create pins and place them on the map. 我正在使用MKMapView对象,并且能够创建图钉并将其放置在地图上。 What I am trying to do now is get the information back from the pin. 我现在想做的就是从图钉中获取信息。 For example in my 例如在我的

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation (id<MKAnnotation>)annotation

I create my pins and set them on the map. 我创建了我的图钉并将其设置在地图上。 I am also creating buttons to use when a user clicks on the pin. 我还创建了用户单击图钉时要使用的按钮。 All that works well and good. 所有的一切都很好。 Now my question is when that button is clicked how do I get information from that clicked pin? 现在我的问题是,当单击该按钮时,如何从该被单击的图钉获取信息? Below is a small sample code of how I currently have it setup. 以下是我目前如何设置的一小段示例代码。 Any advice would be greatly appreciated. 任何建议将不胜感激。

- (MKAnnotationView*) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
    MKPinAnnotationView *annView = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"location"];
    annView.pinColor = MKPinAnnotationColorRed;
    annView.animatesDrop = YES;
    annView.canShowCallout = YES;
    annView.calloutOffset = CGPointMake(-5, 5);

    UIButton* callout = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    [callout addTarget:self action:@selector(loadDetails:) forControlEvents:UIControlEventTouchUpInside];
    annView.rightCalloutAccessoryView = callout;
    return annView;
}

- (void)loadDetails:(id)sender
{
    //TODO: Get pin coordinates or address and match it back to an array of available address to show details in a new view
}

Any advice is greatly appreciated 任何意见是极大的赞赏

You should create a class that derives from MKPinAnnotationView that has a property of id<MkAnnotationView> . 您应该创建一个派生自MKPinAnnotationView的类,该类具有id<MkAnnotationView>的属性。 During the method above, create this new object instead of the MKPinAnnotationView and set the id<MKAnnotation> property to the annotation passed in. 在上述方法期间,创建此新对象而不是MKPinAnnotationView,并将id<MKAnnotation>属性设置为传入的注释。

Instead of setting the target of your callout, respond to the delegate method 而不是设置标注的目标,而是响应委托方法

mapView:annotationView:calloutAccessoryControlTapped:

The annotationView will have an annotation property with the coordinate. annotationView将具有带有坐标的annotation属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM