Osmdroid Bonuspack - MyLocationNewOverlay

I currently have several functions that cause several problems that initially work, but after changing some things now cause errors. Using Android Studio, which allowed me to view previous versions of the code, but to no avail.

Anyway, I have MyLocationNewOverlay declared globally, for example:

MyLocationNewOverlay location_overlay;

What is triggered when a user switches to activity with a 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();

When it worked, this code displayed a small human marker with a circle of precision around it, but now it does not even cause errors. Iv'e tried now decrepit MyLocationOverlay, which also did not work.

onClick , , .

public void onBtnFocusOnMe(View view){
    GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());
    if(gp != null){
        mapController.animateTo(gp);
        mapController.zoomTo(16);
    }
}

GeoPoint gp = new GeoPoint(location_overlay.getMyLocation());

+4
1

, , , - :

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

}

. , .

EDIT: :

 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:

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

, , ArrayList mOverlayItems .

+1

Source: https://habr.com/ru/post/1628950/


All Articles