-1

I'm creating a hicking app in android, and so far, i can insert my path inserting various markers along the way i want to create, now i want to draw a polyline between the markers that i get from my mysql database. Would appreciate some guide to the best way i can draw the lines between my markers.

public void getCoordsId(final String id_trilho) {
    RequestQueue requestQueue = Volley.newRequestQueue(MapsActivity.this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, urlget,
            new Response.Listener<String>() {
                LatLng location;
                @Override
                public void onResponse(String response) {
                    System.out.println(response);
                    Toast.makeText(MapsActivity.this, "boa entrou", Toast.LENGTH_LONG).show();
                    try {
                        JSONArray array = new JSONArray(response);
                        for (int i = 0; i < array.length(); i++) {
                            JSONObject jo = array.getJSONObject(i);

                            Double lat = Double.parseDouble(jo.getString("lat"));
                            Double lng = Double.parseDouble(jo.getString("lon"));
location = new LatLng(lat,lng);
                              MarkerOptions options = new MarkerOptions();
                            options.position(location);
                            mMap.addMarker(options);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

Basically i get the markers on a button click, i would like to know how i can add the lines.

Thank you for any help in advance

Sapo121
  • 71
  • 1
  • 12

1 Answers1

0

First of all you save all the lat lng in a data structure. Let's say an ArrayList<LatLng>. Then you pass it to the following function (that is basically a copy paste from this link):

private void createPolylinesFromLatLng(ArrayList<LatLng> list){
    if(myMap != null && list != null && list.size > 0){
        PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
        for (LatLng z : list) {
            LatLng point = list.get(z);
            options.add(point);
        }
        line = myMap.addPolyline(options);
    }
}
Maxime Claude
  • 965
  • 12
  • 27