In my android app, I want to create a single path/polyline from a specific location(far end) to my current location while moving. I could draw the polyline on map with now issue while when I moves to a new location another polyline is been drown on the map, so my map output is full of polylines whichh I don't need, I want only one polyline to be visible to my current location.
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
Polyline polyline = googleMap.addPolyline(polylineOptions);
Remove old Line before adding new one.
Polyline polyline ;
public void addUpdatePolyLine()
{
PolylineOptions polylineOptions = new PolylineOptions().add(currentPosition).add(farEndPosition).width(10).color(Color.GREEN);
if(polyline !=null)
{
polyline.remove();
}
polyline = googleMap.addPolyline(polylineOptions);
}
Related
We are drawing polyline according to different zone colors (Different colors assigned to the different zone where the person is in) and the below issue, we are facing constantly. Is anyone know the solution?
Getting issue on line no 8: polyline.setPoints(points) where points contain array of lat/lng. eg:lat/lng: (34.282619,-119.149895) AlexMamo
PolylineOptions options = new PolylineOptions();
Polyline polyline = null;
for (int i = 1; i < coloredPointArrayLit.size(); i++) {
if (coloredPointArrayLit.get(i).color == coloredPointArrayLit.get(i - 1).color) {
if (polyline != null) {
List<LatLng> points = polyline.getPoints();
points.add(coloredPointArrayLit.get(i).coordinates);
polyline.setPoints(points);
} else {
options.add(coloredPointArrayLit.get(i - 1).coordinates);
options.add(coloredPointArrayLit.get(i).coordinates);
options.color(coloredPointArrayLit.get(i).color);
options.jointType(JointType.ROUND);
options.endCap(roundCap);
options.startCap(roundCap);
// Add the polyline to the map colored polyline
polyline = this.googleMap.addPolyline(options
.width(DensityTool.adjustToDensity(context, 7)));
}
} else {
options = new PolylineOptions();
options.add(coloredPointArrayLit.get(i - 1).coordinates);
options.add(coloredPointArrayLit.get(i).coordinates);
polyline = null;
}
}
Any help will be appreciated.
You are using new PolyLineOptions instance for each and every line you draw to the maps. This is make the drawing slow.
Only use one instance of polyline options and use only the .add(LatLng) function inside the loops.
Please refer this link: https://stackoverflow.com/a/39616306/4896829
I am using google maps and drawing polylines on it by passing array of coordinates on each location update. After plotting the polyline i first remove the previous one and then i am plotting new polyline based on updated coordinates array list. Problem is, google maps is plotting a separate polyline from starting to end point on each location update.
I know mistake is somewhere in the hierarachy of calling things. Can anybody help me figuring this out.
This is the method in which i am updating the camera focus and plotting polylines.
private void moveCamera(Location location) {
if (location != null) {
if (polyline != null) {
polyline.remove();
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
coordList.add(new LatLng(location.getLatitude(), location.getLongitude()));
// Create polyline options with existing LatLng ArrayList
polylineOptions.addAll(coordList);
polylineOptions
.width(5)
.color(Color.RED);
// Adding multiple points in map using polyline and arraylist
polyline = mMap.addPolyline(polylineOptions);
}
}
just call
polylineOptions.clear(); -> delete current object
and
map.clear() -> invalidate map
before add new polyline
Got the Solution. I was using the same old PolylineOptions again and again by declaring it as global variable. Now before each plot i create a new instance of PolylineOptions and now good to go.
Here is the updated code.
private void moveCamera(Location location) {
if (location != null) {
if (polyline != null) {
polyline.remove();
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLongitude()), 16));
coordList.add(new LatLng(location.getLatitude(), location.getLongitude()));
polyline = mMap.addPolyline(new PolylineOptions().addAll(coordList)
.width(5)
.color(Color.RED));
}
}
I have searched in google and find this answer where i can remove all polyLine of a Map.
But I want to remove only a particular line from the poly Line. For Example I want to remove the line from 2nd to 3rd LatLng in the given code. I want to change color of a particular line of a polyLine or make it transparent. And I also want to add a clickListener to PolyLine
PolylineOptions rectOptions = new PolylineOptions()
.add(new LatLng(37.35, -122.0))
.add(new LatLng(37.45, -122.0)) // North of the previous point, but at the same longitude
.add(new LatLng(37.45, -122.2)) // Same latitude, and 30km to the west
.add(new LatLng(37.35, -122.2)) // Same longitude, and 16km to the south
.add(new LatLng(37.35, -122.0)).width(5).color(Color.RED);; // Closes the polyline.
Polyline polyline = myMap.addPolyline(rectOptions);
Main goal is to remove/make it transparent a particular line of a PolyLine on click or tap.
PolylineOptions line= new PolylineOptions().add(HAMBURG,// these are latlong
KIEL,
KIEL2,
KIEL3
new LatLng(40.748963847316034,
-73.96807193756104)
)
.width(5).color(Color.RED);
Polyline polyline= googleMap.addPolyline(line);
And i want to remov line between KIEL1 and KIEL2
You'll have to manually remove points from the polyline.
EDIT:
Step by step:
Create a List of polylines:
List<Polyline> mPolylines = new ArrayList<Polyline>();
Add PolylineOptions to the map:
Polyline polyline1 = myMap.addPolyline(rectOptions1);
Polyline polyline2 = myMap.addPolyline(rectOptions2);
Polyline polyline3 = myMap.addPolyline(rectOptions3);
Then save the added polylines to your array
mPolylines.add(polyline1);
mPolylines.add(polyline2);
mPolylines.add(polyline3);
Now at any time you can trim the polyline like so:
// Get polyline1
List<LatLng> points = mPolylines.get(0).getPoints();
// Set the bounds of points to remove (inclusive)
int startPoint = 1, endPoint = 2; // will remove kiel1 and kiel2
// Remove the points
for (int i=startPoint; i<=endPoint; i++) {
points.remove(i);
}
// Added this line as getPoints returns a copy
mPolylines.get(0).setPoints(points);
Now in theory this should work fine. I found that the points don't actually change after setPoints.
I even tried:
Polyline polyline = mPolylines.get(0);
// Get copy of the points
List<LatLng> points = polyline.getPoints();
mPolylines.get(0).remove();
mPolylines.remove(0);
for (int i=3000; i<7000; i++) {
points.remove(i);
}
// Create a PolylineOptions object with the new points
PolylineOptions polylineOptions = new PolylineOptions().addAll(points);
mPolylines.add(0, mMap.addPolyline(polylineOptions));
And to my surprise a new Polyline was added (I can tell by the changed stroke width and color), but it still used the old points, even though points.size() returned the correct (trimmed) count.
I'm not sure why this is so, perhaps some error in my code. You can try these methods yourself and see if you are any luckier.
I connected map locations using Polyline API. It creates polyline properly.But sometimes unwanted polyline extended when zoom in and disappears on zoom out.
My code:
locations.clear();
for(int i=0;i<maplatitude.size();i++)
{
locations.add(new LatLng(maplatitude.get(i), maplongitude.get(i)));
}
PolylineOptions rectOptions = new PolylineOptions();
rectOptions.addAll(locations);
// Get back the mutable Polyline
Polyline polyline = mMap.addPolyline(rectOptions);
for(int i=0;i<maplatitude.size();i++)
{
//mMap.addMarker(new MarkerOptions().position(new LatLng(maplatitude.get(i), maplongitude.get(i))).title(name).snippet(mapdate.get(i)+"\n"+maptime.get(i)));
mMap.addMarker(new MarkerOptions().position(new LatLng(maplatitude.get(i), maplongitude.get(i))).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).title(String.valueOf(i)));
}
I solved this: it's a bug from Google. We must filter some close points within 1 or 2 meters.
Polylines appearing on map where they shouldn't
Following
How to draw a path between two markers
I had to add lot of polylines between two markers, to make a path.
One of the markers is draggable, lets say source is draggable.
So, when user starts dragging the marker, the path previously drawn must be erased and a new path between new source and destination must be draw.
I am able to draw the new path, but how can i erase the previous path?
This is how the path is drawn:
for (int z = 0; z < list.size() - 1; z++) {
LatLng src = list.get(z);
LatLng dest = list.get(z + 1);
Polyline line = map.addPolyline(new PolylineOptions()
.add(new LatLng(src.latitude, src.longitude),
new LatLng(dest.latitude, dest.longitude))
.width(2).color(Color.RED).geodesic(true));
}
One solution i can get is
map.clear();
To clear all the polylines, markers etc.. and add the markers again, then drawn the path.
But as soon as I start dragging, the marker is cleared, hence not visible on the map :(
Thank You
Keep track of the Polyline as you add it to the map:
Polyline polyline = this.mMap.addPolyline(new PolylineOptions().....);
Then when you want to remove it:
polyline.remove();
If you have lots of Polylines, just add them to a List as they are put on the map:
List<Polyline> polylines = new ArrayList<Polyline>();
for(....)
{
polylines.add(this.mMap.addPolyline(new PolylineOptions()....));
}
And when you want to delete:
for(Polyline line : polylines)
{
line.remove();
}
polylines.clear();
The key is to keep a reference to the Polyline objects and call .remove() on each one.
I know this is very old question but I noticed that this is very common need. I found another way and I wanted to share it.
Here is the basic idea:
Polyline polylineFinal;
PolylineOptions polylineOptions;
loop {
polylineOptions.add( new LatLng( latitude, longitude ) );
}
polylineOptions.width(2);
polylineOptions.color(Color.RED);
polylineOptions.geodesic(true);
polylineFinal = map.addPolyline (polylineOptions);
Map's "addPolyline" method returns a single polyline which contains all the points. When I need to remove the points, I call polylineFinal's "remove" method.
polylineFinal.remove();
Update
If you want to removes all markers, polylines, polygons, overlays, etc from the map use
mMap.clear(); //it will remove all additional objects from map
and if you only want to remove all or single polyline, polygon, marker etc see #DiskDev Answer above. In this case you must keep track of every single additional object you add to map