简体   繁体   English

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

[英]Google Maps Utils how to decode polylines values from list?

I'm using Google Maps Directions Api and the Google Maps Utils library to draw a path between my location to a marker, I was retrieving the field from the Directions Api, but the polyline that was drawn was not right so now I'm trying to build the polyline step by step, but I'm facing a problem, I was able to add to a arraylist all the polylines but now how do I retrieve them from the arraylist to decoded and build them; 我正在使用谷歌地图方向Api和谷歌地图实用程序库绘制我的位置与标记之间的路径,我正在从方向Api检索字段 ,但绘制的折线不正确所以现在我我正在尝试逐步构建折线,但是我遇到了一个问题,我能够将所有折线添加到arraylist但现在我如何从arraylist中检索它们来解码并构建它们;

This is the JSON: 这是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"
},
{},

This is how I decoded the : 这就是我解释

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

Now I'm adding all the points to the arraylist like this: 现在我将所有的点添加到arraylist中,如下所示:

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

    listaPolylines.add(caminho);

How do I decode now the values from the list? 如何解码列表中的值?

You can use the PolyUtil.decode method from the Google Maps Android API Utility Library : 您可以使用Google Maps Android API Utility Library中PolyUtil.decode方法:

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

In your case, if you want to decode from your listaPolylines : 在您的情况下,如果您想从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));
}

Create a custom model, and create an array list of that custom model to store your LatLng. 创建自定义模型,并创建该自定义模型的数组列表以存储LatLng。

Something like below: 如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM