Google maps polyline not rendering perfectly

有些话、适合烂在心里 提交于 2019-12-23 07:31:47

问题


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.

This is how polyline appears when zoomed in

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;} 

回答1:


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




回答2:


Try this code.

- (void)fetchPolylineWithOrigin:(CLLocation *)origin destination:(CLLocation *)destination {

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lat longitude:longg zoom:12];
    GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView.myLocationEnabled = YES;
    self.view = mapView;
    GMSMarker *marker = [[GMSMarker alloc] init];
    marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
    marker.map = mapView;
    NSString *originString = [NSString stringWithFormat:@"%f,%f", origin.coordinate.latitude, origin.coordinate.longitude];
    NSString *destinationString = [NSString stringWithFormat:@"%f,%f", destination.coordinate.latitude, destination.coordinate.longitude];
    NSString *directionsAPI = @"https://maps.googleapis.com/maps/api/directions/json?";
    NSString *directionsUrlString = [NSString stringWithFormat:@"%@&origin=%@&destination=%@&mode=driving&key=%@&alternatives=true", directionsAPI, originString, destinationString,@"YOUR API KEY"];
    NSURL *directionsUrl = [NSURL URLWithString:directionsUrlString];
    NSURLSessionDataTask *fetchDirectionsTask = [[NSURLSession sharedSession] dataTaskWithURL:directionsUrl completionHandler:
                                                 ^(NSData *data, NSURLResponse *response, NSError *error)
                                                 {
                                                     NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
                                                     if(error)
                                                     {
                                                         return;
                                                     }
                                                     NSArray *routesArray = [json objectForKey:@"routes"];
                                                     GMSPolyline *polyline = nil;
                                                     int i=1;
                                                     for (id route in routesArray)
                                                     {
                                                         NSDictionary *routeDict = [route valueForKey:@"overview_polyline"];
                                                         NSString *points = [routeDict objectForKey:@"points"];
                                                         GMSCoordinateBounds *bounds = [[GMSCoordinateBounds alloc] init];
                                                         GMSPath *path = [GMSPath pathFromEncodedPath:points];
                                                         polyline = [GMSPolyline polylineWithPath:path];
                                                         polyline.strokeWidth = 3;
                                                         if(i==1)
                                                         {
                                                             polyline.strokeColor = [UIColor greenColor];

                                                         }else if(i==2)
                                                         {
                                                             polyline.strokeColor = [UIColor redColor];
                                                         }else{
                                                             polyline.strokeColor = [UIColor blueColor];
                                                         }
                                                         i = i+1;

                                                         bounds = [bounds includingCoordinate:marker.position];
                                                         polyline.map=mapView;
                                                     }
                                                 }];
    [fetchDirectionsTask resume];
}


来源:https://stackoverflow.com/questions/42455063/google-maps-polyline-not-rendering-perfectly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!