How to draw path as I move starting from my current location using Google Maps
I know it's pretty late for an answer, but I'll post this here for those still have the same problem.
If you want to move a marker or/and a Polyline, you don't need to remove/clear and draw again, instead you have to add PolylineOptions once and than set the new points afterwards.
Example Code:
boolean isPolyAdded = false;
Add this line of the code:
if (isPolyLoaded == false){
polyline = map.addPolyline(lineOptions);
isPolyLoaded = true;
}else{
polyline.setPoints(points);
}
to the and of onPostExecute
method
Final Code:
@Override
protected void onPostExecute(List<List<HashMap<String, String>>> result) {
// Traversing through all the routes
for(int i=0;i<result.size();i++){
points = new ArrayList<LatLng>();
lineOptions = new PolylineOptions();
// Fetching i-th route
List<HashMap<String, String>> path = result.get(i);
// Fetching all the points in i-th route
for(int j=0;j<path.size();j++){
HashMap<String,String> point = path.get(j);
double lat = Double.parseDouble(point.get("lat"));
double lng = Double.parseDouble(point.get("lng"));
LatLng position = new LatLng(lat, lng);
points.add(position);
}
// Adding all the points in the route to LineOptions
lineOptions.addAll(points);
lineOptions.width(7);
lineOptions.color(Color.BLACK);
}
// Drawing polyline in the Google Map for the i-th route
if (isPolyLoaded == false){
polyline = map.addPolyline(lineOptions);
isPolyLoaded = true;
}else{
polyline.setPoints(points);
}
}
}
It seems that the best implementation would be to just use an ArrayList<LatLng>
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<LatLng> points; //added
Polyline line; //added
Initialize points
in onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
points = new ArrayList<LatLng>(); //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.