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;
}
});
Related
hi i dont know java but i have a homework and i'm trying to make a aapp with android studio its a map app i can add markerListener to markers but same happening ing every marker i want to give unique markerListener to all of my markers. I would be very glad if you help (my code)
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng Bodrum = new LatLng(37.034407, 27.430540);
LatLng UgurMosque = new LatLng(37.033124, 27.434417);
LatLng ABC = new LatLng(36.033124, 28.434417);
Marker ugur=mMap.addMarker(new MarkerOptions().position(UgurMosque)
.title("1")
);
Marker abc=mMap.addMarker(new MarkerOptions().position(ABC)
.title("2")
);
mMap.moveCamera(CameraUpdateFactory.newLatLng(Bodrum));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String markertitle=marker.getTitle();
Intent i=new Intent(MapsActivity.this, DetailsActivity.class);
i.putExtra("title",markertitle);
startActivity(i);
return false;
}
});
Use Marker tags instead of title, tags are set by you and are predefined identifiers
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
#Override
public boolean onMarkerClick(Marker marker) {
String markertag =marker.getTag().toString();
if(markertag.equals("tag1")){
}
return false;
}
});
You can use a switch-case statement instead
To add markers use
mMap.addMarker("tag1", lattitude, longitude);
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
}
}
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;
}
});
}
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;
}
);
}
Currently, I detect when user tap on Google Map maker as follow:
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
public boolean onMarkerClick(Marker marker) {
// ---- copy to clipbored
copyToClip(marker.getSnippet());
return false;
}
});
However, I would prefer to detect when user tab on the "title" or snippet of the map marker (instead of the marker itself).
Is there a way to do that?
Try this code..
mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {
#Override
public void onInfoWindowClick(Marker marker) {
// TODO
}
});