【问题标题】:Google map SDK - Path not accurate谷歌地图 SDK - 路径不准确
【发布时间】:2016-04-12 17:44:46
【问题描述】:

我试图显示两个位置之间的路径,但它不准确。

if(center.latitude != 0 && center.longitude != 0)
            {
            NSString *urlString = [NSString stringWithFormat:
                                   @"%@?origin=%f,%f&destination=%f,%f&sensor=true&key=%@",
                                   @"https://maps.googleapis.com/maps/api/directions/json",
                                   center.latitude,
                                   center.longitude,
                                   center1.latitude,
                                   center1.longitude,
                                   KGoogleMapServerKey];

            NSError *error;
                NSDictionary *json =[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString] options:NSDataReadingUncached error:&error] options:NSJSONReadingMutableContainers error:&error];
            if(json[@"routes"] != nil && [json[@"routes"] count] > 0)
            {
                GMSPath *path =[GMSPath pathFromEncodedPath:json[@"routes"][0][@"overview_polyline"][@"points"]];
                GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
                singleLine.strokeWidth = 7;
                singleLine.strokeColor = [UIColor colorWithRed:56.0/255.0 green:163.0/255.0 blue:249.0/255.0 alpha:1.0];
                singleLine.map = mapViewGoogle;
            }  

这是我得到的路径:

如您所见,蓝线(路径)并未到达目的地。
如何解决这个问题?

【问题讨论】:

  • 我认为谷歌不知道如何到达目的地,因为没有路线,或者在私人区域,诸如此类。

标签: ios google-maps


【解决方案1】:

您的路线太长了。在来自 Google Directions API 的每个响应中,点数是有限的。在您的示例中,您收到 N 个点,用它们绘制路线并将 mapView 缩放到仅放置四个点的区域。
要始终显示准确的路线,您应该在每次地图缩放后重新计算路线(包括保持路线相同的中点)。
例如,屏幕上的完整路线有 50 分。您正在缩放地图,因此仅显示 4 个点(您的屏幕截图)。现在以#1 为起点,#5 为终点,#2 #3 #4 为中点,并发送新请求以查找这些点的路线。您将获得新的 50 分,使用它们您可以更准确地绘制这部分路线。

【讨论】:

  • 这似乎是有道理的。谢谢
【解决方案2】:

关键是没有通往该目的地的路线,您可以在此处看到:Gmap of your Example

我猜你最好的选择是使用 Gmaps 已经使用的相同方法,我的意思是使用从最后一点到实际点的点/虚线折线。

你可以在这里找到几个例子来实现你的折线类型:

Creating a dashed using IOS SDK

【讨论】:

  • 好吧,如果这是真的,那么为什么 Google 地图会在 web 上正确显示路径?
猜你喜欢
  • 2012-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-16
  • 2013-10-28
相关资源
最近更新 更多