2013-07-09 6 views
12

Ich zeige google map api v2 in meiner app an. Ich habe einige Markierungen in der Karte gesetzt. Ich habe auch Titel und Snippet auf die Marker gesetzt, die angezeigt werden, wenn Sie auf den Marker klicken.Wie bekomme ich Klick-Ereignis des Markierungstextes

Jetzt möchte ich eine neue Aktivität aufrufen, wenn ich auf den Titel des Markers und nicht auf den Marker selbst klicke.

map.setOnMarkerClickListner 

wird nur auf dem Klick auf den Marker genannt.

Aber ich will das nicht tun. Ich möchte, dass der Marker den Titel und das Snippet auf den Klick des Markers zeigt, aber ich möchte beim Klicken auf den Titel eine neue Aktivität aufrufen.

Irgendeine Idee, wie wir das tun?

Dank

+0

möglich Duplikat [Android Marker Klicken Sie Function] (http://stackoverflow.com/questions/15455815/android-marker-click-function) – bummi

Antwort

25

das Sie implementieren setOnInfoWindowClickListener in Ihrem getInfoContents Verfahren müssen zu erreichen, so dass ein Klick auf Ihre infoContents Fenster, um den Hörer wecken zu tun, was Sie wollen, tun Sie es wie 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; 

      } 
     }); 
+0

Vielen Dank :) Es funktionierte ! – Gaurav

+0

Sie begrüßen @Gaurav, haben eine schöne Codierung. –

+0

Heloo emil Adz, hw cn Ich übertrage das Detail des Markers auf die neue Aktivität, indem ich auf diesen Marker klicke, jeder Körper kann mir dabei helfen –

12

Um einen Titel auf eine Markierung gesetzt:

marker.showInfoWindow(); 

einen Klick Zuhörer auf den Titel ein:

googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() { 

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

    } 
}); 
+0

Wo soll diese Methode im zweiten Code-Snippet platziert werden? Ich lege es nur in die Aktivitätsklasse und es sagt mir immer wieder, dass das Symbol 'setOnInfoWindowClickListener' nicht ' –

+0

Hi @PatMyron ist. Ich bin nicht sicher, warum Sie das bekommen. Hast du den Hörer importiert? Und wo genau platzieren Sie Ihren Code? –

+0

Sie müssen den Listener importieren. –

0
/** 
* 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"); 
      } 

     } 
    }); 

} 
4
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"); 
        } 
       }); 
Verwandte Themen