我正在使用Google Maps Directions API和Google Maps Utils庫在我的位置與標記之間繪製路徑,我從Directions API檢索字段overview-polyline,但繪製的多段線不正確,所以現在我試圖一步一步地構建多段線,但是我面臨一個問題,我能夠將所有多段線添加到數組列表中,但是現在如何從數組列表中檢索它們以解碼和構建它們;谷歌地圖實用程序如何解碼列表中的多段線值?
這是JSON:
{
geocoded_waypoints: [
{},
{}
],
routes: [
{
bounds: {},
copyrights: "Dados do mapa ©2017 Google, Inst. Geogr. Nacional",
legs: [
{
distance: {},
duration: {},
end_address: "14 Avenue Latécoère, 31700 Cornebarrieu, França",
end_location: {},
start_address: "R. Zeferino Brandão 9, 2005 Santarém, Portugal",
start_location: {},
steps: [
{
distance: {},
duration: {},
end_location: {},
html_instructions: "Siga para <b>noroeste</b> na <b>R. Zeferino Brandão</b> em direção a <b>Tv. de São Domingos</b>",
polyline: {
points: "k|nnF`[email protected]{@[email protected][email protected][email protected]?DAFC|@"
},
start_location: {},
travel_mode: "DRIVING"
},
{},
這是我的解碼overview-polyline:
...
JSONObject singleRout = (JSONObject) routes.get(0);
JSONObject overview_polyline = (JSONObject) singleRout.get("overview_polyline");
if (overview_polyline != null) {
points = overview_polyline.getString("points");
}
...
protected void fazerCaminho() {
List<LatLng> decodedPath = PolyUtil.decode(points);
if (line == null) {
line = mMap.addPolyline(new PolylineOptions()
.width(3)
.color(Color.rgb(25, 151, 152))
.geodesic(true)
.addAll(decodedPath));
} else {
line.remove();
line = mMap.addPolyline(new PolylineOptions()
.width(3)
.color(Color.rgb(25, 151, 152))
.geodesic(true)
.addAll(decodedPath));
}
}
現在,我將所有的點到ArrayList是這樣的:
JSONObject singlePolyline = (JSONObject) singleStep.get("polyline");
if (singlePolyline != null) {
points = singlePolyline.getString("points");
caminho.put("points" , points);
}
listaPolylines.add(caminho);
如何解碼列表中的值?
我忘了提及,我用一個HashMap(caminho):)和您的代碼不會與一個HashMap的工作,你能幫忙嗎? – alb
我已經更新了我的答案。請再次檢查 – antonio
「(String polyline:listaPolylines)」仍然不起作用它需要hashmap – alb