2

I´ve created a Map Fragment, which adds markers from an arraylist (parsed XML) to a Google map. Between the markers I draw a polyline. All that works fine, but the lines are drawn straight although I´ve activated geodesic.

Is there specific method or code I´ve to implement to get geodesic work. I would like to have a line, which follows the street from on point to another. Or does Geodesic only work with 2 markers? I´ve got more than two.

private void setUpMap() {

            PolylineOptions routeDraw = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);

            try {

                for (RowItem aPoisMap : poisMap) {
                    latitude = aPoisMap.getLatitudePoi();
                    longitude = aPoisMap.getLongitudePoi();
                    poiTitle = aPoisMap.getTitle();
                    poiDesc = aPoisMap.getDesc();
                    marker = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title(poiTitle).snippet(poiDesc));
                    routeDraw.add(new LatLng(latitude, longitude));
                }
                Polyline polyline = mMap.addPolyline(routeDraw.geodesic(true));
                latitudeZoom = poisMap.get(0).getLatitudePoi();
                longitudeZoom = poisMap.get(0).getLongitudePoi();
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitudeZoom, longitudeZoom), 14));

                mMap.setMyLocationEnabled(true);
            } catch (NullPointerException e) {
                Context context = getActivity().getApplicationContext();
                CharSequence text = getResources().getString(R.string.errormap);
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context, text, duration);
                toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
                toast.show();
            }
        }
ben
  • 161
  • 3
  • 18

1 Answers1

4

You misunderstood what's a geodesic line. Now to display route between 2 specific points using the roadways, you will need to use the Directions API to get the points, which can then be constructed into a Polyline.

This answer provides a nice way to parse the results of Directions API output. It's Pretty much just parsing the XML and then connecting the points in the PolylineOptions object.

Note: the default daily limit for Directions API is 2,500 queries.

Community
  • 1
  • 1
Simas
  • 43,548
  • 10
  • 88
  • 116