谷歌地图静态绘制“路线”而非直线

10

我成功地获取了一个显示两个坐标之间路径的Google静态地图。

问题在于绘制的路径只是两点之间的直线。

我阅读到,为了能够在静态谷歌地图上绘制两个点之间的“路径”,即沿着道路和城市地理而不是直线运动,需要添加路径的所有坐标/十字路口。

有没有人知道解决这个问题的简单方法?

4个回答

25

1
要将概览路径转换为静态地图API可用的格式,您可以使用google.maps.geometry.encoding.encodePath()函数进行编码,然后直接使用它。 - Chuck Mah
1
有没有 PHP 的 Web API 解决方案,而不是 JavaScript? - Tharindu
DirectionsService有一个回调函数DirectionsResult,其中包含编码的overview_polyline,您可以在静态地图的“path”参数中使用它,而无需转换,如下所示:"&path=enc:"+response.routes[0].overview_polyline,其中response是回调函数。 - MartinG

0

我认为这不是一个静态地图示例。 - hswner

0

我认为您无法在静态地图API中使用此功能。但是,您可以使用JavaScript API V3中的Directions


谢谢你的回答。是的,获取方向,然后生成路径,我知道你可以用这种方式做到,我只是希望有人已经编写好了这个过程 :) - Pierre
只要你在之前的 API 调用中(通过 Directions API)获取了“编码折线”,就可以在静态地图上绘制路线。请参考 Static Maps API 中的 'enc' 参数。 - 1owk3y

-1

我深入研究了许多建议和代码,并将它们结合起来,制作了一个非常简单的解决方案,上面的代码应该适用于您:

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://maps.googleapis.com/maps/api/directions/json?origin=%f,%f&destination=%f,%f&sensor=false&mode=driving",[startLat floatValue] ,[startLong floatValue], [endLat floatValue], [endLong floatValue]]];

NSDictionary *PathInfo;//the json of the result

NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLResponse *response = nil;
NSError *error = nil;
NSData *responseData =  [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!error) {
    PathInfo = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];
    GMSPath *path =[GMSPath PathInfo[@"routes"][0][@"overview_polyline"][@"points"]];
    GMSPolyline *singleLine = [GMSPolyline polylineWithPath:path];
    singleLine.strokeWidth = 3;
    singleLine.strokeColor = [UIColor redColor];
    singleLine.map = mapView_;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接