【问题标题】:Find location name swift maps查找位置名称快速地图
【发布时间】:2017-03-06 03:05:14
【问题描述】:

我正在使用 swift 创建地图。我如何找到保存在 Apple 地图上的当前位置的名称,比如假设我在沃尔玛,我如何让它打印出位置的名称,如果该位置只打印出地址。

我当前的代码是

class MapVC: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var map: MKMapView!

let manager = CLLocationManager()


func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations[0]
    let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)

    let mylocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
    let region:MKCoordinateRegion = MKCoordinateRegionMake(mylocation, span)
    map.setRegion(region, animated: true)

    print(location.coordinate)
    print(location.coordinate.latitude)
    print(location.speed)

    self.map.showsUserLocation = true
    self.map.showsPointsOfInterest = true


}

override func viewDidLoad() {
    super.viewDidLoad()

    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestAlwaysAuthorization()
    manager.startUpdatingLocation()

}

【问题讨论】:

标签: ios swift swift3 location maps


【解决方案1】:

您可以使用iOS原生API将经纬度转换为地名:

func reverseGeocodeLocation(_ location: CLLocation, 
          completionHandler: @escaping CLGeocodeCompletionHandler)

例如:

var geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in

})

通过获取第一个 CLPlacemark 类型的地标对象来获取地名。那么 CLPlacemark 的 name 属性就是您所追求的。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多