【问题标题】:Osmdroid Bonuspack - MyLocationNewOverlayOsmdroid Bonuspack - MyLocationNewOverlay
【发布时间】:2016-02-16 10:10:22
【问题描述】:

我目前有几个功能会导致一些问题,这些问题原本可以工作,但在更改了一些内容之后现在会产生错误。使用 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,但也没有用。

第二个问题在于按钮上的“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()); 上产生空指针错误

【问题讨论】:

    标签: java android openstreetmap osmdroid


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

    【讨论】:

    • 感谢您的回复,它有帮助,但不幸的是,就像您说的那样,这不是这个问题的解决方案。您使用 Array List 和 ItemizedIconOverlay 的方式是否允许您返回并选择特定的叠加层?我只是像在我的问题中一样添加我的叠加层,所以管理它们很痛苦。
    • 是的,例如,访问特定的叠加层并更新标记的位置很容易。例如,如果我想要一个显示用户当前位置的标记,我只为该单个标记创建一个叠加层,这样我就可以轻松访问它并在需要时更改 GeoPoint。如果您对此感兴趣,我也可以将代码复制给您。
    • 拜托,它也可能会更清楚地说明问题!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2012-04-23
    • 1970-01-01
    相关资源
    最近更新 更多