this code is remove with click infoWindow
// Setting click event handler for InfoWIndow
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// Remove the marker
marker.remove();
}
});
but, How remove a single marker by Id without click infoWindow ? i will remove by Button View
Whenever you add the marker to map don't forget to keep its record, like adding it to Map or ArrayList.
your_google_map_obj.addMarker(new MarkerOptions()) //this adds Marker on Google Map, you
should know it always returns Marker object so that you can use it later especially for
removal
so Marker marker=your_google_map_obj.addMarker(new MarkerOptions()) add this marker object to list or map markerArraylist.add(marker); then easily you can extract marker from list by Marker marker=markerArraylist.get(index); and then call marker.remove();
Other way to do it
After adding the marker it is possible to obtain its reference:
Marker marker = map.addMarker(..);
Marker class has remove method, check this documentation
Related
I am having trouble implementing OnClickListener for Marker(s) which are not in Cluster, i.e. not added using:
mClusterManager.addItem(markerCluster);
I have set OnMarkerClickLister as follows:
mMap.setOnCameraIdleListener(mClusterManager);
mMap.setOnMarkerClickListener(mClusterManager);
If I just use:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
return false;
}
});
the click on markers is not working at all.
For example: I have this situation:
Two green dots and big blue dot (with number 6) are one Cluster, but the red marker is not in Cluster as I don't want it to be Clustered like other markers.
InfoWindow is normally showing when I click on red marker, but onMarkerClick doesn't work - as well as OnClusterItemClickListener. But OnClusterItemClickListener works when I click on markers which are in Cluster.
Hope you understand what I am trying to achieve. If not, please let me know.
Updated answer
The image that you added in your edit explains the issue clearly, thanks for that!
In order too solve this issue we need to register the listener differently. Namely: by registering it to the ClusterManager's MarkerManager as that class handles everything of the markers now. We also need to add the NormalMarkers a bit differently, so lets go through it step by step:
1) Update the OnMarkerClickListener of the mMap:
mMap.setOnMarkerClickListener(mClusterManager.getMarkerManager()); // Note the `MarkerManager` here
2) This MarkerManager holds all the collections. We need to create a new collection on this manager to which we will add the NormalMarkers that should be displayed apart from the clusters:
MarkerManager.Collection normalMarkersCollection = mClusterManager.getMarkerManager().newCollection();
3) Adding the markers is done similar to how this was before, but with using the addMarker() method on the collection that we created. We must pass a MarkerOptions() object to this:
// Create a normal marker
val markerOptions = MarkerOptions()
.position(new LatLng(...))
.title("My marker")
.snippet("With description")
// Add it to the collection
normalMarkersCollection.addMarker(markerOptions)
4) Last but not least: we want the OnClickListener on it:
normalMarkersCollection.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener {
public boolean onMarkerClick(marker: Marker) {
// NORMAL MARKER CLICKED!
return false;
}
})
5) Done! Now the normal markers are added to the map just like before, but with a working OnMarkerClickListener.
Earlier answer
(Setting the click listeners for the clusters and clustered items)
You should add the clicklistener to the mClusterManager. Setting the clicklistener on the mMap does not work when using the ClusterManager.
Thus instead of using mMap.setOnMarkerClickListener, set the ClusterItemClickListener on the cluster manager:
mClusterManager.setOnClusterItemClickListener(new ClusterManager.OnClusterItemClickListener<MyItem>() {
#Override
public boolean onClusterItemClick(MyItem item) {
Log.d("cluster item","clicked");
return true;
}
});
Additionally, if you want to capture the onclick of the clustered item item, use the ClusterClickListener:
mClusterManager.setOnClusterClickListener(new ClusterManager.OnClusterClickListener<MyItem>() {
#Override
public boolean onClusterClick(Cluster<MyItem> cluster) {
Log.d("cluster","clicked");
return true;
}
});
If you want to have both Marker and Cluster listeners working you can write
mGoogleMap.getMarkerManager().onMarkerClick(marker);
inside your OnMarkerClickListener
I want to open a new Activity about which clicked Marker. But i can't. Because markers are adding automatically and i am getting coordinate from the database.
How do I know which marker clicked??
for(int i=0; i<all.size();i=i+4){
// her kaydin id,lat,lng,title bilgisi alinip ArrayListe atiliyor..
Double latDouble = Double.parseDouble(all.get(i+1));
Double lngDouble = Double.parseDouble(all.get(i+2));
map.addMarker(new MarkerOptions().position(new LatLng(latDouble, lngDouble)).snippet(all.get(i).toString()).title(all.get(i+3)));
You can use OnMarkerClickListener, but if you don't want a listener in each marker and the info is in the marker, you can use OnInfoWindowClickListener like this:
getMap().setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
public void onInfoWindowClick(Marker marker) {
// here you have the Marker to start the activity, for example:
String url = marker.getTitle();
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
Add "ID" to each marker added.
Then, implement OnMarkerClickListener. As given in documentaion:
"You can use an OnMarkerClickListener to listen for click events on
the marker. To set this listener on the map, call
GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user
clicks on a marker, onMarkerClick(Marker) will be called and the
marker will be passed through as an argument."
You will receive a "Marker" object in OnMarkerClickListener. Compare the "ID"s .
If you know order of your data, you can get the order of marker and match them.
I mean, your first line of database represents the marker which id is = 0.
You can get Id of a marker like this.
int order = Integer.parseInt(marker.getId().substring(1));
googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
int order = Integer.parseInt(marker.getId().substring(1));
}
});
I have an activity with a Google Map and some markers placed on the map. Additionaly, i have some TextViews that shows info from the first selected marker on the map.
I want to be able to access information from any selected marker from my map. I want my information from my Textviews to change when I click another marker. Can you tell me what method should I call or what should I do to be able to do that?
Thank you.
Check the docs
and to get the marker title and marker snippet check these links
getTitle ()
getSnippet ()
Add the marker as follows
myMarker = getMap().addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!"));
then, set the TextView using getTitle() or getSnippet() as follows
tv.setText(myMarker.getTitle());
or
tv.setText(myMarker.getSnippet());
and
to change the text of the TextView each time you click the marker detect the clicks with an onClickListener(). May be like this..
map.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(marker.isInfoWindowShown()) {
marker.hideInfoWindow();
} else {
marker.showInfoWindow();
}
tv.setText(myMarker.getTitle()); //Change TextView text here like this
return true;
}
});
UPDATE: I have solved the performance issue by adding a previousMarker object. So only the previous clicked marker will be remove and replaced with the default icon. However the info window is still not shown when I click on the marker.
I have a map view and set some markers on it. What I want is when I clicked on a marker, it changes its icon to be a different icon, and when I click on another marker, the previous marker's icon should change to its original one.
What I've done is something like this but it just simply changes the marker icon whenever I click on the marker.
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
LatLng markerPos=marker.getPosition();
String markerLocationName=marker.getTitle();
String markerSubCategoryName=marker.getSnippet();
marker.remove();
MarkerOptions markerOptions =
new MarkerOptions().position(markerPos)
.title(markerLocationName)
.snippet(markerSubCategoryName)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
mMap.addMarker(markerOptions);
Log.d("marker","change marker icon"); // can open a dialog window here
return false;
}
So if I click 2 markers, I will get 2 new icons appears, meanwhile what I want is only the current clicked marker changes its icon.
So I've also done something like this by adding 2 more lines of code. It succeeds doing what I want but it has some drawback (see below).
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
mMap.clear();
populateAllMarkersOnMap();//repopulate markers on map
LatLng markerPos=marker.getPosition();
String markerLocationName=marker.getTitle();
String markerSubCategoryName=marker.getSnippet();
marker.remove(); //remove the current clicked marker
MarkerOptions markerOptions =
new MarkerOptions().position(markerPos)
.title(markerLocationName)
.snippet(markerSubCategoryName)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.new_icon));// Changing marker icon
mMap.addMarker(markerOptions); //add marker with new icon into map
return false;
}
The drawback is 1/ it "disable" the info window (the same thing also happen in the first way). 2/ it clear all the markers on map and set all the markers again. Imagine I have 100 markers, should that be a performance problem on every click I do?
The populateAllMarkersOnMap() can be something as simple like this at the moment:
private void populateAllMarkersOnMap(){
setMarker(latA1, lonA1, "A1","A1.1");
setMarker(latA2, lonA2, "A2","A2.1");
// ... (100 times or populated via a loop)
};
So is there a way to get previous clicked marker to change its icon back to default when I click a new marker? Apologise for my English, if you think I should put another title for my question, please help.
Finally I found the best and most simple way. I made a previousMarker object and store the current clicked marker:
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
if(previousMarker!=null){
previousMarker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
}
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
previousMarker=marker; //Now the clicked marker becomes previousMarker
return false;
}
You might be looking for this method probably
Called when the marker's info window is closed.
optional public func mapView(mapView: GMSMapView, didCloseInfoWindowOfMarker marker: GMSMarker)
I found the best and most simple way. I made another marker object and store the current clicked marker enter code here
#Override
public boolean onMarkerClick(Marker marker) { //Called when a marker has been clicked or tapped.
if(previousMarker!=null){
marker2.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.dot_icon));
}
marker.setIcon(BitmapDescriptorFactory.fromResource(R.drawable.ct_icon));
marker2=marker; //Now the clicked marker becomes previousMarker
return false;
}
I set up a marker an set it's draggable state to true. But when I call marker.getPosition() I am always getting the same location, like marker's position is not updated after drag end.
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map_place))
.getMap();
LatLng MYLOCATION = new LatLng(Double.valueOf(myLat), Double.valueOf(myLng));
marker = new MarkerOptions()
.position(MYLOCATION)
.title(getString(R.string.map_title_my_location)).draggable(true);
mMap.addMarker(marker).showInfoWindow();
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(MYLOCATION, 18));
close.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LatLng pos = marker.getPosition();
Toast.makeText(activity, "Lat: " + pos.latitude + "; Long: " + pos.longitude, Toast.LENGTH_LONG).show();
}
});
as class vars I defined:
GoogleMap mMap;
MarkerOptions marker;
Any idea?!
This is an old question but since I had the same problem myself, here's the solution: you need to set the OnMarkerDragListener for the map instance first (mMap.setOnMarkerDragListener(...)) for the markers position value to get updated. You don't really need to do anything inside the listener implementation methods (you can use marker.getPosition() anywhere you like), they just have to exist.
UPDATE
This is also documented now in https://developers.google.com/maps/documentation/android-api/marker#marker_drag_events
You can use an OnMarkerDragListener to listen for drag events on a marker. To set this listener on the map, call GoogleMap.setOnMarkerDragListener. To drag a marker, a user must long press on the marker. When the user takes their finger off the screen, the marker will stay in that position. When a marker is dragged, onMarkerDragStart(Marker) is called initially. While the marker is being dragged, onMarkerDrag(Marker) is called constantly. At the end of the drag onMarkerDragEnd(Marker) is called. You can get the position of the marker at any time by calling Marker.getPosition().
You should implement OnMarkerDragListener() in your main activity. And inside the method onMarkerDragEnd() you can get the position of your marker (by using its ID)