我想在地图上绘制与我通过Google Directions API获得的路线JSON相对应的路线:http://code.google.com/apis/maps/documentation/directions/
我已经想出如何从阶段字段中提取纬度和经度,但这并不能很好地遵循弯曲的道路.我认为我需要的是解码折线信息,我找到了关于如何编码折线的Googles说明:http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html
我确实在这里找到了一些代码用于Android以及解码折线的Javascript,例如:
使用Google Directions API地图查看绘制路线 - 解码折线
但是对于Objective-C iPhone代码我找不到相同的内容,任何人都可以帮我这个吗?我敢肯定,如果必须的话,我可以自己做,但如果它已经可以在某个地方使用,那肯定能节省我一些时间...
编辑:这里的关键是能够逐个字符地解码base64编码.更具体地说,我在谷歌的JSON中得到了类似的东西,它使用base64编码进行编码:
... "overview_polyline" : {
"points" : "ydelDz~vpN_@NO@QEKWIYIIO?YCS@WFGBEBICCAE?G@y@RKBEBEBAD?HTpB@LALALCNEJEFSP_@LyDv@aB\\GBMB"
},
...
Run Code Online (Sandbox Code Playgroud)
注意:我应该提一下,这个问题是指Google Maps API v1,使用GMSPolyLine polyLineWithPath在v2中更容易实现,因为下面的许多答案都会告诉你(感谢@cdescours).
iphone json objective-c google-directions-api google-polyline
我正在绘制地图中两点之间的路线.我这样得到了积分:
StringBuilder urlString = new StringBuilder();
urlString.append("http://maps.googleapis.com/maps/api/directions/json");
urlString.append("?origin=");// from
urlString.append(Double.toString(src.latitude));
urlString.append(",");
urlString.append(Double.toString(src.longitude));
urlString.append("&destination=");// to
urlString.append(Double.toString(dest.latitude));
urlString.append(",");
urlString.append(Double.toString(dest.longitude));
urlString.append("&sensor=false&mode=");
if (tipo != null) {
urlString.append(tipo);
}
return urlString.toString;
Run Code Online (Sandbox Code Playgroud)
我收到了Google的回复,并获得了JSON:
resp = new JSONObject(builder.toString());
Log.i("Location", "Contenido del kml: "+resp);
JSONArray routeObject = resp.getJSONArray("routes");
JSONObject routes = routeObject.getJSONObject(0);
JSONObject overviewPolylines = routes
.getJSONObject("overview_polyline");
String encodedString = overviewPolylines.getString("points");
ArrayList<LatLng> puntos=decodePoly(encodedString);
Run Code Online (Sandbox Code Playgroud)
我从谷歌获得的回复:(采用JSON格式):
Response: {"status":"OK","routes":[{"waypoint_order":[],"summary":"R-3 and A-3","bounds":{"southwest":{"lng":-3.676540000000001,"lat":40.00040000000001},"northeast":{"lng":-2.99933,"lat":40.43357}},"legs":[{"duration":{"value":3267,"text":"54 mins"},"distance":{"value":85039,"text":"85.0 km"},"end_location":{"lng":-2.99933,"lat":40.00040000000001},"start_address":"Calle del General Díaz Porlier, 91, 28006 Madrid, Spain","end_address":"Camino Veinticuatro, 2, 16400 Tarancón, Cuenca, Spain","start_location":{"lng":-3.676540000000001,"lat":40.43331000000001},"via_waypoint":[],"steps":[{"html_instructions":"Head <b>north<\/b> on <b>Calle …Run Code Online (Sandbox Code Playgroud) 是的,所以我目前正在我的应用中使用Google Directions API来检索两个位置之间的路线.
当我发送路线指示请求时,我会在JSON中检索有关路线的一些细节,包括沿路线的每条道路的名称,相应的开始和结束纬度坐标以及它们的折线值.
例如:如果我在两条道路之间发送请求http://maps.googleapis.com/maps/api/directions/json?origin=redfern+ave,+dublin&destination=limetree+ave,+dublin&sensor=false,我会得到遵循JSON响应(沿路线输出一条道路).
{
"distance" : {
"text" : "0.2 km",
"value" : 203
},
"duration" : {
"text" : "1 min",
"value" : 18
},
"end_location" : {
"lat" : 53.435250,
"lng" : -6.132140000000001
},
"html_instructions" : "Head \u003cb\u003eeast\u003c/b\u003e on \u003cb\u003eRedfern Ave.\u003c/b\u003e toward \u003cb\u003eMartello Court\u003c/b\u003e",
**"polyline" : {
"points" : "woceIvgmd@O}DOkDQqF"**
},
Run Code Online (Sandbox Code Playgroud)
到目前为止,我的应用程序解析此信息,并在列表视图中列出道路和方向,如下所示:

我想要做的是在地图上突出显示从A到B的整个路线,但是我在网上找不到有关如何在新的Google Maps API v2上执行此操作的任何有用信息.我看到使用折线而不是叠加来在Google Maps v2上绘制线条,但是据我所知,它们只绘制直线,这对我来说毫无用处.无论如何使用我掌握的信息突出显示路线(道路名称,开始和结束纬度坐标,折线点?任何帮助表示赞赏.
此外,我看到响应中有一个"折线"值可能很有用,但我无法弄清楚如何解析或使用这些信息.有谁知道我如何能够理解这个值来绘制折线?
**"polyline" : {
"points" : "woceIvgmd@O}DOkDQqF"**
Run Code Online (Sandbox Code Playgroud)
编辑:我的解决方案代码在下面的答案中提供.
android routes highlight directions google-maps-android-api-2
android ×2
directions ×1
google-maps ×1
highlight ×1
iphone ×1
json ×1
objective-c ×1
polyline ×1
routes ×1