2010-09-08 15 views
6

Attualmente sto sviluppando un'applicazione con l'SDK di Andoid Maps.Le mappe Android ottengono l'evento di scorrimento

Ora desidero ricevere un avviso se l'utente scorre la mappa per caricare indicatori aggiuntivi da un server in base al nuovo centro mappa.

Ho già cercato una funzione per registrare un ascoltatore ma non ho trovato nulla.

C'è un modo per essere informato sulle modifiche al centro mappa? Non voglio implementare un meccanismo di polling per quello ... :(

risposta

1

ho fatto in due modi:..

tocco ascoltatore Impostare l'ascoltatore in contatto per la vista mappa Ogni volta che l'utente solleva il dito (o si muove, o atterra), è possibile ricaricare.

mapView.setOnTouchListener(new OnTouchListener() { 

    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_UP: 
      // The user took their finger off the map, 
      // they probably just moved it to a new place. 
      break; 
      case MotionEvent.ACTION_MOVE: 
      // The user is probably moving the map. 
      break; 
     } 

     // Return false so that the map still moves. 
     return false; 
    } 
}); 

Override onLayout. Ogni volta che la carta si sposta, onLayout viene chiamato. Se si estende la classe MapView, è possibile ignorare onLayout per catturare questi eventi. Ho impostato un timer qui per vedere se il movem ent si era fermato.

public class ExtendedMapView extends MapView { 
    private static final long STOP_TIMER_DELAY = 1500; // 1.5 seconds 
    private ScheduledThreadPoolExecutor mExecutor; 
    private OnMoveListener mOnMoveListener; 
    private Future mStoppedMovingFuture; 

    /** 
    * Creates a new extended map view. 
    * Make sure to override the other constructors if you plan to use them. 
    */ 
    public ExtendedMapView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     mExecutor = new ScheduledThreadPoolExecutor(1); 
    } 

    public interface OnMoveListener { 
     /** 
     * Notifies that the map has moved. 
     * If the map is moving, this will be called frequently, so don't spend 
     * too much time in this function. If the stopped variable is true, 
     * then the map has stopped moving. This may be useful if you want to 
     * refresh the map when the map moves, but not with every little movement. 
     * 
     * @param mapView the map that moved 
     * @param center the new center of the map 
     * @param stopped true if the map is no longer moving 
     */ 
     public void onMove(MapView mapView, GeoPoint center, boolean stopped); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 

     if (mOnMoveListener != null) { 
      // Inform the listener that the map has moved. 
      mOnMoveListener.onMove(this, getMapCenter(), false); 

      // We also want to notify the listener when the map stops moving. 
      // Every time the map moves, reset the timer. If the timer ever completes, 
      // then we know that the map has stopped moving. 
      if (mStoppedMovingFuture != null) { 
       mStoppedMovingFuture.cancel(false); 
      } 
      mStoppedMovingFuture = mExecutor.schedule(onMoveStop, STOP_TIMER_DELAY, 
        TimeUnit.MILLISECONDS); 
     } 
    } 

    /** 
    * This is run when we have stopped moving the map. 
    */ 
    private Runnable onMoveStop = new Runnable() { 
     public void run() { 
      if (mOnMoveListener != null) { 
       mOnMoveListener.onMove(ExtendedMapView.this, getMapCenter(), true); 
      } 
     } 
    }; 
} 

È possibile utilizzare il timer anche nel metodo di ascolto touch. Questo era solo un esempio. Spero che aiuti!

+0

sprecato così tanto tempo su this.None delle opere di metodo. il downvote è necessario. – Nezam

1

Date un'occhiata al seguente post sul blog (viene fornito con il codice di Github): http://bricolsoftconsulting.com/extending-mapview-to-add-a-change-event/

+0

non funziona –

+0

Funziona - per molte persone - come puoi vedere dai commenti su quella pagina. Potrebbe non funzionare per te, a seconda del tuo sistema operativo e/o dispositivo Android. Ma lasciare un commento vago e darmi un downvote non aiuterà nessuno. Che ne dici di impegnarsi in una conversazione costruttiva qui e facendoci sapere quale dispositivo e quale versione di Android stai usando? – Theo

+0

Sto usando Galaxy Nexus su 4.1 e Galaxy Note 10.1 su 4.0, il componente raramente lancia eventi di cambiamento. Ci vuole un sacco di tempo e lo scorrimento fino a quando non lancia il primo evento di cambiamento, quindi smette di funzionare di nuovo. È molto più affidabile gestire il listener di eventi touch dal MapView standard. Quindi non funziona, i miei utenti non sono legati a un dispositivo su cui l'autore stava testando. –

Problemi correlati