2010-09-08 27 views
6

Actualmente estoy desarrollando una aplicación con Andoid Maps SDK.Android Maps get Scroll Event

Ahora me gustaría recibir un aviso si el usuario se desplaza por el mapa para cargar marcadores adicionales desde un servidor basado en el nuevo centro del mapa.

Ya he buscado una función para registrar un oyente, pero no encontré nada.

¿Hay alguna forma de informarse sobre los cambios en el centro del mapa? Si no desea implementar un mecanismo de votación para que ... :(

Respuesta

1

he hecho esto de dos maneras:..

táctil oyente Establecer el oyente en el tacto de la vista del mapa Cada vez el usuario levanta su dedo (o se mueve, o toca el suelo), puede volver a cargar.

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; 
    } 
}); 

Anulación OnLayout. Cada vez que el mapa se mueve, OnLayout se llama. Si se extiende la clase MapView, puede anular onLayout para ver estos eventos. Configuré un temporizador aquí para ver si el movimiento ent se había detenido.

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); 
      } 
     } 
    }; 
} 

También puede utilizar el temporizador en el método de escucha de contacto. Esto fue solo un ejemplo. ¡Espero que esto ayude!

+0

perdido tanto tiempo en this.None de las obras de método. downvote es necesario. – Nezam

1

Tome un vistazo a la siguiente entrada en el blog (viene con código de Github): http://bricolsoftconsulting.com/extending-mapview-to-add-a-change-event/

+0

no funciona –

+0

Funciona, para muchas personas, como puede ver en los comentarios de esa página. Es posible que no funcione para usted, según su sistema operativo y/o dispositivo Android. Pero dejar un comentario vago y darme un voto negativo no ayudará a nadie. ¿Qué hay de participar en una conversación constructiva aquí y decirnos qué dispositivo y qué versión de Android está utilizando? – Theo

+0

Estoy usando Galaxy Nexus en 4.1 y Galaxy Note 10.1 en 4.0, el componente rara vez arroja evento de cambio. Lleva mucho tiempo y se desplaza hasta que arroja el primer evento de cambio, luego deja de funcionar nuevamente. Es mucho más confiable manejar el detector de eventos táctiles desde el MapView estándar. Por lo tanto, no funciona, mis usuarios no están vinculados a un dispositivo que el autor estaba probando. –