Android:Osmdroid 在触摸时添加标记不准确
Posted
技术标签:
【中文标题】Android:Osmdroid 在触摸时添加标记不准确【英文标题】:Android: Osmdroid add marker on touch not accurate 【发布时间】:2016-05-29 17:05:45 【问题描述】:我尝试在基于此question 触摸地图视图时添加标记,这是我的代码
GeoPoint geoPoint = new GeoPoint(mLatitude, mLongitude);
IMapController mapController = mMapView.getController();
mapController.setZoom(5);
mapController.setCenter(geoPoint);
Overlay overlay = new Overlay(getActivity())
ItemizedIconOverlay<OverlayItem> items = null;
@Override
protected void draw(Canvas c, MapView osmv, boolean shadow)
@Override
public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView)
Projection proj = mapView.getProjection();
GeoPoint loc = (GeoPoint) proj.fromPixels((int)e.getX(), (int)e.getY());
double longitude = loc.getLongitudeE6()/1000000;
double latitude = loc.getLatitudeE6()/1000000;
mLatitude = latitude;
mLongitude = longitude;
ArrayList<OverlayItem> markers = new ArrayList<>();
OverlayItem item = new OverlayItem("", "", new GeoPoint(latitude, longitude));
item.setMarker(ContextCompat.getDrawable(getActivity(), R.drawable.ic_maps_marker_large));
markers.add(item);
if (items == null)
items = new ItemizedIconOverlay<>(getActivity(), markers, null);
mMapView.getOverlays().add(items);
mMapView.invalidate();
else
mMapView.getOverlays().remove(items);
mMapView.invalidate();
items = new ItemizedIconOverlay<>(getActivity(), markers, null);
mMapView.getOverlays().add(items);
return true;
;
mMapView.getOverlays().add(overlay);
问题是在随机位置上添加的标记放大时,不准确。但是,如果 mapview 缩小它就可以正常工作,可能是大约 5 级及以下。
【问题讨论】:
【参考方案1】:(代表 OP 发布)。
最后我可以通过使用这段代码来解决这个问题:
GeoPoint geoPoint = new GeoPoint(mLatitude, mLongitude);
IMapController mapController = mMapView.getController();
mapController.setZoom(5);
mapController.setCenter(geoPoint);
Overlay overlay = new Overlay(getActivity())
ItemizedIconOverlay<OverlayItem> items = null;
@Override
protected void draw(Canvas c, MapView osmv, boolean shadow)
@Override
public boolean onSingleTapConfirmed(MotionEvent e, MapView mapView)
Projection proj = mapView.getProjection();
GeoPoint loc = (GeoPoint) proj.fromPixels((int)e.getX(), (int)e.getY());
double longitude = loc.getLongitude();
double latitude = loc.getLatitude();
mLatitude = latitude;
mLongitude = longitude;
ArrayList<OverlayItem> markers = new ArrayList<>();
OverlayItem item = new OverlayItem("", "", new GeoPoint(latitude, longitude));
item.setMarker(ContextCompat.getDrawable(getActivity(), R.drawable.ic_maps_marker_large));
markers.add(item);
if (items == null)
items = new ItemizedIconOverlay<>(getActivity(), markers, null);
mMapView.getOverlays().add(items);
mMapView.invalidate();
else
mMapView.getOverlays().remove(items);
mMapView.invalidate();
items = new ItemizedIconOverlay<>(getActivity(), markers, null);
mMapView.getOverlays().add(items);
return true;
;
mMapView.getOverlays().add(overlay);
【讨论】:
以上是关于Android:Osmdroid 在触摸时添加标记不准确的主要内容,如果未能解决你的问题,请参考以下文章