2013-05-05 7 views
10

mam taki kod:Android Google Maps dodać do znacznika własny tag

protected void onPostExecute(final ArrayList<HashMap<String, String>> adapter) { 

       for (final HashMap<String, String> a : adapter) { 
        LatLng pos = new LatLng(Double.valueOf(a.get(TAG_latitude)), Double.valueOf(a.get(TAG_longitude))); 
        Log.e("pppppos", String.valueOf(pos.latitude)); 
        Marker m = map.addMarker(new MarkerOptions().position(pos) 
          .title(a.get(TAG_NAME)) 
          .snippet("Kiel is cool")); 

        map.setOnInfoWindowClickListener(
           new OnInfoWindowClickListener(){ 
            public void onInfoWindowClick(Marker marker){ 
             Intent nextScreen = new Intent(SearchExchangerActivity.this, BankExchangersListActivity.class); 
             nextScreen.putExtra("exchanger_id", id);  
             startActivityForResult(nextScreen, 0); 
            } 
            }); 
       } 

Ale muszę ustawić niewidoczne pola użytkownika np Tag_id dla każdego znacznika i używać tego identyfikatora następnie podczas wysyłania dodatkowych informacji do innych aktywność, coś w stylu:

protected void onPostExecute(final ArrayList<HashMap<String, String>> adapter) { 

      for (final HashMap<String, String> a : adapter) { 
       LatLng pos = new LatLng(Double.valueOf(a.get(TAG_latitude)), Double.valueOf(a.get(TAG_longitude))); 
       Marker m = map.addMarker(new MarkerOptions().position(pos) 
         .title(a.get(TAG_NAME)) 
         .snippet("Kiel is cool") 
                .Tag_id(TAG_ID)); 

       map.setOnInfoWindowClickListener(
          new OnInfoWindowClickListener(){ 
           public void onInfoWindowClick(Marker marker){ 
            Intent nextScreen = new Intent(SearchExchangerActivity.this, BankExchangersListActivity.class); 
            nextScreen.putExtra("exchanger_id", marker.get(TAG_ID));  
            startActivityForResult(nextScreen, 0); 
           } 
           }); 
      } 

Czy to naprawdę możliwe? Jak mogę uzyskać w swoim słuchaczu jaki znacznik klikam?

również można to zrobić poprzez tytułowym polu ... ale dostaję błąd podczas zapisywania marker.getTitle() ...

UPD

for (final HashMap<String, String> a : adapter) { 
       LatLng pos = new LatLng(Double.valueOf(a.get(TAG_latitude)), Double.valueOf(a.get(TAG_longitude))); 
       Log.e("pppppos", String.valueOf(pos.latitude)); 
       HashMap<Marker, String> m = new HashMap<Marker, String>(); 
         m.put(map.addMarker(new MarkerOptions().position(pos) 
         .title(a.get(TAG_NAME)) 
         .snippet("Kiel is cool")), "1");      

      } 

      map.setOnInfoWindowClickListener(
       new OnInfoWindowClickListener(){ 
        public void onInfoWindowClick(HashMap<Marker, String> marker){ 
         Intent nextScreen = new Intent(SearchExchangerActivity.this, BankExchangersListActivity.class); 
         nextScreen.putExtra("exchanger_id", "1"); 
         startActivityForResult(nextScreen, 0); 
        } 
        }); 

Odpowiedz

11
  1. Możesz użyć Map<Marker, String> i zachować dane tam lub
  2. użyć Android Maps Extensions, która dodaje getData i i funkcje do klasy Marker.

Przy okazji. Nie powinieneś ustawiać InfoWindowAdapter w pętli. To nie ma sensu. Pozostaje tylko ostatni.

+1

można dać przykład wykorzystania mapy ? –

+0

gdy próbuję napisać Mapa m = m.put (map.addMarker (nowe MarkerOptions(). Pozycja (poz) \t \t \t \t .title (a.get (TAG_NAME)) \t \t \t \t \t \t \t .Snippet ("Kiel is cool")), "1"); \t coś jest złe –

+1

@ValdisAzamaris Zobacz tutaj, aby obejść ten problem, używając 'Map': http://code.google.com/p/gmaps-api-issues/issues/detail?id=4650 –

2

To jest obecnie oficjalnie dostępne, setTag i getTag zostały dodane do interfejsu API Markera.

Poniższa został zabrany z sekcji „associate data with a marker” z oficjalnego dokumentu:

/** 
* A demo class that stores and retrieves data objects with each marker. 
*/ 
public class MarkerDemoActivity extends FragmentActivity implements 
     OnMarkerClickListener, 
     OnMapReadyCallback { 

    private static final LatLng PERTH = new LatLng(-31.952854, 115.857342); 
    private static final LatLng SYDNEY = new LatLng(-33.87365, 151.20689); 
    private static final LatLng BRISBANE = new LatLng(-27.47093, 153.0235); 

    private Marker mPerth; 
    private Marker mSydney; 
    private Marker mBrisbane; 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.marker_demo); 

     SupportMapFragment mapFragment = 
       (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    /** Called when the map is ready. */ 
    @Override 
    public void onMapReady(GoogleMap map) { 
     mMap = map; 

     // Add some markers to the map, and add a data object to each marker. 
     mPerth = mMap.addMarker(new MarkerOptions() 
       .position(PERTH) 
       .title("Perth"); 
     mPerth.setTag(0); 

     mSydney = mMap.addMarker(new MarkerOptions() 
       .position(SYDNEY) 
       .title("Sydney"); 
     mSydney.setTag(0); 

     mBrisbane = mMap.addMarker(new MarkerOptions() 
       .position(BRISBANE) 
       .title("Brisbane"); 
     mBrisbane.setTag(0); 

     // Set a listener for marker click. 
     mMap.setOnMarkerClickListener(this); 
    } 

    /** Called when the user clicks a marker. */ 
    @Override 
    public boolean onMarkerClick(final Marker marker) { 

     // Retrieve the data from the marker. 
     Integer clickCount = (Integer) marker.getTag(); 

     // Check if a click count was set, then display the click count. 
     if (clickCount != null) { 
      clickCount = clickCount + 1; 
      marker.setTag(clickCount); 
      Toast.makeText(this, 
          marker.getTitle() + 
          " has been clicked " + clickCount + " times.", 
          Toast.LENGTH_SHORT).show(); 
     } 

     // Return false to indicate that we have not consumed the event and that we wish 
     // for the default behavior to occur (which is for the camera to move such that the 
     // marker is centered and for the marker's info window to open, if it has one). 
     return false; 
    } 
} 
+0

Zwróć uwagę, że tag getter i setter są dostępne tylko od usług Google Play 9.4.0 – Skoua

Powiązane problemy