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.
Related
I have a problem with using Google maps API on Android. I have a button which removes the selected marker off the maps interface, and information about it from the SQLite db that I have set up. Although my only problem is once multiple markers are on the map this feature stops and doesn't remove them:
Below shows the method which removes the marker from the maps and replaces them. As I said this works perfectly fine with a single marker and my testing has been a success, but with multiple it does not work.
I have an onclicklisterner for the markers which displays the information and a popup box for the marker, for the user to remove the marker they must click the marker which sets the global variable to that object then once the remove button is pressed the removeMarker() method is called. The getAllMarkers() method loops through a SQLite db and pulls information and adds to the maps.
End problem: Removing a marker when multiple markers are placed on the map doesn't work. Only works when a single marker is placed on the map.
Marker lastOpened = null;
To remove the information from the db the condition in the if statement returns a boolean value if it has been done:
if(this.mDbHelper.deleteLine(lastOpened.getTitle()))
Remove method
public void removeMarker(){
if(this.lastOpened != null){
if(this.mDbHelper.deleteLine(lastOpened.getTitle())){
lastOpened.remove();
getAllMarkers();
}
}
}
Thanks
You can either use googleMap.clear() or you can store your Markers in a collection of kind and remove them in a loop:
private ArrayList<Marker> mMarkers;
...
private void removeMarkers() {
for (Marker marker: mMarkers) {
marker.remove();
}
mMarkers.clear();
}
Here's a related ticket discuss how to remove marker: Remove a marker from a GoogleMap
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.
I'm trying to implement app in which you can add your own marker which will be given a shutdown time. To do so I need to know how to manage my markers. Let's say I have array list of users with assigned unique ID.
private ArrayList<User> userList = new ArrayList<>();
Then I will create array list of markers which will contain information like Latitude, Longitude, Title, owner's ID and deadline.
private ArrayList<MyMarker> mMarkersArray = new ArrayList<MyMarker>();
Next whenever user will activate add marker method, new marker will be pushed to my list of markers. Ideologically everything seems nice and easy, furthermore creating new object looks like this:
Marker mMarker = mMap.addMarker(new MarkerOptions() (...) );
but when it comes to managing specific markers it seems like I'm missing something. There will be some trigger method which will check the deadline of all markers (or rather first one on the 'sorted by deadline' list) and then it should remove (not hide, because I think it would be inefficient from the memory point of view). How to achieve this? I can't add some custom variable like ID to markers (so I could then find the one I'm interested in) and I'm a bit lost.
There is a way to achieve this by clearing whole map and then rendering again all markers except the inactive, but as far as I'm concerned it's very inefficient and there has to be better solution.
If you want to remove a specific marker from the map, you can just call the remove(). method.
Sample code to remove a marker from MapView and HashMap:
for (Marker marker : hm.keySet()) {
if (hm.get(marker).equals(deadline)) {
marker.remove();
hm.remove(marker);
}
}
You dont need to clear the entire MapView, if you just call remove() method on specific marker.
I am new to android coding. I am trying to toggle on and off markers that I managed to display on my map, with a button in my action bar.
So far I have created this method, I do not understand what I need to do next
Here, basically I make an array of locations and use for loop to put all the markers on my map. Now what I want to be able to do is hide the markers if they are visible via a button click and show the markers if they are hidden.
public boolean showShops(){
rL = new ArrayList<LatLng>();
rl.add(new LatLng(40.433433, -1.422423));
rl.add(new LatLng(40.433434, -1.422534));
for(LatLng nRL : rL){
mMap.addMarker(new MarkerOptions()
.position(nRL)
.title("Shop")
}
return true;
}
I have been trying to figure it out for a long time now and cannot seem to find the solution. I have managed to find out that you have to setVisible(false); to hide and setVisible(true); to show, but I do not know how I can implement it. I tried adding that instead of .add in my above code but I get errors.
Can someone please help.
Thanks.
If there's nothing else on your map that you DON'T want to hide, use
clear on your GoogleMap object which removes all additional overlays of your map.
If that method doesn't fit you, you have to keep a reference to all markers (for example in an ArrayList) and call remove or setVisible() on each of them individually:
Keep an
ArrayList<Marker> myMarkers = new ArrayList<Marker>();
and also add every marker that you add to the map to that list.
for(LatLng nRL : rL){
myMarkers.add(mMap.addMarker(new MarkerOptions()
.position(nRL)
.title("Shop"));
}
If you want to set them all to invisible, iterate over that list and setVisible(false) on all of them.
for (Marker m : myMarkers) {
m.setVisible(false);
}
If you want to make your marker invisible you can also use marker.setAlpha(0).
I add a marker on each map click, and I only want to see the last one.
I tried the following code:
if(marker!=null)
{
marker.visible(false);
marker=null;
}
marker=new MarkerOptions().position(latLng);
googleMap.addMarker(marker);
I see every marker even though I set it to invisible.
How can I remove after each click? I can't find any remove methods.
Just keep a reference to the marker each time you do the click :
Marker marker = map.addMarker(MARKER_OPTIONS);
Then call remove() method :
marker.remove();
As mention here in documentation , Remove() method in Marker class will help you
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Marker#remove()
and here tutorial about adding and removing markers on map
http://www.jiahaoliuliu.com/2013/08/android-adding-and-removing-markets-on.html