Google Maps iOS Sdk, Getting Directions Between 2 Locations

Google Maps iOS SDK, Getting Directions between 2 locations

It sounds like you are looking for UI Chrome like the Google Maps app has for showing directions. Google Maps SDK for iOS will paint you a map, but you are responsible for the additional navigation chrome.

You can use the Google Directions API to request directions, and then use the encoded path returned from the service to draw a GMSPolyline using GMSPath's pathFromEncodedPath: method.

Getting directions between two points in google maps iOS SDK

You don't do anything with the dataTask so it isn't actually being called. You need to call resume().

let url = URL(string: "http://maps.googleapis.com/maps/api/directions/json?origin=\(latitude),\(longitude)&destination=\(finallat),\(finallong)&key=**************")
let task = URLSession.shared.dataTask(with: url!) { (data:Data?, response:URLResponse?, error:Error?) in
if let data = data {
do {
// Convert the data to JSON
let jsonSerialized = try JSONSerialization.jsonObject(with: data, options: []) as? [String : Any]

if let json = jsonSerialized, let url = json["url"], let explanation = json["explanation"] {
print(url)
print(explanation)
}
} catch let error as NSError {
print(error.localizedDescription)
}
} else if let error = error {
print(error.localizedDescription)
}
}
task.resume()

How to find distance between two coordinates using google maps SDK for ios?

Google Maps SDKs currently do not have this kind of feature. You will need get the results programmatically via Distance Matrix Web Service request.

If you would like to have this feature available in the Google Maps SDKs, you may file this as a feature request in Public Issue Tracker:
https://developers.google.com/maps/support/#issue_tracker

Drawing Route Between Two Places on GMSMapView in iOS

`first get all points coordinates which are coming in route then add these points latitude and longitude in path in will draw path according to that`

GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:18.5203 longitude:73.8567 zoom:12];
_mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
_mapView.myLocationEnabled=YES;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(18.5203, 73.8567);
marker.icon=[UIImage imageNamed:@"aaa.png"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mapView;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(@(18.520).doubleValue,@(73.856).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(@(16.7).doubleValue,@(73.8567).doubleValue)];

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 2.f;
rectangle.map = _mapView;
self.view=_mapView;

Get Directions using Google Maps API in iOS

You can use Directly Google map to get the direction from your current location to desire location by using following lines of code

(If you have "Google Map" App in your phone if loop will execute otherwise else loop will execute and open google map on your web browser)

if (UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!)) {
UIApplication.shared.openURL(NSURL(string:
"comgooglemaps://?saddr=&daddr=\(Float((Yourcoordinate.latitude)!)),\(Float((Yourcoordinate.longitude)!))&directionsmode=driving")! as URL)

} else {

let url = "https://www.google.co.in/maps/dir/?saddr=&daddr=\(Float((Yourcoordinate.latitude)!)),\(Float((Yourcoordinate.longitude)!))"
UIApplication.shared.openURL(NSURL(string:url)! as URL)
}

Hope this will help...

Find distance of location to route in Google Maps SDK

There is a GMSGeometryIsLocationOnPath function in the GMSGeometryUtils module in the Google Maps SDK.

You should be able to use that to calculate what you need.

Pseudocode (not tested):

let currentLocation: CLLocationCoordinate2D = ...
let routePath: GMSPath = routePolyline.path
let geodesic = true
let tolerance: CLLocationDistance = 40

let within40Meters = GMSGeometryIsLocationOnPath(currentLocation, routePath, geodesic, tolerance)

Drawing Route Between Two Places on GMSMapView in iOS

`first get all points coordinates which are coming in route then add these points latitude and longitude in path in will draw path according to that`

GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:18.5203 longitude:73.8567 zoom:12];
_mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
_mapView.myLocationEnabled=YES;
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(18.5203, 73.8567);
marker.icon=[UIImage imageNamed:@"aaa.png"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mapView;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(@(18.520).doubleValue,@(73.856).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(@(16.7).doubleValue,@(73.8567).doubleValue)];

GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 2.f;
rectangle.map = _mapView;
self.view=_mapView;


Related Topics



Leave a reply



Submit