Osmdroid Bonuspack - MyLocationNewOverlay

时间:2016-02-16 10:10:23

标签: java android openstreetmap osmdroid

我目前有一些功能会导致一些问题,这些问题在最初工作但在改变一些事情之后现在会产生错误。使用Android Studio,我可以查看以前版本的代码,但无济于事。

无论如何,我有一个全局声明的MyLocationNewOverlay:

MyLocationNewOverlay location_overlay;

当用户使用地图导航到活动时会启动:

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

当它工作时,这个代码显示了一个带有精确圆圈的小人类标记,但现在即使它没有产生任何错误也没有。我已经尝试了现在已经破旧的MyLocationOverlay,它也没有用。

第二个问题在于&#39; onClick&#39;在一个按钮上的方法,该按钮应该将地图聚焦在用户的当前位置,这也用于工作。

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

上产生空指针错误

1 个答案:

答案 0 :(得分: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()));

}

最后使地图视图无效。希望它有所帮助。

编辑:标记当前位置的代码,以及在新位置通过时更新当前位置的代码:

 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中的单个项目并使地图无效。