4

I've been following this tutorial that helps me draw route directions(polyline) between 2 tapped places on map. However I don't need to get tapped locations since by the time I want to show user the route, I'll already have latitude and longitude of origin and destination.

Can I create GMSPath with an array of latitude and longitude I already have? If so, how..?

- (void)addDirections:(NSDictionary *)json {

NSDictionary *routes = [json objectForKey:@"routes"][0];

NSDictionary *route = [routes objectForKey:@"overview_polyline"];
NSString *overview_route = [route objectForKey:@"points"];
GMSPath *path = [GMSPath pathFromEncodedPath:overview_route];


GMSPolyline *polyline = [GMSPolyline polylineWithPath:path];
polyline.map = self.mainGoogleMap;}
durazno
  • 559
  • 2
  • 7
  • 25

1 Answers1

9

Yes you can do it via following method.

First you need to create NSMutablePath global object.

Then you have to add latitude and longitude in that path like below and then assign that path to polyline object and your polyline is drawn

In viewDidLoad

 pathDynamic = [[GMSMutablePath alloc] init];

When you want to add the location in mutable path

   CLLocation *loc = [[CLLocation alloc] initWithLatitude:[[dictData valueForKey:@"fl_route_latitude"] doubleValue] longitude:[[dictData valueForKey:@"fl_route_longitude"] doubleValue]];
        
    [pathDynamic addCoordinate:loc.coordinate];//Add your location in mutable path
   
    GMSPolyline *polyline = [GMSPolyline polylineWithPath:pathDynamic];
    // Add the polyline to the map.
    polyline.strokeColor = AppOrangeColor;
    polyline.strokeWidth = 3.0f;
    polyline.map = [self getMapView];
Community
  • 1
  • 1
Chirag Shah
  • 3,034
  • 1
  • 30
  • 61
  • But wouldn't that just draw a straight line between 2 places..? Would that actually draw the appropriate route? – durazno Aug 24 '15 at 09:20
  • @durazno Yes draw straight line two line between two location. if you want actually route then you need the perfect location array or encode polyline. – Chirag Shah Aug 24 '15 at 09:27
  • What do you mean by perfect location array? – durazno Aug 24 '15 at 09:29
  • @durazno **EncodedPath** is a array location which is encoded. Perfect location means each and every location come in between that two places. – Chirag Shah Aug 24 '15 at 09:37