2017-03-06 58 views
0

我正在使用swift创建地图。我如何才能找到我在苹果地图上保存的当前位置的名称,例如让我说我在沃尔玛,如何让它打印出位置的名称,以及它是否打印出地址。查找位置名称swift地图

我当前的代码是

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

} 
+0

[将坐标转换为城市名称?](https://stackoverflow.com/questions/27735835/convert-coordinates-to-city-name) – Emm

回答

-1

您可以使用iOS原生API转换经度和纬度的地方名:

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

例如:

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

}) 

获取的地方通过获取CLPlacemark类型的地标的第一个对象来获取名称。然后,CLPlacemark的name属性就是你所追求的。

+0

这样输出'placemarks.name'即可得到名称 –

+0

是。请参阅此文档以获取更多信息。 https://developer.apple.com/reference/corelocation/clplacemark –