2013-03-16 2 views
1

Je cherche à créer une méthode qui s'exécute lorsque l'on clique sur la boîte d'informations/l'extrait d'un marqueur. Pour l'instant, je ne peux rien trouver à ce sujet, seulement le marqueur. De l'aide ou des liens pour trouver plus d'infos?Android Marker Click Fonction

+1

Toujours vérifier l'api doc premier: https://developers.google.com/maps/documentation/android/marker#info_window_click_events – moujib

Répondre

7

Pour créer une méthode qui fonctionne lorsque vous cliquez sur la fenêtre d'information vous devez setOnInfoWindowClickListener:

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(); 
         } 
        } 
       }); 
+0

Vous avez essentiellement répondu à une question que je vais devoir demander plus tard, ainsi que celui que j'ai demandé, alors merci beaucoup pour ça! –

+0

Je le ferai quand il me le permettra. –

+0

Vous avez oublié de retourner la vue dans 'getInfoContents' – ElliotM

0

Il suffit de définir OnInfoWindowClickListener pour votre carte.

map.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 
     @Override 
     public void onInfoWindowClick(Marker marker) { 
     //YOUR CODE 
     } 
    }); 
Questions connexes