How to draw path as I move starting from my current location using Google Maps

后端 未结 3 1466
不思量自难忘°
不思量自难忘° 2020-12-12 14:20

I am trying to draw route as I move from my current location. I am facing a big problem in drawing route dynamically please help me to solve it. I am having marker at my cur

3条回答
  •  眼角桃花
    2020-12-12 14:32

    It seems that the best implementation would be to just use an ArrayList to store each point given in onLocationChanged(). Then, each time you get a new point, re-draw the line.

    First, import what you need for drawing the lines:

    import com.google.android.gms.maps.model.Polyline;
    import com.google.android.gms.maps.model.PolylineOptions;
    

    Create member variables for the ArrayList and the Polyline:

    private ArrayList points; //added
    Polyline line; //added
    

    Initialize points in onCreate():

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        points = new ArrayList(); //added
        //...............
    

    Then, in onLocationChanged(), add each point you get to the ArrayList:

    @Override
    public void onLocationChanged(Location location) {
          double latitude = location.getLatitude();
          double longitude = location.getLongitude();
          LatLng latLng = new LatLng(latitude, longitude); //you already have this
    
          points.add(latLng); //added
    
          redrawLine(); //added
    
    }
    

    Taking from this answer, define your redrawLine() method.
    Remove all other calls to addMarker(), since you will be calling clear() on your map, which removes all Markers and Polylines.

    private void redrawLine(){
    
        googleMap.clear();  //clears all Markers and Polylines
    
        PolylineOptions options = new PolylineOptions().width(5).color(Color.BLUE).geodesic(true);
        for (int i = 0; i < points.size(); i++) {
            LatLng point = points.get(i);
            options.add(point);
        }
        addMarker(); //add Marker in current position
        line = googleMap.addPolyline(options); //add Polyline
    }
    

    Edit: You will also probably want to dial in the minimum distance in meters between location changed callbacks.

    private static final String TAG = "MainActivity";
    private static final long INTERVAL = 1000 * 60 * 1; //1 minute
    private static final long FASTEST_INTERVAL = 1000 * 60 * 1; // 1 minute
    private static final float SMALLEST_DISPLACEMENT = 0.25F; //quarter of a meter
    

    Call setSmallestDisplacement():

    protected void createLocationRequest() {
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(INTERVAL);
        mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
        mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); //added
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    }
    

    That should be enough to get you started. You may need to fine-tune the frequency of location changed callbacks to get your desired result. There's probably more to it than that, but you can find the edge cases and fix them after testing.

提交回复
热议问题