24

I've been searching for help on implementing OnMarkerClickListener but nothing I've found has worked. This is my marker below and when clicked it only changes colour(light blue). I'm looking for it to open a bigger window so I can put in more info. Is this possible?

     googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(49.378,-0.3904))
    .title("Hello World")
    .snippet("This is my test app")    
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

The marker works fine above on my Map but now I would like to click on the marker and for it to open a new activity/page or a bigger window, what ever is easier to work with. As I am a real novice at making apps, If anyone who has successfully got a working example please could you put up a link or some code.

Thanks in advance!

Edit:

From the tutorial that was suggested I have changed some of the MainActivity.java.

I've added in OnMarkerClickListener and have chosen to add unimplemented methods to the Public Class

  public class MainActivity extends Activity implements LocationListener, OnMarkerClickListener {

Underneath private void setUpMap() I have added to my code: private Marker myMarker, the setonMarkerclick listener and myMarker =, :

       private Marker myMarker;
         {
googlemap.setOnMarkerClickListener(this);

myMarker = googlemap.addMarker(new MarkerOptions()
    .position(new LatLng(LatLng))
    .title("Hello World")
    .snippet("My First App")    
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE)));

 }

In the unimplemented method at the bottom I have:

   @Override
   public boolean onMarkerClick(Marker arg0) {
// TODO Auto-generated method stub 

return false;

What do I need to change in the public Boolean OnMarkerClick part? I'm not getting any errors but its just not working. What else do I have to add in or change?

Any help is appreciated!

Kara
  • 6,115
  • 16
  • 50
  • 57
user1977908
  • 1,105
  • 4
  • 11
  • 16

5 Answers5

31

Marker click events

Don't snap to marker after click in android map v2

Quoting from the above post

You can use an OnMarkerClickListener to listen for click events on the marker. To set this listener on the map, call GoogleMap.setOnMarkerClickListener(OnMarkerClickListener). When a user clicks on a marker, onMarkerClick(Marker) will be called and the marker will be passed through as an argument. This method returns a boolean that indicates whether you have consumed the event (i.e., you want to suppress the default behavior). If it returns false, then the default behavior will occur in addition to your custom behavior. The default behavior for a marker click event is to show its info window (if available) and move the camera such that the marker is centered on the map.

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMarkerClickListener.

Use OnMarkerClickListener on your marker.

Check the link for code snippets

Google Maps API v2: How to make markers clickable?

Example: Works on my phone


    Marker source, destination;
    GoogleMap mMap;

    mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    source = mMap.addMarker(new MarkerOptions()
            .position(sc)
            .title("MyHome")
            .snippet("Bangalore")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

    destination = mMap.addMarker(new MarkerOptions()
            .position(lng)
            .title("MapleBear Head Office")
            .snippet("Jayanager")
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.pin)));

    mMap.setOnMarkerClickListener(marker -> {
        if (marker.getTitle().equals("MyHome")) // if marker source is clicked
            Toast.makeText(MainActivity.this, marker.getTitle(), Toast.LENGTH_SHORT).show();// display toast
        return true;
    });

