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

how to get location name from default annotations Mapkit in iOS

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

我想点击它(ex.Swiss酒店)并在Swift

中获取名称

第 1 步

在您的地图上添加手势

let tgr = UITapGestureRecognizer(target: self, action: #selector(self.tapGestureHandler))
tgr.delegate = self
mapView.addGestureRecognizer(tgr)

第 2 步

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

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

第 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)
        }

    })