Я использую это так:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location: CLLocation = locations.last!
print("Location: \(location)")
let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude,
longitude: location.coordinate.longitude,
zoom: self.zoomLevel)
if let mapView = self.mapView {
if mapView.isHidden {
mapView.isHidden = false
mapView.camera = camera
} else {
mapView.animate(to: camera)
}
}
self.calculateDistance(destiLat: Lat Coordinate, destiLongL: Long Coordinate)
}
//This function calculates the distance between origin and destination
private func calculateDistance(destiLat: CLLocationDegrees, destiLongL: CLLocationDegrees) {
DispatchQueue.main.async {
let origin = CLLocation(latitude: self.locationManager.location?.coordinate.latitude ?? 0, longitude: self.locationManager.location?.coordinate.longitude ?? 0)
let destination = CLLocation(latitude: destiLat, longitude: destiLongL)
let distanceInMeters = origin.distance(from: destination)
print(distanceInMeters)
if distanceInMeters.isLessThanOrEqualTo(60) {
//You are in range of 60 meters
//Change the distance and show your button here
}
}
}
Я не знаю, лучший ли это способ, но он работает для меня.