Osmdroid Bonus Pack – MyLocationNewOverlay
I currently have several features causing some issues that initially worked fine but now produce errors after changing something. Using Android Studio you can look at previous versions of the code, but to no avail.
Anyway, I have a MyLocationNewOverlay global declaration like this:
MyLocationNewOverlay location_overlay;
Starts when the user navigates to an activity using map:
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();
This code shows a small humanoid marker with a precision circle when run, but now it doesn’t show any errors, even though it doesn’t produce any errors. I tried MyLocationOverlay
, which is now obsolete, but it doesn’t work either.
The second problem lies in the “onClick” method on the button, which should focus the map on the user’s current location, which also used to work.
public void onBtnFocusOnMe(View view){
GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());
if(gp != null){
mapController.animateTo(gp);
mapController.zoomTo(16);
}
}
This will be done in GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());
A null pointer error was generated
Solution
How I usually cover some projects is like this, it’s not your direct solution, but you might be able to extract something useful from here :
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()));
}
Finally, you invalidate the map view. Hope this helps.
Edit: Code to mark the current location
and code to update the current location when a new location is passed:
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();
}
}
MyLocationOverlay class:
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;
}
Basically what I did was overwrite a single item in the ArrayList mOverlayItems and invalidate the map when the method was called.