如何从iOS

时间:2016-08-16 14:32:53

标签: ios swift mapkit cllocationmanager mapkitannotation

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

我想点击它(ex.Swiss Hotel)并在Swift中获得名字

enter image description here

1 个答案:

答案 0 :(得分:3)

<强>步骤-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

最后将lat和long转换为地址

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

    })