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

Google Maps Utils how to decode polylines values from list?

我正在使用 Google Maps Directions Api 和 Google Maps Utils 库在我的位置和标记之间绘制一条路径,我正在检索字段 来自方向 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`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));
    }
}

现在我将所有点添加到数组列表中,如下所示:

JSONObject singlePolyline = (JSONObject) singleStep.get("polyline");
    if (singlePolyline != null) {
        points = singlePolyline.getString("points");
        caminho.put("points" , points);
    }

    listaPolylines.add(caminho);

现在如何解码列表中的值?

您可以使用 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));
}

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