iOS:Swift-如何在触摸屏上添加精确的地图并获取该位置的详细地址? [英] iOS : Swift - How to add pinpoint to map on touch and get detailed address of that location?
本文介绍了iOS:Swift-如何在触摸屏上添加精确的地图并获取该位置的详细地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在触摸iOS地图时添加注释,并获取各个位置的详细地址(地标).如何在Swift中实现这一目标?
I want to add annotation on touch of iOS map and fetch the detailed address (Placemark) of respective location. How I can achieve this in Swift?
提前谢谢.
推荐答案
要对地图的触摸做出反应,您需要为mapView设置一个点击识别器
To react to the touch on map you need to set up a tap recogniser for the mapView
在 viewDidLoad
中:
let gestureRecognizer = UITapGestureRecognizer(target: self, action:"handleTap:")
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)
处理水龙头并获得被点击的位置坐标:
Handle the tap and get the tapped location coordinates:
func handleTap(gestureRecognizer: UILongPressGestureRecognizer) {
let location = gestureRecognizer.location(in: mapView)
let coordinate = mapView.convert(location, toCoordinateFrom: mapView)
// Add annotation:
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
mapView.addAnnotation(annotation)
}
现在,您只需要实现MKMapView委托函数即可绘制注释.一个简单的Google搜索应该可以为您提供其余的服务.
Now you only have to implement the MKMapView delegate functions to draw the annotations. A simple google search should get you the rest of that.
这篇关于iOS:Swift-如何在触摸屏上添加精确的地图并获取该位置的详细地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文