如何从 iOS 中的默认注释 Mapkit 中获取位置名称

Ard*_*rda 1 mapkit cllocationmanager ios mapkitannotation swift

如何从 iOS 中 Mapkit 的默认位置点获取位置名称。

我想点击它(例如瑞士酒店)并在 Swift 中获取名称

在此输入图像描述

Anb*_*hik 5

步骤1

在地图上添加手势

let tgr = UITapGestureRecognizer(target: self, action: #selector(self.tapGestureHandler))
tgr.delegate = self
mapView.addGestureRecognizer(tgr)
Run Code Online (Sandbox Code Playgroud)

第2步

获取触摸位置的坐标,例如

func tapGestureHandler(tgr: UITapGestureRecognizer)
{
let touchPoint = tgr.locationInView(yourmapview)
let touchMapCoordinate = yourmapview.convertPoint(touchPoint, toCoordinateFromView: yourmapview)
print("tapGestureHandler: touchMapCoordinate = \(touchMapCoordinate.latitude),\(touchMapCoordinate.longitude)")
}
Run Code Online (Sandbox Code Playgroud)

步骤3

最后将纬度和经度转换为地址

let geoCoder = CLGeocoder()
    let location = CLLocation(latitude: touchMapCoordinate.latitude, longitude: touchMapCoordinate.longitude)

    geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in

        // Place details
        var placeMark: CLPlacemark!
        placeMark = placemarks?[0]

        // Address dictionary
        print(placeMark.addressDictionary)

        // Location name
        if let locationName = placeMark.addressDictionary!["Name"] as? NSString {
            print(locationName)
        }

        // Street address
        if let street = placeMark.addressDictionary!["Thoroughfare"] as? NSString {
            print(street)
        }

        // City
        if let city = placeMark.addressDictionary!["City"] as? NSString {
            print(city)
        }

        // Zip code
        if let zip = placeMark.addressDictionary!["ZIP"] as? NSString {
            print(zip)
        }

        // Country
        if let country = placeMark.addressDictionary!["Country"] as? NSString {
            print(country)
        }

    })
Run Code Online (Sandbox Code Playgroud)