Search code examples
iosobjective-cgoogle-mapsgoogle-maps-api-3

Google maps polyline not rendering perfectly


I am drawing polyline using latest google maps API for iOS. I am constructing polyline point by point but it is not rendering properly as when i zoom out the polyline vanishes(not in literal terms) from the map and when i zoom in it simply shows the line.

Zoomed in view This is how polyline appears when zoomed in

Zoomed out view This is how it appears when zoomed out

here is my function for drawing polyline

RCPolyline *polyline = [[RCPolyline alloc] init];
[polyline drawPolylineFromPoint:self.selectedEmployee.location toPoint:location];

i have override init: for RCPolyline to be something like this

- (instancetype)init {
self = [super init];
if (self) {
    self.strokeWidth = 5.0f;
    self.strokeColor = UIColor.redColor;
    self.geodesic = YES;
    self.map = [RCMapView sharedMapView];
}
return self;}

and drawPolylineFromPoint:toPoint: does this

 - (void)drawPolylineFromPoint:(CLLocation *)pointX toPoint:(CLLocation *)pointY {
      GMSMutablePath *path = [GMSMutablePath path];
      [path addCoordinate:pointX.coordinate];
      [path addCoordinate:pointY.coordinate];
      self.path = path;} 

Solution

  • I found the glitch, i was making local instance of RCPolyline class and was calling the method for constructing polyline from that what i wanted was to have a global object for RCPolyline instance and update the GMSPath for the RCPolyline class instance

    something like this:

    - (instancetype)initWithMap:(GMSMapView *)mapView {
        self = [super init];
        if (self) {
          self.strokeWidth = 4.0f;
          self.strokeColor = [UIColor redColor];
          self.geodesic = YES;
          self.map = mapView;
          self.mutablePath = [GMSMutablePath path];
        }
          return self;}
    

    and now i am calling this method from that same instance.

    - (void)appendPolylineWithCoordinate:(CLLocation *)location {
        [self.mutablePath addCoordinate:location.coordinate];
        self.path = self.mutablePath;}
    

    PS: RCPolyline is subclass of GMSPolyline