Reputation: 270
I have a onMarkerClickListener on my google map which fires when a marker is pressed as it should.
the markers are created in a Tag class that creates the marker within itself on a map that is passed though:
instance of the list at the start of the map activity:
//tags
List<Tag> tags = new ArrayList<Tag>();
In the onCreate() of the activity that contains the googleMap I add the markers to a list:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....other code here
//add the markers to the map
tags.add(new Tag(map,1,"City Of Dundee", "Home Of The Jakey", DUNDEE, this.getResources(),R.drawable.ic_launcher));
tags.add(new Tag(map,2,"Some Place","This is some place",LOCATION_Z,this.getResources(),R.drawable.ic_launcher));
....other code here
}//end on create
Constructor for the Tag class:
public Tag(GoogleMap map, int atagID,String tagTitle, String tagSnippet, LatLng tagPosition, Resources resource, int id){
this.tagID = atagID;
this.position = tagPosition;
this.title = tagTitle;
this.snippet = tagSnippet;
this.icon = BitmapDescriptorFactory.fromResource(id);
this.theTag = map.addMarker(new MarkerOptions()
.position(tagPosition)
.title(tagTitle)
.snippet(tagSnippet)
.icon(icon));
}
This creates the tag and it display on the map properly
In the listener for the onMarkerClickedListener i compare the marker clicked on the map to the marker from the list but the if statement never passes, even when I compare the titles which are identical.
The Listener:
onMarkerClickListener = new OnMarkerClickListener(){
@Override
public boolean onMarkerClick(Marker marker) {
//for loop that goes over array or marker
//if marker is equal to mark in array
//do marker functionality
for(Tag currentTag : tags){
if(currentTag.theTag == marker){
switch(currentTag.tagID){
case 1:
//do something for that button
Toast.makeText(getApplicationContext(), "marker 1", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(getApplicationContext(), "marker 2", Toast.LENGTH_SHORT).show();
return false;
default:
Toast.makeText(getApplicationContext(), "default", Toast.LENGTH_SHORT).show();
return false;
}
}else{
Toast.makeText(getApplicationContext(), "theTag " + currentTag.tagID + ": " + currentTag.theTag.getTitle(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "marker: " + marker.getTitle(), Toast.LENGTH_SHORT).show();
}
}
return false;
}
};
I hove no idea why it never reaches the switch statement any ideas?
Upvotes: 0
Views: 3787
Reputation: 381
The == operator will compare the direction of memory of both objects (which are not the same, since are different instances) instead of the object properties. the equals method, will compare the value of the attributes of your object or if you override the equals you can compare what ever you want, which will be more suitable to you.
Upvotes: 0
Reputation: 3836
Expanding on @MaciejGórski comment from the selected answer I would suggest using the ID of the marker (which is oddly a string).
This helps you avoid keeping hard references to Markers which themselves have references to the GoogleMap
object, and thus the entire MapView
/MapFragment
/SupportMapFragment
In my class I have:
private Map< String, MyObject > markersMap = new HashMap< String, MyObject >();
Then when I create the markers I add them to the map:
markersMap.put( marker.getId(), myObj );
Then in the listener I am able to retrieve myObj like this
@Override
public void onInfoWindowClick( Marker m )
{
Log.v( TAG, "onInfoWindowClick m.getId = " + m.getId() );
MyObject myObj = markersMap.get( m.getId() );
Log.v( TAG, "onInfoWindowClick myObj = " + myObj );
}
This way I avoid leaking the Views/Activity/Map etc
Upvotes: 0
Reputation: 270
I found the documentation i was looking for, don't know how i missed it.
As I have read you can't compare a marker using the '==' but you can using
if(markerA.equals(markerB)
{
}
Like so:
if(theTag.equals(marker){
//it will compare properly this way instead of returning false every time
}
Reference to website: https://developers.google.com/maps/documentation/android/marker
Upvotes: 2
Reputation: 19436
You do not need the for loop in your code. When you do the equality test on the markers(using ==) it will return false, and never reach your switch. Instead try removing the for loop and just us.
switch(marker.getId()){
case 1:
//do something for that button
Toast.makeText(getApplicationContext(), "marker 1", Toast.LENGTH_SHORT).show();
return true;
case 2:
Toast.makeText(getApplicationContext(), "marker 2", Toast.LENGTH_SHORT).show();
return false;
default:
Toast.makeText(getApplicationContext(), "default", Toast.LENGTH_SHORT).show();
return false;
}
}else{
Toast.makeText(getApplicationContext(), "theTag " + currentTag.tagID + ": " + currentTag.theTag.getTitle(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "marker: " + marker.getTitle(), Toast.LENGTH_SHORT).show();
}
Upvotes: 1