【问题标题】:Google maps polyline not rendering perfectly谷歌地图折线渲染不完美
【发布时间】:2017-07-16 06:07:09
【问题描述】:

我正在使用最新的 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;} 

【问题讨论】:

  • 我猜你的折线路径数据太多了。
  • @wf9a5m75 和这有什么关系?
  • 路径的绘制点使用内存。很多点意味着适用于 iOS 的 Google Maps SDK 使用大量内存。我想这就是原因。为了减少你的分数,你可以对你的路径进行编码。在这里查看stackoverflow.com/questions/30393067/…
  • @wf9a5m75 我解决了我的问题,我提供了我出错的答案,但感谢您的意见。

标签: ios objective-c google-maps google-maps-api-3


【解决方案1】:

我发现了故障,我正在制作 RCPolyline 类的本地实例,并正在调用构造折线的方法,我想要的是为 RCPolyline 实例创建一个全局对象,并为 @987654323 更新 GMSPath @类实例

类似这样的:

- (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:RCPolylineGMSPolyline 的子类

【讨论】:

    【解决方案2】:

    试试这个代码。

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

    【讨论】:

    • 我没有使用 API 来获取两个坐标之间的路线 我正在记录用户移动和绘制位置数据时的位置数据
    猜你喜欢
    • 2014-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-07
    • 1970-01-01
    • 2014-04-19
    • 2011-07-02
    相关资源
    最近更新 更多