1

I try to make a if statement for a GoogleMap OnInfoWindowClickListener. But everytime I come to the else. And hint?

Marker s780 = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(52.16033, 7.87964))
    .title("780")
    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_780)));

Marker s149 = googleMap.addMarker(new MarkerOptions()
    .position(new LatLng(52.60861, 7.99206))
    .title("149")
    .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_149)));

googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
@Override
public void onInfoWindowClick(Marker marker) {
    if (marker.equals("s780"))
        Toast.makeText(getActivity(), "Marker 727", Toast.LENGTH_SHORT).show();
    if (marker.equals("s149"))
        Toast.makeText(getActivity(), "Marker 725", Toast.LENGTH_SHORT).show();
    else
        Toast.makeText(getActivity(), "Argument:"+marker, Toast.LENGTH_SHORT).show();
Eduard B.
  • 6,735
  • 4
  • 27
  • 38
Marcus Berger
  • 67
  • 1
  • 12

4 Answers4

0

In your code: if (marker.equals("s780")), marker is of type Marker, while "s780" is of type String. Of course equals returns false. You should change it to something like:

final Marker s780 = ...
googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
    @Override
    public void onInfoWindowClick(Marker marker) {
        if (marker == s780) {
            ...
        }
    }
}
xizzhu
  • 895
  • 6
  • 9
0

You should work with the getId() function of the Marker class.

For example inside the :

public void onInfoWindowClick(Marker marker) {
    StringBuilder builder = new StringBuilder(marker.getId());
    String str = builder.deleteCharAt(0).toString() ;
    int id = Integer.parseInt(str) ;
    if (id == 0)
        Toast.makeText(getActivity(), "Marker 780 was pressed", Toast.LENGTH_LONG).show();
}

Then you can do a simple comparison with ints which is much easier. The ids of the markers are in order of when they were added to the map, starting by 0. So your marker s780 has the id 0 and the marker s149 has the id 1.

I hope that helped

VollNoob
  • 267
  • 4
  • 15
0

You just missed an else, so even if the first if (s780) was true, you would make a Toast with

Marker 727

then check again for if (s149), but eventually go inside the else and make a Toast with

Argument:s780

   googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
     @Override
     public void onInfoWindowClick(Marker marker) {
        if (marker.equals("s780"))
            Toast.makeText(getActivity(), "Marker 727", Toast.LENGTH_SHORT).show();
        else //You missed this else
        if (marker.equals("s149")) 
            Toast.makeText(getActivity(), "Marker 725", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(getActivity(), "Argument:"+marker, Toast.LENGTH_SHORT).show();
David Corsalini
  • 7,958
  • 8
  • 41
  • 66
0

I have found a way for me:

if (marker.getTitle().equals("555 Test")) 
 { Toast.makeText(getActivity(), "Marker 5", Toast.LENGTH_SHORT).show();}

Thank you for your hinds.

Marcus Berger
  • 67
  • 1
  • 12