我正在使用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()
}
答案 0 :(得分:-1)
您可以使用iOS原生API将经度和纬度转换为地名:
func reverseGeocodeLocation(_ location: CLLocation,
completionHandler: @escaping CLGeocodeCompletionHandler)
例如:
var geocoder = CLGeocoder()
geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error)->Void in
})
通过获取CLPlacemark类型的地标的第一个对象来获取地名。然后CLPlacemark的name
属性就是你所追求的。