【发布时间】:2011-03-01 09:26:46
【问题描述】:
我在我的应用程序中使用 iPhone 原生地图。我在数组中有一组 lat、long、title 和 subtitle,并将它们绘制成带有注释的映射。当我单击注释标题和副标题时显示。当我单击注释时,有什么方法可以获取注释的经纬度吗?
【问题讨论】:
标签: iphone objective-c xcode maps
我在我的应用程序中使用 iPhone 原生地图。我在数组中有一组 lat、long、title 和 subtitle,并将它们绘制成带有注释的映射。当我单击注释标题和副标题时显示。当我单击注释时,有什么方法可以获取注释的经纬度吗?
【问题讨论】:
标签: iphone objective-c xcode maps
是的。实现MKMapViewDelegate - 协议(reference)的方法- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view。在那里,您可以通过访问annotation - MkAnnotationView 的属性来访问纬度和经度,它符合MkAnnotation - 协议(reference),因此有一个名为coordinate 的属性。
更新: 以下代码示例在地图上选择注释后打印其纬度/经度:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
NSLog(@"Latitude: %f", view.annotation.coordinate.latitude);
NSLog(@"Longitude: %f", view.annotation.coordinate.longitude);
}
【讨论】:
可以实现以下方法
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
MapAnnotation *annotation = (MapAnnotation *)view.annotation; //MapAnnotation your class which conform to MKAnnotation
float lattude = annotation.coordinate.latitude;
float longtude = annotation.coordinate.longitude;
}
在符合 MKMapViewDelegate 的类中
【讨论】:
斯威夫特 2:0
添加 MKMapViewDelegate。
func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView){
let currentAnnotationLattitude = view.annotation?.coordinate.latitude
let currentAnnotationLongitude = view.view.annotation?.coordinate.longitude
}
注意:它适用于单个地图视图中的多个引脚注释。此委托将提供注释视图的当前纬度和经度。
【讨论】: