web-dev-qa-db-fra.com

Comment obtenir l'événement de clic du texte du marqueur

J'affiche google map api v2 dans mon application. J'ai placé quelques marqueurs sur la carte. J'ai également défini le titre et l'extrait sur les marqueurs qui s'affichent lorsque vous cliquez sur le marqueur.

Maintenant, je veux appeler une nouvelle activité lorsque l'on clique sur le titre du marqueur et non sur le marqueur lui-même.

map.setOnMarkerClickListner

est appelé uniquement sur le clic du marqueur.

Mais je ne veux pas faire ça. Je veux que le marqueur affiche le titre et l'extrait au clic du marqueur, mais je veux appeler une nouvelle activité au clic du titre.

Une idée de comment on fait ça?

Merci

18
Gaurav

Pour ce faire, vous devez implémenter setOnInfoWindowClickListener dans votre méthode getInfoContents afin qu'un clic sur votre fenêtre infoContents réveille l'auditeur pour faire ce que vous voulez, vous le faites comme ça :

   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;

            }
        });  
31
Emil Adz

Pour définir un titre sur un marqueur:

marker.showInfoWindow();

Pour définir un écouteur de clics sur le titre:

googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

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

    }
});
18
class Android
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");
                    }
                });
5
Sanjeev Sangral
/**
 * 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");
            }

        }
    });

}
0
Amit Sinha