iOS: Swift——如何在触摸地图上添加精确定位,并获取该位置的详细地址?

时间:2023-02-05 20:21:52

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?

我想在iOS map的touch上添加注释,获取相应位置的详细地址(Placemark)。如何在Swift中实现这一点?

Thanks in advance.

提前谢谢。

3 个解决方案

#1


17  

To react to the touch on map you need to set up a tap recogniser for the mapView

要对地图上的触摸做出反应,您需要为mapView设置一个tap识别器

in viewDidLoad:

在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(gestureReconizer: UILongPressGestureRecognizer) {

    let location = gestureReconizer.locationInView(mapView)
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}

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.

现在,您只需实现MKMapView委托函数来绘制注释。一个简单的谷歌搜索应该可以得到剩下的内容。

#2


2  

Swift 4:

斯威夫特4:

 @IBOutlet weak var mapView: MKMapView!

 func handleLongPress (gestureRecognizer: UILongPressGestureRecognizer{
    if gestureRecognizer.state == UIGestureRecognizerState.began {

        let touchPoint: CGPoint = gestureRecognizer.location(in: mapView)
        let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView)

        addAnnotationOnLocation(pointedCoordinate: newCoordinate)
    }
}

func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D{

    let annotation = MKPointAnnotation()
    annotation.coordinate = pointedCoordinate
    annotation.title = "Loading..."
    annotation.subtitle = "Loading..."
    mapView.addAnnotation(annotation)
}

#3


0  

For swift 3.0

斯威夫特3.0

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)

func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) {

    let location = gestureReconizer.locationInView(mapView)
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}

#1


17  

To react to the touch on map you need to set up a tap recogniser for the mapView

要对地图上的触摸做出反应,您需要为mapView设置一个tap识别器

in viewDidLoad:

在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(gestureReconizer: UILongPressGestureRecognizer) {

    let location = gestureReconizer.locationInView(mapView)
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}

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.

现在,您只需实现MKMapView委托函数来绘制注释。一个简单的谷歌搜索应该可以得到剩下的内容。

#2


2  

Swift 4:

斯威夫特4:

 @IBOutlet weak var mapView: MKMapView!

 func handleLongPress (gestureRecognizer: UILongPressGestureRecognizer{
    if gestureRecognizer.state == UIGestureRecognizerState.began {

        let touchPoint: CGPoint = gestureRecognizer.location(in: mapView)
        let newCoordinate: CLLocationCoordinate2D = mapView.convert(touchPoint, toCoordinateFrom: mapView)

        addAnnotationOnLocation(pointedCoordinate: newCoordinate)
    }
}

func addAnnotationOnLocation(pointedCoordinate: CLLocationCoordinate2D{

    let annotation = MKPointAnnotation()
    annotation.coordinate = pointedCoordinate
    annotation.title = "Loading..."
    annotation.subtitle = "Loading..."
    mapView.addAnnotation(annotation)
}

#3


0  

For swift 3.0

斯威夫特3.0

let gestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
gestureRecognizer.delegate = self
mapView.addGestureRecognizer(gestureRecognizer)

func handleTap(_ gestureReconizer: UILongPressGestureRecognizer) {

    let location = gestureReconizer.locationInView(mapView)
    let coordinate = mapView.convertPoint(location,toCoordinateFromView: mapView)

    // Add annotation:
    let annotation = MKPointAnnotation()
    annotation.coordinate = coordinate
    mapView.addAnnotation(annotation)
}