Google Maps Utils如何解码列表中的折线值?

时间:2017-04-03 11:55:58

标签: java android google-maps directions google-polyline

我使用Google Maps Directions Api和Google Maps Utils库在我的位置和标记之间绘制路径,我从Directions Api中检索字段,但是折线是绘制不对,所以现在我正在尝试逐步构建折线,但是我遇到了问题,我能够将所有折线添加到arraylist但是现在我如何从arraylist解码并构建它们;

这是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`p_t@GB{@t@MFQPMLKXETEZCVCL?@?BAD?@?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);

如何解码列表中的值?

2 个答案:

答案 0 :(得分:10)

您可以使用Google Maps Android API Utility Library中的PolyUtil.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));
}

答案 1 :(得分:-1)

创建自定义模型,并创建该自定义模型的数组列表以存储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();