谷歌地图 - 当地图放大时,使路线沿着街道行进

时间:2014-07-09 20:51:10

标签: ios objective-c cocoa-touch google-maps google-maps-sdk-ios

我遇到的问题与以下SO问题中描述的相同:

(放大时路线不跟随街道)

MapKit - Make route line follow streets when map zoomed in

Route drawing on Google Maps for iOS not following the street lines

但似乎没有任何解决上述问题的答案。

我通过以下函数添加指向我的GMSMapView地图:

-(void) addPointToMap:(CLLocationCoordinate2D) coordinate
{
    CLLocationCoordinate2D position = CLLocationCoordinate2DMake(
                                                                 coordinate.latitude,
                                                                 coordinate.longitude);
    GMSMarker *marker = [GMSMarker markerWithPosition:position];
    marker.map = mapView_;
    [waypoints_ addObject:marker];
    NSString *positionString = [[NSString alloc] initWithFormat:@"%f,%f",
                                coordinate.latitude,coordinate.longitude];
    [waypointStrings_ addObject:positionString];
    if([waypoints_ count]>1){
        NSString *sensor = @"false";
        NSArray *parameters = [NSArray arrayWithObjects:sensor, waypointStrings_,
                               nil];
        NSArray *keys = [NSArray arrayWithObjects:@"sensor", @"waypoints", nil];
        NSDictionary *query = [NSDictionary dictionaryWithObjects:parameters
                                                          forKeys:keys];
        MDDirectionService *mds=[[MDDirectionService alloc] init];
        SEL selector = @selector(addDirections:);
        [mds setDirectionsQuery:query
                   withSelector:selector
                   withDelegate:self];
    }

}

这里是setDirectionsQuery函数:

static NSString *kMDDirectionsURL = @"http://maps.googleapis.com/maps/api/directions/json?";

- (void)setDirectionsQuery:(NSDictionary *)query withSelector:(SEL)selector
              withDelegate:(id)delegate{
  NSArray *waypoints = [query objectForKey:@"waypoints"];
  NSString *origin = [waypoints objectAtIndex:0];
  int waypointCount = [waypoints count];
  int destinationPos = waypointCount -1;
  NSString *destination = [waypoints objectAtIndex:destinationPos];
  NSString *sensor = [query objectForKey:@"sensor"];
  NSMutableString *url =
  [NSMutableString stringWithFormat:@"%@&origin=%@&destination=%@&sensor=%@",
   kMDDirectionsURL,origin,destination, sensor];
  if(waypointCount>2) {
    [url appendString:@"&waypoints=optimize:true"];
    int wpCount = waypointCount-2;
    for(int i=1;i<wpCount;i++){
      [url appendString: @"|"];
      [url appendString:[waypoints objectAtIndex:i]];
    }
  }
    url = [url
           stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
    _directionsURL = [NSURL URLWithString:url];
  [self retrieveDirections:selector withDelegate:delegate];
}

注意:我已按照此Google教程进行了一些修改:

https://www.youtube.com/watch?v=AdV7bCWuDYg

在此先感谢,任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

最后我找到了解决方案,感谢WWJD在他的问题中的最后编辑!

Route drawing on Google Maps for iOS not following the street lines

答案:

我之前基本上做的是,我只使用我在路线中收到的信息,而如果你检查了你从Google Directions API收到的JSON文件,你会看到你得到的很多更多信息在和。这是我们生成正确结果和正确折线所需的信息。