我目前有一些功能会导致一些问题,这些问题在最初工作但是在改变一些事情之后现在会产生错误.使用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());
解决方法:
我通常如何覆盖一些项目是这样的,它不是直接你的解决方案,但你可以从这里提取一些有用的东西:
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;} @OverrIDepublic 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); }}@OverrIDepublic boolean onSingleTapUp(MotionEvent event, MapVIEw mapVIEw) { // Todo auto-generated method stub //return super.onSingleTapUp(event, mapVIEw); return true;}
基本上我所做的是在调用方法时覆盖ArrayList mOverlayItems中的单个项目并使地图无效.
总结以上是内存溢出为你收集整理的java – Osmdroid Bonuspack – MyLocationNewOverlay全部内容,希望文章能够帮你解决java – Osmdroid Bonuspack – MyLocationNewOverlay所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)