【问题标题】:Google Maps - Make route line follow streets when map zoomed in谷歌地图 - 地图放大时让路线跟随街道
【发布时间】:2014-07-09 20:51:09
【问题描述】:

我遇到了与以下 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];
}

注意:我已经按照这个谷歌教程做了一些修改:

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

提前致谢,任何帮助将不胜感激!

【问题讨论】:

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


    【解决方案1】:

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

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

    来自答案:

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

    【讨论】:

      猜你喜欢
      • 2012-10-12
      • 2019-11-06
      • 2011-10-12
      • 2014-08-14
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多