我成功地获取了一个显示两个坐标之间路径的Google静态地图。
问题在于绘制的路径只是两点之间的直线。
我阅读到,为了能够在静态谷歌地图上绘制两个点之间的“路径”,即沿着道路和城市地理而不是直线运动,需要添加路径的所有坐标/十字路口。
有没有人知道解决这个问题的简单方法?
我成功地获取了一个显示两个坐标之间路径的Google静态地图。
问题在于绘制的路径只是两点之间的直线。
我阅读到,为了能够在静态谷歌地图上绘制两个点之间的“路径”,即沿着道路和城市地理而不是直线运动,需要添加路径的所有坐标/十字路口。
有没有人知道解决这个问题的简单方法?
https://developers.google.com/maps/documentation/javascript/reference/directions#DirectionsService
并将概览路径转换为适合静态地图API要求的格式:
https://developers.google.com/maps/documentation/maps-static/start#Paths
使用折线可以绘制直线。
Polyline类在地图上定义了一种线性覆盖物,由一系列连接的线段组成。Polyline对象由LatLng位置数组组成,并创建一系列按顺序连接这些位置的线段。
您可以在此处查看示例。
https://google-developers.appspot.com/maps/documentation/javascript/examples/polyline-simple
关于折线的内容https://developers.google.com/maps/documentation/javascript/overlays
我认为您无法在静态地图API中使用此功能。但是,您可以使用JavaScript API V3中的Directions。
我深入研究了许多建议和代码,并将它们结合起来,制作了一个非常简单的解决方案,上面的代码应该适用于您:
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_;
}