2017-04-26 63 views
1

我正在嘗試將標記沿着兩個選定的標記以及它們的座標設置。如何迭代Google Directions API的路徑?

我從這個代碼得到的路徑:

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

當我使用for循環到第一標記移動到第二標記的位置。它採取了直線路徑,但它應該沿着從google方向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]; 
    } 

謝謝。

回答

0

嘗試此代碼目的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); 
     } 
    }); 
}]; 

}

相關問題