How to get click event of the marker text

35,386

Solution 1

To achieve this you need to implement setOnInfoWindowClickListener in your getInfoContents method so that a click on your infoContents window will wake the listener to do what you want, you do it like so:

   map.setInfoWindowAdapter(new InfoWindowAdapter() {

            // Use default InfoWindow frame
            @Override
            public View getInfoWindow(Marker args) {
                return null;
            }

            // Defines the contents of the InfoWindow
            @Override
            public View getInfoContents(Marker args) {

                // Getting view from the layout file info_window_layout
                View v = getLayoutInflater().inflate(R.layout.info_window_layout, null);

                // Getting the position from the marker
                clickMarkerLatLng = args.getPosition();

                TextView title = (TextView) v.findViewById(R.id.tvTitle);
                title.setText(args.getTitle());

                map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {          
                    public void onInfoWindowClick(Marker marker) 
                    {
                        if (SGTasksListAppObj.getInstance().currentUserLocation!=null)
                        {   
                            if (String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLatitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) &&
                                    String.valueOf(SGTasksListAppObj.getInstance().currentUserLocation.getLongitude()).substring(0, 8).contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                            {
                                Toast.makeText(getApplicationContext(), "This your current location, navigation is not needed.",  Toast.LENGTH_SHORT).show();
                            }
                            else
                            {
                                FlurryAgent.onEvent("Start navigation window was clicked from daily map");
                                tasksRepository = SGTasksListAppObj.getInstance().tasksRepository.getTasksRepository();
                                for (Task tmptask : tasksRepository)
                                {
                                    String tempTaskLat = String.valueOf(tmptask.getLatitude());
                                    String tempTaskLng = String.valueOf(tmptask.getLongtitude());

                                    Log.d(TAG, String.valueOf(tmptask.getLatitude())+","+String.valueOf(clickMarkerLatLng.latitude).substring(0, 8));

                                    if (tempTaskLat.contains(String.valueOf(clickMarkerLatLng.latitude).substring(0, 8)) && tempTaskLng.contains(String.valueOf(clickMarkerLatLng.longitude).substring(0, 8)))
                                    {  
                                        task = tmptask;
                                        break;
                                    }
                                }

                                Intent intent = new Intent(getApplicationContext() ,RoadDirectionsActivity.class);
                                intent.putExtra(TasksListActivity.KEY_ID, task.getId());
                                startActivity(intent);

                            }
                        }
                        else
                        {
                            Toast.makeText(getApplicationContext(), "Your current location could not be found,\nNavigation is not possible.",  Toast.LENGTH_SHORT).show();
                        }
                    }
                });

                // Returning the view containing InfoWindow contents
                return v;

            }
        });  

Solution 2

To set a title on a marker:

marker.showInfoWindow();

To set a click listener on title:

googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

    @Override
    public void onInfoWindowClick(Marker arg0) {
        // TODO Auto-generated method stub

    }
});

Solution 3

GoogleMap mGoogleMap;  
mGoogleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {

                    @Override
                    public void onInfoWindowClick(Marker arg0) {
                        Intent intent = new Intent(getBaseContext(), Activity.class);
                        String reference = mMarkerPlaceLink.get(arg0.getId());
                        intent.putExtra("reference", reference);

                        // Starting the  Activity
                        startActivity(intent);
                        Log.d("mGoogleMap1", "Activity_Calling");
                    }
                });
Share:
35,386

Related videos on Youtube

Gaurav
Author by

Gaurav

Android Developer

Updated on July 09, 2022

Comments

  • Gaurav
    Gaurav almost 2 years

    I am displaying google map api v2 in my app. I have set some markers in the map. I have also set title and snippet on the markers which are shown when you click the marker.

    Now I want to call a new activity when clicked on the marker's title and not on marker itself.

    map.setOnMarkerClickListner
    

    is called only on the click of the marker.

    But I dont want to do that. I want the marker to show the title and snippet on the click of the marker but I want to call new activity on the click of the title.

    Any idea how we do that?

    Thanks

  • Pat Myron
    Pat Myron over 8 years
    where is that method in the second code snippet supposed to be placed? I'm just putting it in the Activity Class and it keeps telling me 'cannot resolved symbol 'setOnInfoWindowClickListener''
  • class Android
    class Android over 8 years
    Hi @PatMyron, I am not sure, why you are getting this. Did you import the listener? And where exactly are you placing your code?
  • Nikhil Singh
    Nikhil Singh over 8 years
    Heloo emil Adz , hw cn i Transfer the detail of marker to the new Activity by clicking on that marker please any body can help me on that
  • Emil Adz
    Emil Adz over 8 years
    @NikhilSingh, it has nothing to do with the question at hand, but to do that you can pass this data to the next activity by adding it to the intent bundle that you created to open the next activity.
  • Nikhil Singh
    Nikhil Singh over 8 years
    @EmilAdz thanks sir fr ur response bt how i can get the information of only that marker on which it is clicked
  • Emil Adz
    Emil Adz over 8 years
    @NikhilSingh, I don't understand you question, you do it exactly as it shown here in the answer. Please ask a new and more clear question and I will try to help you there.
  • Zin Win Htet
    Zin Win Htet over 8 years
    You need to import the listener.
  • Phong Nguyen
    Phong Nguyen over 5 years
    @EmilAdz it seems like it handles clicking event for the whole InfoWindow, how about if my window view have 2 TextView and 1 Button then I wanna handle clicking event for only the Button?
  • Emil Adz
    Emil Adz over 5 years
    @ThinkTwiceCodeOnce, you can't do it.