Google Maps Utils how to decode polylines values from list?
Asked Answered
L

2

6

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;

This is the 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:

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?

Lubricious answered 3/4, 2017 at 11:55 Comment(0)
P
17

You can use the PolyUtil.decode method from the Google Maps Android API Utility Library:

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

In your case, if you want to decode from your 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));
}
Pinch answered 3/4, 2017 at 12:15 Comment(5)
I forgot to mentioned that I'm using a hashmap (caminho) :) and your code doesn't work with a hashmap, can you help?Lubricious
"for (String polyline : listaPolylines)" still doesn't work it requires hashmapLubricious
I have to add the PolylineOptions().addAll(decoded)", inside the for, right?Lubricious
That's it. Updated againPinch
Let us continue this discussion in chat.Lubricious
W
-1

Create a custom model, and create an array list of that custom model to store your 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();
Wrestling answered 3/4, 2017 at 12:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.