android - 如何将 1 米更改为像素距离?

标签 android

我在开发Android map 应用时,想在 map 上画一个半径为1米的圆。如您所知,我不能直接绘制 1 米,我应该根据缩放级别将 1 米转换为两个像素的距离。我如何转换它,是否有任何我可以使用的 API。

Canvas.draw(x, y, radius),我应该给这个方法赋什么值?

最佳答案

假设您的 map 是 Google map ,他们使用墨卡托投影,因此您需要使用它进行转换。 在墨卡托投影下,一个像素以米表示的距离随纬度而变化,因此虽然一米与地球半径相比是一个非常小的距离,但纬度很重要。

以下所有示例都是 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) 
}

这两个混搭在地球表面绘制了一个给定半径的圆:

http://maps.forum.nu/gm_sensitive_circle2.html

http://maps.forum.nu/gm_drag_polygon.html

如果您选择忽略投影,那么您将使用笛卡尔坐标并使用毕达哥拉斯定理简单地绘制圆:

http://en.wikipedia.org/wiki/Circle#Cartesian_coordinates

关于android - 如何将 1 米更改为像素距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3385117/

相关文章:

android - Toast.LENGTH_LONG 和 Toast.LENGTH_SHORT 的值是多少?

java - FIrebaseUI(版本 0.4.0): firebase recycler Adapter not fetching data from firebase database

android - 使用 support-vector-drawable 时的 Resources$NotFoundException

android - Cordova CLI "cordova build android"错误 "Does not appear to be an xcode project, no xcode project file in..."?

android - 获取 java.lang.ExceptionInInitializerError 和 java.lang.UnsatisfiedLinkError

android - Kotlin Coroutine ViewModel UnitTesting不涵盖ViewModel

android - 无法在 VS2015 Android 模拟器中安装 android 设备

java - 如何在 Java 中访问和使用 PC Direct WiFi?

android - 获取错误 JSONException : No Value For

android - 我在设备错误上没有剩余空间