当我开发Android地图应用程序时,我想在半径为1米的地图上绘制一个圆圈.如你所知,我不能直接绘制1米,我应该转换1米到两个像素的距离取决于缩放级别.如何转换它,我可以使用任何API.
Canvas.draw(x,y,radius),我应该给这个方法什么值?
Mar*_*elo 10
假设您的地图是Google地图,他们使用墨卡托投影,因此您需要将其用于转换.在墨卡托投影下,像素以米为单位的距离随纬度而变化,因此虽然距离地球半径的距离非常小,但纬度很重要.
以下所有示例都是javascript,因此您可能需要翻译它们.
以下是坐标系的一般说明:
http://code.google.com/apis/maps/documentation/javascript/maptypes.html#WorldCoordinates
此示例包含MercatorProjection对象,其中包含fromLatLngToPoint()和fromPointToLatLng()方法:
http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html
将(x,y)转换为(lat,lon)后,绘制圆形的方法如下:
// Pseudo code
var d = radius/6378800; // 6378800 is Earth radius in meters
var lat1 = (PI/180)* centerLat;
var lng1 = (PI/180)* centerLng;
// Go around a circle from 0 to 360 degrees, every 10 degrees
for (var a = 0 ; a < 361 ; a+=10 ) {
var tc = (PI/180)*a;
var y = asin(sin(lat1)*cos(d)+cos(lat1)*sin(d)*cos(tc));
var dlng = atan2(sin(tc)*sin(d)*cos(lat1),cos(d)-sin(lat1)*sin(y));
var x = ((lng1-dlng+PI) % (2*PI)) - PI ;
var lat = y*(180/PI);
var lon = x*(180/PI);
// Convert the lat and lon to pixel (x,y)
}
Run Code Online (Sandbox Code Playgroud)
这两个mashup在地球表面绘制一个给定半径的圆:
http://maps.forum.nu/gm_sensitive_circle2.html
http://maps.forum.nu/gm_drag_polygon.html
如果您选择忽略投影,那么您将使用笛卡尔坐标并使用毕达哥拉斯定理绘制圆圈:
http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates
看一下api 中的Projection对象.它有一个名为metersToEquatorPixels()的方法.鉴于api中的描述,它可能只是在赤道上准确,但我认为值得一提,以防准确性对你来说不是问题.
这是在叠加层的绘制方法中使用此方法的方法,给定以米为单位的半径以及要绘制圆的位置的纬度和经度:
Projection projection = mapView.getProjection();
Point center = projection.toPixels(new GeoPoint(yourLat * E6, yourLong * E6), null);
float radius = projection.metersToEquatorPixels(radiusInMeters);
canvas.draw(center.x, center.y, radius, new Paint());
Run Code Online (Sandbox Code Playgroud)
您必须问的三个问题 1- 您的地图有多大 2- 您的缩放级别是多少 3- 您的屏幕有多大
让我们假设地图与屏幕具有相同的纵横比(如果不是,那么您需要担心以哪种方式裁剪(垂直与水平)或以哪种方式拉伸,然后将我们的答案更改为 1)
一旦你有了答案 1 和 3,你就可以计算出 100% 缩放情况下米与像素之间的比率,这样你就可以得到每米的像素
接下来你需要维护一个缩放系数(例如:放大两倍大小为200%)
你对画圆的调用将如下所示
Canvas.draw(x,y, radius_in_meters * pixels_per_meter * zoom_factor/100);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16136 次 |
| 最近记录: |