Osmdroid Bonuspack – MyLocationNewOverlay
Actualmente tengo un par de características que están causando algunos problemas que originalmente funcionan, pero después de cambiar algunas cosas están produciendo errores. Uso de Android Studio que me permitió mirar las versiones anteriores del código, pero en vano.
De todos modos tengo un MyLocationNewOverlay declarado globalmente como así:
- Osmdroid muestra una cuadrícula vacía
- Abrir mapa de la calle trabajando offline en Android
- Limitar el desplazamiento en mapas sin conexión, en Android
- Creación de un mapa sin conexión en Android
- Osmdroid. OnItemSingleTapUp no funciona para Drawable personalizado
MyLocationNewOverlay location_overlay;
Que se inicia cuando el usuario navega a la actividad con el mapa:
map = (MapView) findViewByID(R.id.map); map.setVisibility(MapView.VISIBLE); <..some working code that sets the tile source and the center..> location_overlay = new MyLocationNewOverlay(getApplicationContext(), map); location_overlay.enableMyLocation(); location_overlay.setDrawAccuracyEnabled(true); map.getOverlays().add(location_overlay); map.invalidate();
Cuando estaba trabajando este código mostró un pequeño marcador humano con el círculo de precisión alrededor de él, pero ahora no, aunque no produce ningún error. Iv'e intentó el ahora decrépito MyLocationOverlay
que tampoco funcionó.
La segunda cuestión se encuentra dentro de un método 'onClick' en un botón que se supone que el mapa se centra en la ubicación actual de los usuarios, esto también se utiliza para trabajar.
public void onBtnFocusOnMe(View view){ GeoPoint gp = new GeoPoint(location_overlay.getMyLocation()); if(gp != null){ mapController.animateTo(gp); mapController.zoomTo(16); } }
Que produce un error de puntero nulo en GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());
- OsmDroid y MapQuest: ¿Cómo puedo usar los mosaicos JPEG?
- RoadManager para el error osmdroid
- Adición de superposición a OSMDROID
- Cómo descargar osmdroid azulejos en el usuario final?
- Archivo sin conexión de Osmdroid
- El proveedor de azulejos de Osmdroid y Mapnik ya no funciona
- Osmdroid - cambiar la carpeta del mapa local
- ¿Cómo se puede descargar el mapa en osmdroid?
¿Cómo normalmente superponer algunos elementos es así, no es directamente su solución, pero tal vez puede extraer algo útil de aquí:
public void showStartGoalMarkers(GeoPoint start, GeoPoint goal) { List<OverlayItem> mStartGoalItems = new ArrayList<>(); OverlayItem startItem = new OverlayItem("", "", start); Drawable newMarker = mMapView.getContext().getResources().getDrawable(R.drawable.ic_start); startItem.setMarker(newMarker); mStartGoalItems.add(startItem); OverlayItem goalItem = new OverlayItem("", "", goal); Drawable newMarker2 = mMapView.getContext().getResources().getDrawable(R.drawable.ic_end); goalItem.setMarker(newMarker2); mStartGoalItems.add(goalItem); mMapView.getOverlays().add(new ItemizedIconOverlay<OverlayItem>(mStartGoalItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() { @Override public boolean onItemSingleTapUp(int index, OverlayItem item) { return false; } @Override public boolean onItemLongPress(int index, OverlayItem item) { return false; } }, mMapView.getResourceProxy())); }
Y al final invalida la vista del mapa. Espero eso ayude.
EDIT: el código para marcar la ubicación actual y que también actualiza la posición actual cuando se pasa una nueva ubicación:
private void markMyLocation(Location location) { mOverlayItems.add(0, new OverlayItem("", "", new GeoPoint(location))); if (mMyLocationOverlay == null) { mMyLocationOverlay = new MyLocationOverlay(mOverlayItems, new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() { @Override public boolean onItemSingleTapUp(int index, OverlayItem item) { IMapController mapController = mMapView.getController(); mapController.setCenter(item.getPoint()); mapController.setZoom(mMapView.getMaxZoomLevel()); return true; } @Override public boolean onItemLongPress(int index, OverlayItem item) { return false; } }, mMapView.getResourceProxy()); mMapView.getOverlays().add(mMyLocationOverlay); mMapView.getController().setZoom(16); } else { IMapController mapController = mMapView.getController(); mapController.setCenter(mOverlayItems.get(0).getPoint()); mMapView.invalidate(); } }
La clase MyLocationOverlay:
public class MyLocationOverlay extends ItemizedIconOverlay<OverlayItem> { List<OverlayItem> mMyLocation; int mResourceId; public MyLocationOverlay(List<OverlayItem> pList, OnItemGestureListener<OverlayItem> pOnItemGestureListener, ResourceProxy pResourceProxy) { super(pList, pOnItemGestureListener, pResourceProxy); this.mMyLocation = pList; this.mResourceId = R.drawable.my_location; } @Override public void draw(Canvas canvas, MapView mapview, boolean arg2) { super.draw(canvas, mapview, true); if (!mMyLocation.isEmpty()) { IGeoPoint geoPointLocation = mMyLocation.get(0).getPoint(); Point out = new Point(); mapview.getProjection().toPixels(geoPointLocation, out); Bitmap bm = BitmapFactory.decodeResource(mapview.getResources(), mResourceId); canvas.drawBitmap(bm, out.x - bm.getWidth() / 2, //shift the bitmap center out.y - bm.getHeight() / 2, //shift the bitmap center null); } } @Override public boolean onSingleTapUp(MotionEvent event, MapView mapView) { // TODO Auto-generated method stub //return super.onSingleTapUp(event, mapView); return true; }
Básicamente lo que hago es sobrescribir el único elemento en el ArrayList mOverlayItems cuando el método se llama e invalidar el mapa.
- Android Drawable – getConstantState.newDrawable () vs mutate ()
- Cómo agregar borde a una ruleta sin afectar su menú emergente