如何使用 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要创建一个实现MKAnnotation协议的对象,然后将该对象添加到MKMapView:
实例化您的委托对象并将其添加到地图:
地图将访问AnnotationDelegate 上的坐标属性,以找出将图钉放置在地图上的位置。
如果您想自定义注释视图,您需要在地图视图控制器上实现 MKMapViewDelegate viewForAnnotation 方法:
如果您想实现图钉拖动功能,您可以阅读关于处理注释触摸事件 Apple 操作系统参考库。
您还可以查看这篇文章 使用 mapkit 进行拖放,它引用 GitHub 上的工作示例库。您可以通过检查 DDAnnotation 对象上的 _coordinates 成员来获取拖动注释的坐标。
You need to create an object that implements the MKAnnotation protocol and then add that object to the MKMapView:
Instantiate your delegate object and add it to the map:
The map will access the coordinate property on your AnnotationDelegate to find out where to put the pin on the map.
If you want to customize your annotation view you will need to implement the MKMapViewDelegate viewForAnnotation method on your Map View Controller:
If you would like to implement the pin drag functionality you can read about handling annotation touch events in the Apple OS Reference Library.
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.
放置图钉的方法有多种,并且您没有在问题中指定使用哪种方法。第一种方法是以编程方式执行此操作,因为您可以使用 RedBlueThing 编写的内容,只是您并不真正需要自定义类(取决于您所针对的 iOS 版本)。对于 iOS 4.0 及更高版本,您可以使用此代码片段以编程方式放置图钉:
如果您希望能够通过长按实际的地图视图来放置图钉,可以这样做:
如果您想枚举所有注释,只需使用两个片段中的代码即可。这是记录所有注释位置的方式:
There are multiple ways to drop a pin, and you don't specify which way to do it in your question. The first way is to do it programmatically, for that you can use what RedBlueThing wrote, except that you don't really need a custom class (depending on what version of iOS you are targetting). For iOS 4.0 and later you can use this snippet to programmatically drop a pin:
If you want to be able to drop a pin by for example long pressing on the actual mapView, it can be done like this:
If you want to enumerate all the annotations, just use the code in both snippets. This is how you log positions for all annotations:
您可以通过jcesarmobile在被点击时回答与 iphone mapkit 协调,您可以将图钉放置在任何位置,如下所示
you can get touched location by ,jcesarmobile answer on get tapped coordinates with iphone mapkit and you can drop pin any where as bellow
您可能还需要设置 MapView Delegate。
然后调用它的委托,
viewForAnnotation
:You might also need to set MapView Delegate.
Then call its delegate,
viewForAnnotation
: