I am making an app where I need to mark a circle of 5 km radius around my default marked coordinate so that if the user swipes and goes beyond that threshold to select a location, I can display a bottomsheet. Does the google map SDK for iOS have any such function that can be utilised here to measure the distance between the current coordinate and the new one? The new coordinate being the one where the user ends up after swiping?
Currently I am using a UILongPressGestureRecognizer to recognize the swipe on the screen to get the new coordinate for measuring the distance.
private var originalCoordinate: CLLocationCoordinate2D?
private var newCoordinate: CLLocationCoordinate2D?
private var distance: Double?
@objc private func handleLongPress(_ gesture: UILongPressGestureRecognizer) {
switch gesture.state {
case .began:
guard let originalCoordinate = getCoordinateForSelectedSection(selectedSection) else {
return
}
self.originalCoordinate = originalCoordinate
newCoordinate = nil
distance = nil
case .changed:
let gesturePoint = gesture.location(in: _googleMap)
let currentCoordinate = _googleMap.projection.coordinate(for: gesturePoint)
newCoordinate = currentCoordinate
case .ended:
if let originalCoordinate = originalCoordinate, let newCoordinate = newCoordinate {
let originalLocation = CLLocation(latitude: originalCoordinate.latitude, longitude: originalCoordinate.longitude)
let newLocation = CLLocation(latitude: newCoordinate.latitude, longitude: newCoordinate.longitude)
distance = newLocation.distance(from: originalLocation)
print("Distance: (distance ?? 0)")
print("New coordinate: (newCoordinate.latitude), (newCoordinate.longitude)")
}
default:
break
}
}
private func getCoordinateForSelectedSection(_ section: SelectedAddressBottomSelectionEnum) -> CLLocationCoordinate2D? {
switch section {
case .Home:
return buttonArrays.first(where: { $0.type == Constants.addressButtonTypeHome })?.coordinate
case .Work:
return buttonArrays.first(where: { $0.type == Constants.addressButtonTypeWork })?.coordinate
case .Current:
return buttonArrays.first(where: { $0.type == Constants.addressButtonTypeLive })?.coordinate
case .None:
return nil
}
}
I have three different default locations so I have three different cases for that. Is there a solution which is simpler?