获取以米为单位的像素距离mapView
问题描述:
我想知道在给定的缩放级别,有多少米是特定的像素距离。获取以米为单位的像素距离mapView
原因:我想知道半径,以米为单位,在MapView的一个循环,这非常适合在的MapView的 - >radiusPixels = mapView.getWidth()/2;
我找到了方法mapView.getProjection().metersToEquatorPixels(radiusMeters)
,这确实的是,相反的我需要。但是这种方法或其他任何有用的东西都没有反转。
我(可能天真)的方法来解决这个问题如下:
private double getFittingRadiusInMeters() {
return getMeters(mapView.getWidth()/2);
}
private double getMeters(int pixels) {
Projection proj = mapView.getProjection();
Point mapCenterPixels = new Point(mapView.getWidth()/2, mapView.getHeight()/2);
//create 2 geopoints which are at pixels distance
GeoPoint centerGeoPoint = proj.fromPixels(mapCenterPixels.x, mapCenterPixels.y);
GeoPoint otherGeoPoint = proj.fromPixels(mapCenterPixels.x + pixels, mapCenterPixels.y);
Location loc = new Location("");
loc.setLatitude(centerGeoPoint.getLatitudeE6()/1E6);
loc.setLongitude(centerGeoPoint.getLongitudeE6()/1E6);
Location loc2 = new Location("");
loc2.setLatitude(otherGeoPoint.getLatitudeE6()/1E6);
loc2.setLongitude(otherGeoPoint.getLongitudeE6()/1E6);
return loc.distanceTo(loc2);
}
但它不能很好地工作。我总是得到比mapView小得多的圆 - 半径太小。
我知道distanceTo方法表示“近似”,但半径与预期大小有很大不同。不应该是近似的效果。
谢谢。
答
你的方法有一个小错误。
您正在计算屏幕*级别屏幕一半的值。找到的距离只适用于在相同的纬度值上绘制圆(经度可能没有问题)。
由于地球是快速离散的,因此在不同的纬度水平上计算相同像素数的距离会产生不同的结果。从Equador级别移动到接近杆级的位置,相同数量的像素会导致以米为单位的更小距离。
但是,如果您将地图定位在距离绘制圆的地方很远的纬度,请致电getFittingRadiusInMeters()
。
否则,它应该工作正常。
解
方法getMeters()
应该接收作为参数GeoPoint
(或至少纬度),其应该被用于计算距离。
问候。