Draw routes in Google Maps from Android, taking the lat / lng where the polyline should go


I need to draw routes in Google Map from Android, having the latitude and longitude (lat / lng) where the polyline should go. The app I do captures the position where a person is traveling with his cell phone, and then that route must be drawn on the map. What happens is that the GPS is not so precise, and when drawing a polyline between each lat / lng that is captured, there is not a straight line in the streets, but they are humpbacked. I do not know if there is a library or something that can be used to make the route well drawn.

asked by Daniel Piad Aldazabal 20.06.2016 в 16:21

3 answers


Here you can see how to set up your project to support Google Maps get a API key and from the official documentation of the class Polilyne and the creation of forms , you can basically see how to draw a Polyline in the following way . Remember that the Polyline must be modified from the main thread or a IllegalStateException will be released at run time:

 GoogleMap map;
 // ... get a map.
 // Add a thin red line from London to New York.
 Polyline line = map.addPolyline(new PolylineOptions()
     .add(new LatLng(51.5, -0.1), new LatLng(40.7, -74.0))

A complete example :

public class PolylineDemoActivity extends AppCompatActivity
        implements OnMapReadyCallback {

    protected void onCreate(Bundle savedInstanceState) {...}

    public void onMapReady(GoogleMap map) {
        // Override the default content description on the view, for accessibility mode.
        // Ideally this string would be localised.
        map.setContentDescription("Google Map with polylines.");

        // A simple polyline with the default options from Melbourne-Adelaide-Perth.
        map.addPolyline((new PolylineOptions())
                .add(MELBOURNE, ADELAIDE, PERTH));

        // A geodesic polyline that goes around the world.
        mClickablePolyline = map.addPolyline((new PolylineOptions())
                .add(LHR, AKL, LAX, JFK, LHR)

        // Rectangle centered at Sydney.  This polyline will be mutable.
        int radius = 5;
        PolylineOptions options = new PolylineOptions()
                .add(new LatLng(SYDNEY.latitude + radius, SYDNEY.longitude + radius))
                .add(new LatLng(SYDNEY.latitude + radius, SYDNEY.longitude - radius))
                .add(new LatLng(SYDNEY.latitude - radius, SYDNEY.longitude - radius))
                .add(new LatLng(SYDNEY.latitude - radius, SYDNEY.longitude + radius))
                .add(new LatLng(SYDNEY.latitude + radius, SYDNEY.longitude + radius));

        // Move the map so that it is centered on the mutable polyline.

Note: I leave links to the official documentation because Google has a bad (but necessary) habit of changing certain configuration procedures from time to time.

I hope it helps. greetings

answered by 20.06.2016 в 20:32

Try this:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    intent.addCategory(Intent.CATEGORY_LAUNCHER );     
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");

Maybe this question will help you solve the problem: link

answered by 30.11.2017 в 10:27

Maybe you can save the checkpoints in temporary variables, and have them show a polyline that has been calculated as a car route between both checkpoints. So you force the polyline to respect the original drawing of the streets, which does not happen in pedestrian mode or in free mode as it is happening to you. Lucky mate

answered by 06.08.2018 в 19:01