我是Swift IOS开发的新手,我试图找出用户与每个标记之间的距离。我正在使用google maps api和distance函数,但是即时通讯无法正常工作。有谁知道为什么这不起作用以及我应该更改什么?
// Creates markers
func createMarkers() {
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: lat , longitude: long)
marker.title = bar_name
marker.map = mapView
var userLocation = locationManager.location
var meters = userLocation?.distance(from: CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude))
// let meters = func distance(from CLLocation: CLLocation) -> CLLocation(latitude: marker.position.latitude, longitude: marker.position.longitude)
// I alternatively tried ^^ this commented line of code ^^ as well
print(meters!)
}
尝试一下:
let coordinate0 = CLLocation(latitude: 5.0, longitude: 5.0)
let coordinate1 = CLLocation(latitude: 5.0, longitude: 3.0)
let distanceInMeters = coordinate0.distance(from: coordinate1)