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 overview-polyline 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 overview-polyline:
...
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?
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With