7 votes

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 they should go to the polyline. The app that I capture the position where you are going, moving a person with their cell phone, and then you must draw the route on the map. What happens is that the GPS is not as accurate, and drawing a polyline between each lat/lng that you capture, it is not a straight line in the streets, but that are humpback. I don't know if there is some library or something that can be used for the route is well drawn.


HCarrasko Points 850

Here you can see how to configure your project so that it supports Google Maps to get an API key and from the official documentation of the class Polilyne and the creation of forms, basically you can see how to draw a Polyline the following way. Remember that the Polyline must be changed from the main thread or a IllegalStateException will be thrown 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 setup procedures once in a while.

I hope to help you. greetings


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: https://stackoverflow.com/questions/14710744/how-to-draw-road-directions-between-two-geocodes-in-android-google-map-v2


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. Luck mate


HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by: