My question is I want to draw the time between two points and my both location are coming from different controllers
for my first Location :-
extension HomeViewController: PickupLocationDelegate {
func didSelectLocation(place: GooglePlaceModel?) {
guard let place = place else {return}
enter_Location_TF.text = place.name
customDirectionViewTwo.isHidden = false
direction_Button.isHidden = true
location_Button.setImage(UIImage(named: "directionPoint")?.withRenderingMode(.alwaysOriginal), for: .normal)
pickupMarkers.forEach { (marker) in
marker.map = nil
}
let position = CLLocationCoordinate2D(latitude: place.latitude , longitude: place.longitude)
print(position)
let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: place.latitude, longitude: place.longitude))
let marker = GMSMarker()
marker.position = position
marker.map = mapView
mapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 14)
pickupMarkers.append(marker)
}
}// My second location:-
extension HomeViewController: DropLocationDelegate {
func didSelectDrop(location: GooglePlaceModel?) {
guard let location = location else {return}
dropLocationLbl?.text = location.name
let position = CLLocationCoordinate2D(latitude: location.latitude , longitude: location.longitude)
print(position)
let marker = GMSMarker(position: position)
marker.icon = #imageLiteral(resourceName: "icon-drop-location")
marker.map = mapView
mapView.camera = GMSCameraPosition.camera(withTarget: position, zoom: 14)
pickupMarkers.append(marker)
let path = GMSMutablePath()
path.add(CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude))
pickupMarkers.forEach { (marker) in
path.add(marker.position)
}
let bounds = GMSCoordinateBounds(path: path)
mapView?.animate(with: GMSCameraUpdate.fit(bounds, withPadding: 50.0))
}
}
marker are showing correctly but the path is not showing. I used the GMSMutable path to draw the line between location but this is not working properly. Any help?
Preconditions
You need to get a Google directions api Key following this link How to get a Google Directions API key and you also need to add this line
in your
AppDelegate
didFinishLaunchingWithOptions
methodNow our issue
To find a path you need to use a method like this one, using
googleapis.directions
request, passing twoCLLocationCoordinate2D
then in the closure you will get an array ofCLLocationCoordinate2D
which are the waypoints of your pathEdit (Added missing function)
Then you can use it in your code like this