How to permanently change behavior of markers for app?

103 views Asked by At

I want to make it so that when a user creates a marker in google maps, it is able to be double clicked (or held on or something) and bring up a new Activity. Right now I have it so that I create a marker on the map manually and use an onMarkerClickListener to wait until the marker is clicked to launch the new activity. Basically, I want to make it so that all markers created inside my app share this behavior, and I need some sort of check to differentiate between my own user marker and Google/external Markers.

Here is the code below:

public void setUsermarker() {

    mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker.equals(userMarker)) {  //if clicked marker equals marker created by user
                Intent intent = new Intent(MapsActivity.this, ObjectViewActivity.class);
                startActivity(intent);

            }       //Otherwise just show the info window
            return false;
        }
    });
}
1

There are 1 answers

2
Daniel Nugent On BEST ANSWER

You can add the ID of each Marker that you explicitly add to your map in a Set<String> in order to differentiate them from the other Markers on your map.

Documentation: https://developers.google.com/android/reference/com/google/android/gms/maps/model/Marker#getId()

    //instance variables:
    LatLng latLng;
    String title;
    String snippet;
    Set<String> markerIDs = new HashSet<String>();

When adding a Marker in your app, capture the returned Marker, get the ID, and add the ID to the Set:

    //Adding your markers
    Marker marker = mMap.addMarker(new MarkerOptions().position(latLng)
            .title(title)
            .snippet(snippet));

    //Add each ID to the Set
    String id = marker.getId();
    markerIDs.add(id);

Then, just check if the Marker ID is in the Set before launching the other Activity:

public void setUsermarker() {

        mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

            @Override
            public boolean onMarkerClick(Marker marker) {
                if (markerIDs.contains(marker.getId())) {  //if clicked marker equals marker created by user
                    Intent intent = new Intent(MapsActivity.this, ObjectViewActivity.class);
                    startActivity(intent);

                }       //Otherwise just show the info window
                return false;
            }
        });
    }