【问题标题】:How to iterate over the path from Google Directions API?如何从 Google Directions API 迭代路径?
【发布时间】:2017-04-26 09:45:40
【问题描述】:

我正在尝试沿两个选定的标记移动标记并设置它们的坐标。

我正在从这段代码中获取路径:

GMSPath *path1 =[GMSPath pathFromEncodedPath:self.dataReceive[@"routes"][0][@"overview_polyline"][@"points"]];

当我使用 for 循环将第一个标记移动到第二个标记的位置时。它走的是一条直线路径,但它应该沿着从谷歌方向 API 获取的路径坐标移动。

    for (int i = 0; i< path1.count; i++) {

        CLLocationCoordinate2D position = [path1 coordinateAtIndex:i];
        [CATransaction begin];
        [CATransaction setAnimationDuration:50];
        self.marker.position = position;
        self.marker.map = self.mapView;
        [CATransaction commit];
    }

谢谢。

【问题讨论】:

    标签: ios google-maps google-polyline


    【解决方案1】:

    试试这段代码 Objective C。

     - (void)showPathFromCurrentLocationForCoordinate:(CLLocationCoordinate2D)coord{
    CLLocationCoordinate2D destination = coord;
    NSMutableString *urlString = [[NSMutableString alloc] initWithString:@"https://maps.googleapis.com/maps/api/directions/json?"];
    [urlString appendString:[NSString stringWithFormat:@"origin=%f,%f&destination=%f,%f&sensor=true",self.deviceLocation.latitude,self.deviceLocation.longitude,destination.latitude,destination.longitude]];
    
    [RestApi getPath:urlString withParameter:nil withHandler:^(id responseObject, NSError *error, NSURLResponse *response) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (error == nil) {
                NSDictionary *json = responseObject;
                NSArray *routes = [json objectForKey:@"routes"];
                if (routes != nil && routes.count > 0) {
                    NSDictionary *route = [routes objectAtIndex:0];
                    long distance = 0;
                    NSArray *legs = [route objectForKey:@"legs"];
                    for (NSDictionary *leg in legs) {
                        long dist = [leg[@"distance"][@"value"] longValue];
                        distance = distance + dist;
                    }
                    GMSPath *path =[GMSPath pathFromEncodedPath:route[@"overview_polyline"][@"points"]];
                    GMSPolyline *line = [GMSPolyline polylineWithPath:path];
                    line.strokeColor = PATH_STROKE_COLOR;
                    line.strokeWidth = PATH_STROKE_WIDTH;
                    line.map = mapView;
                    GMSMutablePath *markerpath = [GMSMutablePath path];
                    [markerpath addCoordinate: self.deviceLocation];
                    [markerpath addCoordinate: marker.position];
    
                    GMSCoordinateBounds *bonds = [[GMSCoordinateBounds alloc] initWithPath:markerpath];
                    [CATransaction begin];
                    [CATransaction setValue:[NSNumber numberWithFloat: 1.0] forKey:kCATransactionAnimationDuration];
                    [mapView animateWithCameraUpdate:[GMSCameraUpdate fitBounds:bonds withPadding:MAP_BOUNDS_OFFSET_PADDING]];
                    [CATransaction commit];
                }
                else{
                    NSLog(@"Google direction API failed.");
                }
            }
            else if (error != nil){
                NSLog(@"%@",error.localizedDescription);
            }
        });
    }];
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-22
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-13
      相关资源
      最近更新 更多