Asad Haider
  • 504
  • 1
  • 5
  • 17
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Thanks for your help. I have added in the code from the tutorial but there has been no changes so far, I can't click on the markers and it doesn't open anything. I have made an edit to my question and have included the updated code. Please have a look when you get the chance. – user1977908 May 19 '13 at 21:27
  • @user1977908 well if you see the accepted answer in the link it should work. Are you using info widow and trying to click on the same?. Also make your return statement in onMarkerClick to true – Raghunandan May 20 '13 at 05:57
  • I tried this code but I couldn't actually click on a marker afterwards. Also when I added in OnMarkerClickListener in the "public class MainActivity extends Activity" it asked me to add unimplemented methods so I had two @override public boolean onMarkerClick. This didn't really work aswell and couldn't get rid of one. – user1977908 May 28 '13 at 21:09
  • @JiteshUpadhyay not entirely. i have added a cope snippet of my own. – Raghunandan Feb 26 '14 at 04:55
  • @JiteshUpadhyay i am not sure if even copied it from the link which i normally don't do. I always check the docs. any way i added whats necessary. duplciating answers is wrong if you copy paste and add some of your own its not wrong. – Raghunandan Feb 26 '14 at 04:57
  • no issue sir i am removing my comments, please do not feel bad, please do not mind, please do not feel that i interfere yours work!! sorry – Jitesh Upadhyay Feb 26 '14 at 04:58
  • @JiteshUpadhyay no feel bad if you think or sense something is wrong don't hesitate to flag the answer in which case it will be dealt appropriately by moderators. – Raghunandan Feb 26 '14 at 04:59
  • Hello every body please any body tell me how can i transfer the the details of markers to new activity by applying click on listener on the marker – Nikhil Singh Jan 25 '16 at 13:31
  • @NikhilSingh you can see the documentaion of what details you can get with Marker and use intent to pass the details to new activity. https://developers.google.com/android/reference/com/google/android/gms/maps/model/Marker. Do your stuff in onMarkerClick – Raghunandan Jan 25 '16 at 16:23
  • @Raghunandan thanx fr ur advice Sir I will go through the link u mentioned – Nikhil Singh Jan 27 '16 at 06:01
10

This code handles the maker click event and loads a new layout (XML) with some information:

/**
 * adding individual markers, displaying text on on marker click on a
 * bubble, action of on marker bubble click
 */
private final void addLocationsToMap() {
    int i = 0;
    for (Stores store : storeList) {
        LatLng l = new LatLng(store.getLatitude(), store.getLongtitude());

        MarkerOptions marker = new MarkerOptions()
                .position(l)
                .title(store.getStoreName())
                .snippet("" + i)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
        googleMap.addMarker(marker);
        ++i;
    }

    googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker marker) {

            try {
                popUpWindow.setVisibility(View.VISIBLE);
                Stores store = storeList.get(Integer.parseInt(marker
                        .getSnippet()));

                // set details
                email.setText(store.getEmail());
                phoneNo.setText(store.getPhone());
                address.setText(store.getAddress());

                // setting test value to phone number
                tempString = store.getPhone();
                SpannableString spanString = new SpannableString(tempString);
                spanString.setSpan(new UnderlineSpan(), 0,
                        spanString.length(), 0);
                phoneNo.setText(spanString);

                // setting test value to email
                tempStringemail = store.getEmail();

                SpannableString spanString1 = new SpannableString(tempStringemail);
                spanString1.setSpan(new UnderlineSpan(), 0, spanString1.length(), 0);
                email.setText(spanString1);

                storeLat = store.getLatitude();
                storelng = store.getLongtitude();

            } catch (ArrayIndexOutOfBoundsException e) {
                Log.e("ArrayIndexOutOfBoundsException", " Occured");
            }

        }
    });

}
TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69
6

If you need the event Click in a market,this code it's the solution.

private GoogleMap mGoogleMap;


mGoogleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
        {

            @Override
            public boolean onMarkerClick(Marker arg0) {
                if(arg0 != null && arg0.getTitle().equals(markerOptions2.getTitle().toString())); // if marker  source is clicked
                    Toast.makeText(menu.this, arg0.getTitle(), Toast.LENGTH_SHORT).show();// display toast
                return true;
            }

        });

Good Luck

David Hackro
  • 3,652
  • 6
  • 41
  • 61
0

I suggest use of OnInfoWindowClickListener, it will trigger when you click on marker and then the snippet. Use setTag to attach any object with the marker.

    Marker marker = mMap.addMarker(markerOptions);
    marker.setTag(myObject);

and the listener

      mMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker arg0) {

         MyObject mo =  (MyObject )arg0.getTag();


           }
       });
Hassi
  • 120
  • 8
0

Below code to used for Kotlin when user click on marker to perform any action

        googleMap!!.setOnMarkerClickListener { marker ->
            if (marker.title == "Marker1")
                Log.d(TAG, "Clicked on Marker1")
            true
        }
Pratik Dodiya
  • 2,337
  • 1
  • 19
  • 12