1

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

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

在此处输入图像描述

4

1 回答 1

4

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

第三步

最后将 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)
        }

    })
于 2016-08-16T14:46:43.850 回答