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

7

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
source

3 answers

4

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))
     .width(5)
     .color(Color.RED));

A complete example :

public class PolylineDemoActivity extends AppCompatActivity
        implements OnMapReadyCallback {

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

     @Override
    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)
                .width(5)
                .color(Color.BLUE)
                .geodesic(true)
                .clickable(mClickabilityCheckbox.isChecked()));

        // 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.
        map.moveCamera(CameraUpdateFactory.newLatLng(SYDNEY));
    }

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
0

Try this:

Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
    Uri.parse("http://maps.google.com/maps?saddr="+latitude_cur+","+longitude_cur+"&daddr="+latitude+","+longitude));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addCategory(Intent.CATEGORY_LAUNCHER );     
    intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
    startActivity(intent);

Maybe this question will help you solve the problem: link

    
answered by 30.11.2017 в 10:27
0

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