I'm currently having a map, and each 10 meters I use LocationListener to refresh my location and get the new Latitude and Longitude. Now I wish that the route the user is taking will be displayed with a red line. So everytime the OnLocationChange() from LocationListener class is called, I want to update the map with a line between the last location and the new location.
So far I've added the following:
private void initializeDraw() {
lineOptions = new PolylineOptions().width(5).color(Color.RED);
lineRoute = workoutMap.addPolyline(lineOptions);
}
during the OnLocationChanged I call this:
drawTrail();
now what should I insert into this function so that each time it adds the newly attained location as a point and draws a line from the last to the new point.
Thanks
A polyline is a list of points, where line segments are drawn between consecutive points. A polyline has the following properties: Methods in this class must be called on the Android UI thread. If not, an IllegalStateException will be thrown at runtime.
Call GoogleMap.addPolyline () to add the polyline to the map. Set the polyline's clickable option to true if you want to handle click events on the polyline. There's more about event handling later in this tutorial.
You've built an Android app containing a Google map, with polygons to represent areas on the map and polylines to represent routes or other connections between locations. You've also learned how to use the Maps SDK for Android . Learn about the Circle object. Circles are similar to polygons but have properties that reflect the shape of a circle.
A Polyline is a series of connected line segments. Polylines are useful to represent routes, paths, or other connections between locations on the map. Create a PolylineOptions object and add points to it. Each point represents a location on the map, which you define with a LatLng object containing latitude and longitude values.
First translate Location
into LatLng
:
LatLng newPoint = new LatLng(location.getLatitude(), location.getLongitude());
Then add a point to existing list of points:
List<LatLng> points = lineRoute.getPoints();
points.add(newPoint);
lineRoute.setPoints(points);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With