How to erase PolyLine in Google Map..? - android

First, i'm sorry about my English.
I created the app for detecting walking and tracking step. Tracking step is good. But problem is that the app redetecting steps, Doesn't erase before polyline.
I try to map.clear(), polyline.remove()... etc. But Doesn't activate.
this is my source for draw polyline
polyline = map.addPolyline(polylineOptions.add(new LatLng(location.getLatitude(), location.getLongitude())));
polylineOptions.width(5);
polylineOptions.color(Color.RED);
map.addPolyline(polylineOptions);
this places onLocationListener in LocationListener.
location is onLocationListener's parameter.
How to remove previous polylines...?
Im sorry about my English. Please Help me.

To remove polyline.
//Adds the line to map
Polyline line = map.addPolyline(polylineOptions);
//removes the same line from map
line.remove();

As mentioned in the Shapes documentation specifically on Remove a Polyline, call the setMap() method passing null as the argument to remove a polyline from the map.
How to do it is also shown in Remove a Polyline sample.

Related

Can I instantiate a Polyline without a GoogleMap?

I have an ArrayList<LatLng> that I need to submit to an API as an encoded Polyline. What I've used before is something like:
PolylineOptions pathOptions = new PolylineOptions();
Polyline polyline = googleMap.addPolyline(pathOptions);
polyline.setPoints(getRouteLatLngs());
except that in the case where I'm uploading, I don't have a GoogleMap to create the Polyline for me. Is it possible to create a Polyline without a GoogleMap or do I need to make another plan?
You don't want the Polyline object from GoogleMap. You need a tool to encode a list a points like PolyUtil, available in googlemaps utils library
PolyUtil.encode(latLngPath)
PolyUtil.decode(encodedPath)

How to remove the Poly line from the Google Map v2 android

I am successfully making and drawing poly lines on a marker click event. But there is a thing happening which I do not want to be happen.
And that is when I click on marker Let say JackiMarker it draws route to the target location but then I click to second marker let say MichaelMarker it also draws the route but the previous marker is still there which I do not want to be.
I am sing this code to draw and remove the marker. It has the clearRoute function but it is not working for me , please help me what to do . Please help me in removing the last polyline I have drawn on map.
Please try this,
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.
It is 100 % working code which I shared in the question , the problem was in my end .
what I was doing , was trying to delete the path when clicked on the marker and on the OnMarkerClickListener I was initiating the Route Class , which created another instance of the same class thus creating the array of polylines from 0 index which Is why i was not getting into the for loop of the clearRoute method.
so by instantiating the Route class as a global solved my problem.

Polyline not visible Android Maps Api v2

I am a little bit confused since everything seems to be alright with the code.
The problem is that the polyline won't show up on the map.
Here is the function that I call to place polyline everytime I receive a location
(I added markers in a similar way and they work great)
private void addPolylineLocationOnMap(LatLng newLoc)
{
PolylineOptions poly = new PolylineOptions()
.add(newLoc)
.color(Color.BLUE)
.width(5)
.visible(true)
.zIndex(30);
googleMap.addPolyline(poly);
}
A Polyline needs multiple points!
For example, pass an ArrayList<LatLng> to your method and use addAll() rather than just add().
From the PolylineOptions documentation:
add(LatLng... points) : Adds vertices to the end of the polyline being built.
Alternatively, you can keep a reference to one Polyline and use add() to add points to it as you receive them.
Add poly as an instance variable in your class:
PolylineOptions poly;
Then in onCreate() (or wherever you set up the map):
poly = new PolylineOptions()
.color(Color.BLUE)
.width(5)
.visible(true)
.zIndex(30);
googleMap.addPolyline(poly);
Then as you receive more points:
poly.add(newLoc);

Google maps API v2 find Marker by id

You can simply add Markers with the new V2 Google maps API on Android. Because the Markers are recreated on configuration change or on save instance, you have to reference them via its ids. The question is how to remove from map Marker with particular id?
My use case is to add Markers to the map, store its id's with mapping to real objects. Then user removes one of this real objects, so I find the Marker id and want to remove Marker from the map and the only way I know about is to have Marker object and call remove() on it.
The documentation is wrong about recreating markers on configuration change and it's actually good for us it is wrong there.
I have also came across similar situation in doing map clustering, where i need to remove the marker when it is added to an cluster.
The solution which i used is that, i am holding the reference to the markers when they are being created and added into the map and store the marker in a Map (String - Marker) ,where key(String) would be an auto-generated marker id, and value would be the marker object.
Now,you can get the reference to the marker object by its id and call remove() on that marker.
I hope this will be helpful to you.
SImply try the following:
private Marker myMarker;
myMarker = getMap().addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!"));
now the marker you want to remove you can call the
myMarker.remove();

How to draw a dynamic line (route) with Google Maps Android API v2

I'm wondering what the best practice is to draw a dynamic route on a map with the Google Maps API v2. I want to have a map that's able to prolong the route while the user is moving. There seems to be the obvious solution by using a Polyline and PolylineOptions. But I just can't find an easy way to add points after I instantiated the Polyline. To draw a Polyline is something like this:
PolylineOptions polylineOptions = new PolylineOptions();
polylineOptions.add(POINT1, POINT2, POINT3);
Polyline line = googleMap.addPolyline(polylineOptions);
But after I passed the line to GoogleMap I can't add any new points to it. Something like
polylineOptions.add(POINT1, POINT2, POINT3);
doesn't add anything to my route.
I could just add complete new Polyline. But isn't there a way to prolong just the existing one? I figured out a way by getting all the points of the Polyline, add the new point, and write them back to the line:
List<LatLng> points = line.getPoints();
points.add(POINT4);
line.setPoints(points);
But it seems to be cumbersome to me. Any ideas?
In the mainActivity class, define a private static LatLng variable named prev and initialize it to (0,0) first. Also make a flag variable and assign 0 to it. In the Listener's OnLocationChanged method, create a local variable LatLng named current and get current co-ordinates here... check the value of flag first, if it is 0 then assign current to prev. Then add a polyline.
Assign current to prev again (this will happen every time as after the first time, the flag will be 1)
For example:
public void onLocationChanged(Location location)
{
LatLng current = new LatLng(location.getLatitude(), location.getLongitude());
if(flag==0) //when the first update comes, we have no previous points,hence this
{
prev=current;
flag=1;
}
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(current, 16);
map.animateCamera(update);
map.addPolyline((new PolylineOptions())
.add(prev, current).width(6).color(Color.BLUE)
.visible(true));
prev=current;
current = null;
}
Something like this. Of course performance improvements can be made, this is just an example code. But it should work. Every time the polyline will add only the previous and current point, thereby extending it point by point.
Looking at the documentation, it appears that polylineOptions.add(LatLng) and googleMap.addPolyline(polylineOptions) methods return the polylineOptions object. The first method will also return polylineOptions WITH the point added to the end.
I think you'll have to add the polylineOptions to googleMap.addPolyline(polylineOptions) again or use googleMap.clear() before adding it a second time. Somehting like this:
polylineOptions = googleMap.addPolyline(polylineOptions);
// googleMap.clear();
googleMap.addPolyline(polylineOptions.add(point));

Categories

Resources