谷歌地图折线无法完美渲染

问题描述 投票:0回答:3

我正在使用最新的 iOS 版谷歌地图 API 绘制折线。我正在逐点构建折线,但它无法正确渲染,因为当我缩小折线时,折线会从地图上消失(不是字面意义上的),而当我放大时,它只会显示该线。

这就是放大时折线的显示方式

这是缩小后的样子

这是我绘制折线的函数

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

我已经覆盖

init:
RCPolyline 是这样的

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

并且

drawPolylineFromPoint:toPoint:
会这样做

 - (void)drawPolylineFromPoint:(CLLocation *)pointX toPoint:(CLLocation *)pointY {
      GMSMutablePath *path = [GMSMutablePath path];
      [path addCoordinate:pointX.coordinate];
      [path addCoordinate:pointY.coordinate];
      self.path = path;} 
ios objective-c google-maps google-maps-api-3
3个回答
4
投票

我发现了问题,我正在制作

RCPolyline
类的本地实例,并调用构造折线的方法,我想要的是为 RCPolyline 实例创建一个全局对象,并更新
GMSPath
类的
RCPolyline
实例

类似这样的:

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

现在我从同一个实例调用这个方法。

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

PS:

RCPolyline
GMSPolyline

的子类

-1
投票

试试这个代码。

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

-1
投票

你有解决办法吗..?谷歌地图折线无法完美渲染

© www.soinside.com 2019 - 2024. All rights reserved.