Android - Set listener marker - android

in my googleMap there are 5 markers.
Is there a method by which I can set a listener just on one of them?
The code below shows how to do this, but only if there is a single marker:
GoogleMap mMap
Marker marker = mMap.addMarker(
new MarkerOptions()
.position(new LatLng(dLat, dLong))
.title("Your title")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_pin)));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker m) {
}
}

When you are adding marker , you can add Tag to each Marker,
like this
marker1 = mMap.addMarker(new MarkerOptions()
.position(yourPosition)
.title("yourTitle");
marker1.setTag("YourTag");
and then you can identify which Marker is clicked by accessing the
Tag value in OnMarkerClickListener.
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker m) {
if(m.getTag()=="YourTag"){
//Perfom your operation here
}else if(m.getTag()=="AnotherTag"){
//Perfom your operation here
}
}

Related

Setting onMarkerClickListener on different markers

I want to set OnMarkerClickListener of different Markers. Here I want to print i variable value of loop whenever respective marker will get clicked. So I did by following way .. but it is not working , It display same last value 170 of loop on the Snackbar in every different marker click.. But I suppose to get 0,10,20,30....170 respectively in snackbar on different marker click.
Please help...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
//ON MARKER CLICK
final int finalI = i;
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Snackbar.make((View) findViewById(R.id.map),""+finalI,Snackbar.LENGTH_LONG).show();
return true;
}
});
}
}
Here is the marker which was created by loop
but I am getting same value to 170
To Resolve your problem you should have a marker array.
Try this:
First make your app to implement GoogleMap.OnMarkerClickListener
Then create a Marker array :
Marker[] marker = new Marker[20]; //change length of array according to you
then inside
onMapReady(){
mMap.setOnMarkerClickListener(this);
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
marker[i] = mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
}
then finally
#Override
public boolean onMarkerClick(Marker marker) {
//you can get assests of the clicked marker
return false;
}
I found one way...
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// SETTING MARKER
for(int i=0;i<180;i=i+10) {
LatLng sydney = new LatLng(i, i);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Position"+i));
}
//ON MARKER CLICK
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
for(int i=0;i<180;i=i+10) {
if (marker.getTitle().equals("Marker in Position" + i))
Snackbar.make((View) findViewById(R.id.map), "" + i, Snackbar.LENGTH_LONG).show();
}return true;
}
});
}

how to put particular marker in center from list of markers

i have ListView which showing the number of locations, when i click on any location the map is showing with marker of all the list of location but i want to show the selected location's marker in center and others are there area.
How to show the selected location in center and others are as it is
gMap is the object of GoogleMap
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
marker.showInfoWindow();
LatLng currentPoint = new LatLng(26.5886, 72.982);// pass here your selected marker coordinate(Latitude & Longitude) like lat
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentPoint, 7);// set here your zoom level
gMap.animateCamera(cameraUpdate);
return true;
}
});
for e.g.
gMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(com.google.android.gms.maps.model.Marker marker) {
marker.showInfoWindow();
YourModel myMarker = mMarkersHashMap.get(marker);// it is your model
LatLng currentPoint = new LatLng(Double.valueOf(myMarker.getLat()), Double.valueOf(myMarker.getLon()));
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(currentPoint, 7);
gMap.animateCamera(cameraUpdate);
return true;
}
});

on marker click in Maps

I am trying to get the marker that is clicked but i'm only getting the last marker name
I wish to apply dialog box on the selected marker but it's getting the last marker only
for(int i=0;i<objectResults.length();i++){
JSONObject place=objectResults.getJSONObject(i);
String store_id=place.getString("id");
final String place_name=place.getString("name");
double latitude1, longitude1;
latitude1=place.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
longitude1=place.getJSONObject("geometry").getJSONObject("location").getDouble("lng");
MarkerOptions markerOptions=new MarkerOptions();
LatLng latLng=new LatLng(latitude1,longitude1);
markerOptions.position(latLng);
markerOptions.title(place_name);
mMap.addMarker(markerOptions);
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),"YOU CLICKED ON "+place_name,Toast.LENGTH_LONG).show();
return false;
}
);
}
thanks in Advance.
You should get the title of the clicked marker using getTitle() where is storage place_name. like this:
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(getContext(),"YOU CLICKED ON "+marker.getTitle(),Toast.LENGTH_LONG).show();
return false;
}
);
}

Getting a map marker by its ID and Index when click on particular marker in google map

I'm using a map in one of my applications. I'd like to know what's a common/efficient way to get the associated Object of a marker when it is clicked.
and also i'd like to know what's a current index of a marker.
mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.title("Melbourne")
.snippet(KEY + "Population: 4,137,400"));
#Override
public boolean onMarkerClick(Marker marker) {
String s = marker.getSnippet()
}
Just try this one
{
private Marker myMarker;
private void setUpMap()
{
.......
googleMap.setOnMarkerClickListener(this);
myMarker = googleMap.addMarker(new MarkerOptions()
.position(latLng)
.title("My Spot")
.snippet("This is my spot!")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
......
}
#Override
public boolean onMarkerClick(final Marker marker) {
if (marker.equals(myMarker))
{
//handle click here
}
}
}
Here you can see the HashMap Example.
You create a global HashMap variable like
HashMap< Marker, YourClass> hashMap=new HashMap<Marker, YourClass>();
Put you marker as a key and your object as a value when you are adding a marker on map like
MarkerOptions options = new MarkerOptions();
options.position(new LatLng(Lat, Lng));
options.icon(BitmapDescriptorFactory
.fromResource(R.drawable.ic_your_marker));
Marker mMarker = googleMap.addMarker(options);
mMarker.setTitle("Title");
hashMap.put(mMarker,YourClassObject);
Then override onMarkerClick method like
#Override
public boolean onMarkerClick(Marker marker) {
YourClass obj=(YourClass) hashMap.get(marker);
//now you can use corresponding object
}

MarkerOptions tap on info box

I have application with google map v2 containing couple of MarkerOptions inside it. Is there a way to catch tap on info box that is opened after pin is tapped?
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// do something
}
});
Probably duplicate question.
Try out as below:
GoogleMap mMap;
Marker myMarker= mMap.addMarker(new MarkerOptions()
.position(lng)
.title("Head Quarter Office")
.snippet("Delhi")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon)));
mMap.setOnMarkerClickListener(new OnMarkerClickListener()
{
#Override
public boolean onMarkerClick(Marker arg0) {
if(arg0.getTitle().equals("Marker")) // if marker source is clicked
Toast.makeText(MainActivity.this, arg0.getTitle(),1000).show();// display toast
return true;
}
});

Categories

Resources