2017-03-06 91 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 –