2012-11-27 44 views
2

我想知道在給定的縮放級別,有多少米是特定的像素距離。獲取以米爲單位的像素距離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方法表示「近似」,但半徑與預期大小有很大不同。不應該是近似的效果。

謝謝。

回答

0

你的方法有一個小錯誤。

您正在計算屏幕中央級別屏幕一半的值。找到的距離只適用於在相同的緯度值上繪製圓(經度可能沒有問題)。

由於地球是快速離散的,因此在不同的緯度水平上計算相同像素數的距離會產生不同的結果。從Equador級別移動到接近杆級的位置,相同數量的像素會導致以米爲單位的更小距離。

但是,如果您將地圖定位在距離繪製圓的地方很遠的緯度,請致電getFittingRadiusInMeters()

否則,它應該工作正常。

方法getMeters()應該接收作爲參數GeoPoint(或至少緯度),其應該被用於計算距離。

問候。

相關問題