我如何使用MapKit放置一个别针? [英] How do I drop a pin with MapKit?
问题描述
我想允许我的应用的用户在地图中选择一个位置。本地地图具有放置引脚功能,您可以通过放置引脚来定位某些内容。如何在MapKit中执行此操作?
I would like to allow the user of my app to pick a location in the map. The native map has a "drop pin" feature where you can locate something by dropping a pin. How can I do this in MapKit?
推荐答案
您需要创建一个实现 MKAnnotation 协议,然后将该对象添加到 MKMapView :
You need to create an object that implements the MKAnnotation protocol and then add that object to the MKMapView:
@interface AnnotationDelegate : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
}
实例化您的委托对象并将其添加到地图中:
Instantiate your delegate object and add it to the map:
AnnotationDelegate * annotationDelegate = [[[AnnotationDelegate alloc] initWithCoordinate:coordinate andTitle:title andSubtitle:subt] autorelease];
[self._mapView addAnnotation:annotationDelegate];
地图将访问您的AnnotationDelegate的坐标属性以找出在地图上的哪个位置。
The map will access the coordinate property on your AnnotationDelegate to find out where to put the pin on the map.
如果您想自定义注解视图,您需要在Map视图中实现 MKMapViewDelegate viewForAnnotation 方法(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id< MKAnnotation>)注释$ b
If you want to customize your annotation view you will need to implement the MKMapViewDelegate viewForAnnotation method on your Map View Controller:
- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation
如果您想要实现引脚拖动功能,您可以阅读有关处理 Apple OS Reference Library 。
If you would like to implement the pin drag functionality you can read about handling annotation touch events in the Apple OS Reference Library.
您还可以查看这篇文章关于拖放mapkit,它引用 GitHub 。您可以通过检查 DDAnnotation 对象上的 _coordinates 成员来获取拖动注释的坐标。
You can also check out this article on drag drop with mapkit which refers to a working sample library on GitHub. You can get the coordinates of the dragged annotation by checking the _coordinates member on the DDAnnotation object.
这篇关于我如何使用MapKit放置一个别针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!