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];
Related
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.
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.
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;
I am using google map sdk for ios to provide directions between current user location and an end location. I have so far achieved to draw a GMSPolyline between the current user location and the end location using the code below and it's working great.
GMSPath *encodedPath = [GMSPath pathFromEncodedPath:encodedPathSting];
self.polyline = [GMSPolyline polylineWithPath:encodedPath];
self.polyline.strokeWidth = 4;
self.polyline.strokeColor = [UIColor colorWithRed:55.0/255.0 green:160.0/255.0 blue:250.0/255.0 alpha:1.0];;
self.polyline.map = self.mapView;
Is it possible to remove a part of the GMSPolyline that has been covered by the user through driving/walking? The GMSPolyline must gradually decrease in length as we trace the path.
One way to achieve this is by redrawing the path repeatedly but this is not or may not be efficient.
Thanks.
So get the latlng point of the polyline in an array as described here:
//route is the MKRoute in this example
//but the polyline can be any MKPolyline
NSUInteger pointCount = route.polyline.pointCount;
//allocate a C array to hold this many points/coordinates...
CLLocationCoordinate2D * routeCoordinates = malloc(pointCount * sizeof(CLLocationCoordinate2D));
//get the coordinates (all of them)...
[route.polyline getCoordinates: routeCoordinates
range: NSMakeRange(0, pointCount)
];
//this part just shows how to use the results...
NSLog(#"route pointCount = %d", pointCount);
for (int c = 0; c < pointCount; c++) {
NSLog(#"routeCoordinates[%d] = %f, %f",
c, routeCoordinates[c].latitude, routeCoordinates[c].longitude);
}
//free the memory used by the C array when done with it...
free(routeCoordinates);
Then, implement a while loop for the first point as you move along the path like this:
int c = 0;
while (pointCount.size() > 0)
{
pointCount.get(0).remove();
}
Note: I'm not that experienced with iOS and haven't tested this solution. Treat it as a suggestion rather than a fix. Thanks!
Hope it helps!
I wants to draw polyline for walk like google map app in ios using google maps ios sdk. For more clear understanding i am uploading image that is from google map app(ios.)
Are you asking how to achieve the dotted polyline effect? If so, I don't believe that is supported in the SDK.
You can manually create a similar effect with GMSCircles.
for(int x = 0; x < [self.path count]; x++)
{
CLLocationCoordinate2D coord = [self.path coordinateAtIndex:x];
//draw circle coord
GMSCircle *circle = [GMSCircle circleWithPosition:coord radius:20];
circle.fillColor = [UIColor blueColor];
circle.strokeColor = [UIColor blackColor];
circle.strokeWidth = 2;
circle.map = mapView;
}
For this to really look like the original example you will probably need to add additional points onto the line in order for the circles to be evenly spaced out. For that you could do something like this.
for(all the points in the path)
{
if(the distance from pointA to pointB is > some distance)
{
centerPtr = center point of pointA and pointB
insert centerPt in path
}
}
You can turn this into a simple recursive function that should give you something similar to what you are looking for.