
public class MapItemizedOverlaySelect extends Overlay {
private int LONGPRESS_TIME = 500;
private long timeInitPress = 0;
private long timeFinishPress = 0;
private int latitud = 0;
private int longitud = 0;
private GeoPoint lastMapCenter;
private boolean isLongPress(){
return ((timeInitPress >= 0) && ((timeFinishPress - timeInitPress) > LONGPRESS_TIME));
}
@Override
public boolean onTouchEvent(MotionEvent event, MapView mapView){
if (event.getAction() == MotionEvent.ACTION_DOWN) {
timeInitPress = event.getEventTime();
lastMapCenter = mapView.getMapCenter();
GeoPoint p = mapView.getProjection().fromPixels(
(int) event.getX(),
(int) event.getY());
latitud = p.getLatitudeE6();
longitud = p.getLongitudeE6();
}
if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (!mapView.getMapCenter().equals(lastMapCenter)) {
timeInitPress = -1;
}
}
if (event.getAction() == MotionEvent.ACTION_UP) {
timeFinishPress = event.getEventTime();
if(isLongPress()){
listenerSelectPOI.onSelectPOI(latitud, longitud);
}
}
if (event.getPointerCount() > 1) {
timeInitPress = -1;
}
return false;
}
public interface OnSelectPOIListener{void onSelectPOI(int Latitud, int Longitud);}
private OnSelectPOIListener listenerSelectPOI;
public void setOnSelectPOIListener(OnSelectPOIListener l){listenerSelectPOI = l;}
}
Como podéis ver es muy sencillo. Controlamos el tiempo de inicio de actividad y el de fin, así como los tipos de movimiento y los números de cursores. Puede parecer mucho dicho así pero una vez en el código no lo es.Lo último que me falta por comentar de esta clase es que hacer una vez se ha detectado que es una pulsación larga. Muy fácil. Simplemente creamos un listener que nos devolverá la latitud y la longitud y en donde utilicemos esta clase recogeremos estos valores. Luego podemos hacer lo que queramos, pintar un marcador, hacer una búsqueda sobre estos datos o lo que se os ocurra.Vamos a ver un ejemplo muy simple de utilización de esta clase:
itemizedoverlay = new MapItemizedOverlaySelect();
mapOverlays.add(itemizedoverlay);
itemizedoverlay.setOnSelectPOIListener(new OnSelectPOIListener() {
public void onSelectPOI(int latitud, int longitud) {
//Crear marcador, guardar en base de datos o lo que se os ocurra aquí
}
});
Como veis, muy simple. Simplemente inicializar un objeto, añadirlo a las capas de nuestro mapa y a esperar a que el usuario haga una pulsación larga para hacer lo que vosotros necesitéis.
Enlace a la fuente original: Google Maps en Android: seleccionar puntos en un mapa (V)
Continúar leyendo...