我使用swift 4 for osx。 我想了解以下情况:
这是我的代码:
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
let myLocation = myMap.userLocation.coordinate
myMap.setRegion(MKCoordinateRegionMakeWithDistance(myLocation, 500, 500), animated: true)
let address = "XXX"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { (placemarks, error) in
guard let placemarks = placemarks, let targetLocation = placemarks.first?.location
else {
return
}
let sourceLocation = CLLocation(latitude: myLocation.latitude, longitude: myLocation.longitude)
var distance = sourceLocation .distance(from: targetLocation)
let annotation = MKPointAnnotation()
annotation.coordinate = targetLocation.coordinate
self.myMap.addAnnotation(annotation)
// Distance in km between sourceLocation and targetLocation
print(distance.rounded()/1000, " km")
}
}
现在我的问题:)