2013-05-31 22 views
30

Ho la vista mappa nel mio frammento. Devo aggiornare la mappa e aggiungere diversi marcatori in base alle condizioni. Quindi, dovrei rimuovere gli ultimi marker dalla mappa prima di aggiungere nuovi marker.Android, Come rimuovere tutti i marker da Google Map V2?

In realtà, alcune settimane fa l'app funzionava bene e improvvisamente è successo. Il mio codice è simile a questo:

private void displayData(final List<Venue> venueList) { 

     // Removes all markers, overlays, and polylines from the map. 
     googleMap.clear(); 
. 
. 
. 
} 

L'ultima volta che è stato lavorando bene (prima del nuovo Google Map API annunciare dal team di Android in I/O 2013). Tuttavia, dopo che ho adattato il mio codice per utilizzare questa nuova API. Ora, non so perché questo metodo googleMap.clear(); non funziona!

Qualsiasi suggerimento sarebbe apprezzato. Grazie

=======

Aggiornamento

=======

completo del codice:

private void displayData(final List<Venue> venueList) { 

     // Removes all markers, overlays, and polylines from the map. 
     googleMap.clear(); 

     // Zoom in, animating the camera. 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL), 2000, null); 

     // Add marker of user's position 
     MarkerOptions userIndicator = new MarkerOptions() 
       .position(new LatLng(lat, lng)) 
       .title("You are here") 
       .snippet("lat:" + lat + ", lng:" + lng); 
     googleMap.addMarker(userIndicator); 

     // Add marker of venue if there is any 
     if(venueList != null) { 
      for(int i=0; i < venueList.size(); i++) { 
       Venue venue = venueList.get(i); 
       String guys = venue.getMaleCount(); 
       String girls= venue.getFemaleCount(); 
       String checkinStatus = venue.getCan_checkin(); 
       if(checkinStatus.equalsIgnoreCase("true")) 
        checkinStatus = "Checked In - "; 
       else 
        checkinStatus = ""; 

       MarkerOptions markerOptions = new MarkerOptions() 
         .position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude()))) 
         .title(venue.getName()) 
         .snippet(checkinStatus + "Guys:" + guys + " and Girls:" + girls) 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_orange_pin)); 

       googleMap.addMarker(markerOptions); 
      } 
     } 

     // Move the camera instantly to where lat and lng shows. 
     if(lat != 0 && lng != 0) 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), ZOOM_LEVEL)); 

     googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
      @Override 
      public View getInfoWindow(Marker marker) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 
       return null; 
      } 
     }); 

     googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 
      @Override 
      public void onInfoWindowClick(Marker marker) { 
       String str = marker.getId(); 
       Log.i(TAG, "Marker id: " + str); 
       str = str.substring(1); 
       int markerId = Integer.parseInt(str); 
       markerId -= 1; // Because first item id of marker is 1 while list starts at 0 
       Log.i(TAG, "Marker id " + markerId + " clicked."); 

       // Ignore if User's marker clicked 
       if(markerId < 0) 
        return; 

       try { 
        Venue venue = venueList.get(markerId); 
        if(venue.getCan_checkin().equalsIgnoreCase("true")) { 
         Fragment fragment = VenueFragment.newInstance(venue); 
         if(fragment != null) 
          changeFragmentLister.OnReplaceFragment(fragment); 
         else 
          Log.e(TAG, "Error! venue shouldn't be null"); 
        } 
       } catch(NumberFormatException e) { 
        e.printStackTrace(); 
       } catch(IndexOutOfBoundsException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
+2

Beh, ho provato e '' lavori chiare(). Devi fare qualcosa di sbagliato ... – zbr

+0

Sto chiamando questo metodo due volte. il primo non ha problemi la seconda volta questa linea non cancella i marker e aggiunge nuovi marker alla mappa (insieme ai vecchi). Quindi, ho confuso ciò che sta accadendo. – Hesam

+1

Grazie per la conferma. Cercherò di ricontrollare forse hai ragione sto facendo qualcosa di sbagliato :( – Hesam

risposta

33

Ok ho finalmente trovato un sostituto modo per risolvere il mio problema. Il problema interessante è quando assegni un marcatore alla mappa, il suo id è 'm0'. Quando lo rimuovi dalla mappa e assegni un nuovo marker ti aspetti che l'id debba essere 'm0' ma è 'm1'. Pertanto, mi ha mostrato che l'ID non è affidabile. Così ho definito List<Marker> markerList = new ArrayList<Marker>(); da qualche parte nello onActivityCreated() del mio frammento.

Quindi modificato sopra il codice con quello successivo. Spero che aiuti gli altri se hanno problemi simili con i marcatori.

private void displayData(final List<Venue> venueList) { 
     Marker marker; 

     // Removes all markers, overlays, and polylines from the map. 
     googleMap.clear(); 
     markerList.clear(); 

     // Zoom in, animating the camera. 
     googleMap.animateCamera(CameraUpdateFactory.zoomTo(ZOOM_LEVEL), 2000, null); 

     // Add marker of user's position 
     MarkerOptions userIndicator = new MarkerOptions() 
       .position(new LatLng(lat, lng)) 
       .title("You are here") 
       .snippet("lat:" + lat + ", lng:" + lng); 
     marker = googleMap.addMarker(userIndicator); 
//  Log.e(TAG, "Marker id '" + marker.getId() + "' added to list."); 
     markerList.add(marker); 

     // Add marker of venue if there is any 
     if(venueList != null) { 
      for (Venue venue : venueList) { 
       String guys = venue.getMaleCount(); 
       String girls = venue.getFemaleCount(); 
       String checkinStatus = venue.getCan_checkin(); 
       if (checkinStatus.equalsIgnoreCase("true")) 
        checkinStatus = "Checked In - "; 
       else 
        checkinStatus = ""; 

       MarkerOptions markerOptions = new MarkerOptions() 
         .position(new LatLng(Double.parseDouble(venue.getLatitude()), Double.parseDouble(venue.getLongitude()))) 
         .title(venue.getName()) 
         .snippet(checkinStatus + "Guys:" + guys + " and Girls:" + girls) 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_orange_pin)); 

       marker = googleMap.addMarker(markerOptions); 
//    Log.e(TAG, "Marker id '" + marker.getId() + "' added to list."); 
       markerList.add(marker); 
      } 
     } 

     // Move the camera instantly to where lat and lng shows. 
     if(lat != 0 && lng != 0) 
      googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), ZOOM_LEVEL)); 

     googleMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() { 
      @Override 
      public View getInfoWindow(Marker marker) { 
       return null; 
      } 

      @Override 
      public View getInfoContents(Marker marker) { 
       return null; 
      } 
     }); 

     googleMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() { 
      @Override 
      public void onInfoWindowClick(Marker marker) { 
       int markerId = -1; 

       String str = marker.getId(); 
       Log.i(TAG, "Marker id: " + str); 
       for(int i=0; i<markerList.size(); i++) { 
        markerId = i; 
        Marker m = markerList.get(i); 
        if(m.getId().equals(marker.getId())) 
         break; 
       } 

       markerId -= 1; // Because first item of markerList is user's marker 
       Log.i(TAG, "Marker id " + markerId + " clicked."); 

       // Ignore if User's marker clicked 
       if(markerId < 0) 
        return; 

       try { 
        Venue venue = venueList.get(markerId); 
        if(venue.getCan_checkin().equalsIgnoreCase("true")) { 
         Fragment fragment = VenueFragment.newInstance(venue); 
         if(fragment != null) 
          changeFragmentLister.OnReplaceFragment(fragment); 
         else 
          Log.e(TAG, "Error! venue shouldn't be null"); 
        } 
       } catch(NumberFormatException e) { 
        e.printStackTrace(); 
       } catch(IndexOutOfBoundsException e) { 
        e.printStackTrace(); 
       } catch (NullPointerException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 
+1

Come rimuovere tutti i marcatori da 'ArrayList' o da' MarkerArray' senza 'clear()' metodo –

+0

Funziona per me + 1.Grazie – Ninja

+0

sarebbe bene se prima spiegassi in breve –

15

Se si desidera cancellare "tutti i marcatori, le sovrapposizioni e polilinee dalla mappa", utilizzare clear() sul GoogleMap.

+2

Un caloroso benvenuto a te caro Ahmad per esserti iscritto a StackOverflow.Per evitare di cadere in basso, leggi altre risposte risposte particolarmente accettate prima di rispondere a una domanda, per favore. – Hesam

+1

Aiuta il database di Stackoverflow non risuonò di risposte doppie. Grazie. – Hesam

1

Usa map.clear() per rimuovere tutti gli indicatori da Google map

Problemi correlati