Plot directions on Google map based on geo coordinates - ios

I am building an iOS app that tracks a users location using Google Maps SDK. I am storing any changed lat/long coordinates in an array by checking if the coordinates exist and if they don't exist they get appended.
This is working great when I plot a polyline however I would like to take some of those coordinates and create directions (follow a street, road, path etc).
Is there a way to take say 10 or 20 coordinates and plot directions from those? And once the directions are created, return the lat/long coordinates of the new route?

Yes, you have to call the Google Directions API and pass your coordinates as waypoints. You can also tell the API the order of those waypoints to trace routes. I've done it like this:
http://maps.googleapis.com/maps/api/directions/json?&origin=28.584442,-81.305543&destination=28.596688, -81.302325&waypoints=28.589793,-81.311122|28.595897, -81.308891&sensor=false
Separate each pair of coordinates you want to use as waypoint with a pipe character |
You can learn more on how to use waypoints in routes here:
https://developers.google.com/maps/documentation/directions/#Waypoints

Might help someone else
GMSCameraPosition *cameraPosition=[GMSCameraPosition cameraWithLatitude:13.0733838 longitude:80.19203929 zoom:12];
_mapView =[GMSMapView mapWithFrame:CGRectZero camera:cameraPosition];
GMSMarker *marker=[[GMSMarker alloc]init];
marker.position=CLLocationCoordinate2DMake(13.0733838, 80.19203929);
marker.icon=[UIImage imageNamed:#"mappinblue"] ;
marker.groundAnchor=CGPointMake(0.5,0.5);
marker.map=_mapView;
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.0733838).doubleValue,#(80.19203929).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.074099).doubleValue,#(80.1919654).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.070933).doubleValue,#(80.1842051).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.0770034).doubleValue,#(80.21175909999999).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.0784528).doubleValue,#(80.2120139).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.078285).doubleValue,#(80.21424979999999).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.0774898).doubleValue,#(80.21416479999999).doubleValue)];
[path addCoordinate:CLLocationCoordinate2DMake(#(13.0774401).doubleValue,#(80.2146616).doubleValue)];
GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeWidth = 4.f;
rectangle.map = _mapView;

Related

Origin and destination point is not placed precisely on Google Maps SDK

I tried to draw a route with Google Maps on iOS that come from Google Direction API.
I hit this end point
NSString *urlString = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/directions/json?origin=%#&destination=%#&mode=driving&key=%#", origin, destination, gDirectionKey];
Then I got its routes that I draw with GMSPolyline
GMSPath *path =[GMSPath pathFromEncodedPath:resultData[#"routes"][0][#"overview_polyline"][#"points"]];
if (routeToCustomer == nil)
routeToCustomer = [[GMSPolyline alloc] init];
routeToCustomer.path = path;
routeToCustomer.strokeWidth = 7;
routeToCustomer.strokeColor = [UIColor colorWithHexString:ACTIVE_COLOR];
routeToCustomer.map = _mapView;
It looks like that the starting line doesn't start in its coordinate, but in the nearest way. See image below.
Is it possible to draw line from its coordinate into "starting direction"? If so, any help would be appreciated. Thank you.
You will need draw a polyline from starting point of google direction result and actual starting point in mapView.
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(actualLat,actualLon)];
[path addCoordinate:CLLocationCoordinate2DMake(startLat,startLon)];
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];

Google Map SDK: Draw correct polyline in google map as Device Moves in Background

I am using Google Map SDK for iOS. I am drawing polylines in Driving mode.
But when i stop,and Zoom google map then, my Current position cursor automatically moves and redraw zigzag polylines, due to that all previous polylines drawn get overlapped and polylines get completely changed.Same things happens when i go in background and drive.
Could i know why is it happening? And How can I draw smooth polylines in driving and walking mode same time in same path.
My Code-
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
pointString=[NSString stringWithFormat:#"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];
CLLocationDistance kilometers = [newLocation distanceFromLocation:oldLocation] / 1000;
NSLog(#"Distance Travelled in Kilometer :%f",kilometers);
[self.points addObject:pointString];
GMSMutablePath *path = [GMSMutablePath path];
for (int i=0; i<self.points.count; i++)
{
NSArray *latlongArray = [[self.points objectAtIndex:i]componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:#","]];
[path addLatitude:[[latlongArray objectAtIndex:0] doubleValue] longitude:[[latlongArray objectAtIndex:1] doubleValue]];
}
if (self.points.count>2)
{
GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.strokeColor = [UIColor blueColor];
polyline.strokeWidth = 5.f;
polyline.map = mapView_;
self.mapContainerView = mapView_;
}
}
If , I remain in Same position, then Googme map Cursor position automaticalaly moves and draw polylines like this.
add a NSLocationAlwaysUsageDescription and a UIBackgroundModes -> "location" to Info.plist
AND
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 9) {
manager.allowsBackgroundLocationUpdates = YES;
}
Before allowing background location updtaes:
enter image description here
After Allowing background location updtaes:
enter image description here
Most of this itinerary has been drawn in the background.
Two things are going on. First, the GPS chip does not always return the same location when standing still. The determined GPS location always fluctuates a bit. iOS does an effort to detect that you're standing still, and then supply the same location, but I think that is done to a lesser extend in Driving mode.
Second, by using the convoluted way to store the samples as strings, you go through a %f conversion, which looses accuracy. That can exaggerate any differences between locations. If you use the CLLocation objects directly, you're likely getting a better result (and much cleaner code):
[self.points addObject:newLocation];
GMSMutablePath *path = [GMSMutablePath path];
for (CLLocation *col in self.points)
{
[path addLatitude:col.latitude longitude:col.longitude];
}
Also, make sure you set the correct settings on the CLLocationManager:
theLocationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
theLocationManager.distanceFilter = kCLDistanceFilterNone;
theLocationManager.activityType = CLActivityTypeOtherNavigation;
theLocationManager.allowsBackgroundLocationUpdates = YES
One other thing. It is also very strange that you change the view in the didUpdateToLocation: method:
self.mapContainerView = mapView_;
You should just use setNeedsDisplay on the existing view, after updating the path.

How to draw GMSPolyline slowly and smoothly in iOS?

I'm able to draw GMSPolyline on GoogleMaps with array of locations like this
GMSMutablePath *path = [GMSMutablePath path];
for (int i = 0; i <anotherArray.count; i++) {
NSString *string1 = [anotherArray2 objectAtIndex:i];
NSString *string2 = [anotherArray objectAtIndex:i];
[path addCoordinate:CLLocationCoordinate2DMake(string1.doubleValue,string2.doubleValue)];
}
GMSPolyline *rectangle = [GMSPolyline polylineWithPath:path];
rectangle.strokeColor = [UIColor blueColor];
rectangle.map = mapView_;
rectangle.strokeWidth = 2.0f;
But the locations I'm getting from backend server.
So I have to refresh every 10 seconds,getting new locations and adding it to existing path. These are all working fine.
But when I am adding new locations to existing path is moving very fast.
Cause I have a GMSMarker at starting of path and it is looking like jumping from one place to another place.
So how can I animate GMSMarker like slowly moving from one place to another place?
Check on this tutorial. It states how to draw the route lines on the map using the GMSPolyline class. From this related SO thread:
Change your else block to be something more like this:
[CATransaction begin];
[CATransaction setAnimationDuration:2.0];
marker.position = coordindates;
[CATransaction commit];
We enable you to use Core
Animation
for animating Google Maps.
For a worked sample, please see
AnimatedCurrentLocationViewController.{c,m} in the SDK sample
application.
Also based from this SO post, avoid using 1px stroke-width of line. Instead, use 4px stroke-width. You can also check on this GitHub sample.

Draw Straight Line Google Maps iOS

hey everyone I'm working on an iOS app and I integrated Google Maps API and I'm using Objective C
I implemented a function that put marks on the maps for every touch to the screen + I get the longitude and latitude of that mark
I want to draw straight lines between those marks, every time I add a new mark it should be linked with the one before
Any ideas please !
GMSMutablePath *path = [GMSMutablePath path];
[path addCoordinate:CLLocationCoordinate2DMake(37.36, -122.0)];
[path addCoordinate:CLLocationCoordinate2DMake(37.45, -122.0)];
GMSPolyline *line = [GMSPolyline polylineWithPath:path];
line.map = mapView_;
In addition to Ekta's answer: If you want to change the line when your Marker is dragged, you just need to follow these steps:
Clear your map using [mapview clear];
Redraw the line using directions given by Ekta.
Hope it helps.

get specific marker using his coordinates in google maps for ios

I am using Google Maps in my app for IOS. I have added several markers in a map and now I need to get a specific one using its coordinates.
I used:
CLLocationCoordinate2D position = CLLocationCoordinate2DMake(51.5, -0.127);
GMSMarker *marker = [GMSMarker markerWithPosition:position];
but I think I need to create a new marker, not to get one created before.
One way to do this is to have an NSMutableArray of the markers and search throughout
them.
for (GMSMarker *marker in arrayOfMarkers){
if(marker.position.latitude == coordinate.latitude && marker.position.longitude == coordinate.longitude){
return marker;
}
NB this assumes that there is only one marker at this location.

Resources