2017-04-03 36 views
1

我正在使用Google Maps Directions API和Google Maps Utils庫在我的位置與標記之間繪製路徑,我從Directions API檢索字段,但繪製的多段線不正確,所以現在我試圖一步一步地構建多段線,但是我面臨一個問題,我能夠將所有多段線添加到數組列表中,但是現在如何從數組列表中檢索它們以解碼和構建它們;谷歌地圖實用程序如何解碼列表中的多段線值?

這是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" 
}, 
{}, 

這是我的解碼

... 
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); 

如何解碼列表中的值?

回答

6

您可以使用從Google Maps Android API Utility LibraryPolyUtil.decode方法:

List<LatLng> decoded = PolyUtil.decode(caminho.get("points")); 

在你的情況,如果你想從你的listaPolylines解碼:

for (Map polyline : listaPolylines) { 
    List<LatLng> decoded = PolyUtil.decode(polyline.get("points")); 

    // Do something with your decoded polyline. For example drawing it on your map 
    mMap.addPolyline(new PolylineOptions().addAll(decoded)); 
} 
+0

我忘了提及,我用一個HashMap(caminho):)和您的代碼不會與一個HashMap的工作,你能幫忙嗎? – alb

+0

我已經更新了我的答案。請再次檢查 – antonio

+1

「(String polyline:listaPolylines)」仍然不起作用它需要hashmap – alb

0

創建一個自定義模式,並創建一個數組該自定義模型的列表來存儲您的LatLng。

類似下面:

public class CustomLocations{ 
private LatLng latLng; 
public customLocations(LatLng latLng){ 
this.latLng = latLng 
} 
public void setLatLng(LatLng latLng){ 
this.latLng = latLng 
} 
public LatLng getLatLng(){ 
return latLng; 
} 
} 

//Now, create an array list: 
ArrayList<CustomLocations> yourArray = new ArrayList<>(); 

//add locations: 
yourArray.add(new CustomLocations(yourLatLng)); 

//retrieve: 
yourArray.get(i).getLatLng(); 
相關問題