android - 将屏幕尺寸(像素)转换为纬度和经度

标签 android eclipse web-services google-maps

好的,所以我正在尝试编写一个代码,根据当前可查看区域返回 map 所有 4 个角位置的值。例如,用户通过他的设备访问谷歌地图,他关注一个特定区域并点击一个按钮并返回所有四个角的位置(纬度和经度)。

为此,我有中心坐标,现在我想要的是当前可视区域的跨度,并获得左上角和右下角坐标。为此,我使用了

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x; // returns 480
int height = size.y; // returns 800

现在我想将它们转换为纬度和经度值,以便我可以使用简单的数学运算,例如:

topLeftLat= centerLat + lat/2;
topLeftLon= centerLon - lng/2;
bottomRightLat= centerLat - lat/2;
bottomRightLon = CenterLon + lng/2;

这会给我角坐标。 lat 和 lng 是经度和纬度值转换后的屏幕分辨率。

最佳答案

这在谷歌上搜索了 5 分钟,所以我不知道我为什么要给你这个...学习研究是成为开发人员最重要的部分。

GeoPoint mapCenter = myMap.getMapCenter();

   float centerLatitude = (float)(mapCenter.getLatitudeE6())/1000000;
   float centerLongitude = (float)(mapCenter.getLongitudeE6())/1000000;

   float latitudeSpan = (float)(myMap.getLatitudeSpan())/1000000;
   float longitudeSpan = (float)(myMap.getLongitudeSpan()/1000000);

   GeoPoint mapTopLeft = myMap.getProjection().fromPixels(0, 0);
   float topLatitude = (float)(mapTopLeft.getLatitudeE6())/1000000;
   float leftLongitude = (float)(mapTopLeft.getLongitudeE6())/1000000;

   GeoPoint mapBottomRight
   = myMap.getProjection().fromPixels(myMap.getWidth(), myMap.getHeight());
   float bottomLatitude = (float)(mapBottomRight.getLatitudeE6())/1000000;
   float rightLongitude = (float)(mapBottomRight.getLongitudeE6())/1000000;

String info =
     "Center: " + centerLatitude + "/" + centerLongitude + "\n"
     + "Top Left: " + topLatitude + "/" + leftLongitude + "\n"
     + "Bottom Right: " + bottomLatitude + "/" + rightLongitude + "\n"
     + "latitudeSpan: " + latitudeSpan + "\n"
     + "longitudeSpan: " + longitudeSpan + "\n";

   Toast.makeText(AndroidMapActivity.this,
     info,
     Toast.LENGTH_LONG).show();
  }};

完整教程可在 Android-er: Get the the coordinates (Latitude and Longitude) currently displayed on MapView 获得

关于android - 将屏幕尺寸(像素)转换为纬度和经度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11019965/

相关文章:

c - 使用 C 服务器通过互联网发送大文件(图像)

Python:Django 请求

c# - 将 Web 服务分离到更多文件中

android - APKLIB 未安装在 Maven Repo 中

android - 摄像头 2 : Unable to record video in full screen?

android - 无法完全从 Json 获取数据

android - 如何在 <string> XML (eclipse/android) 中创建一个新行或标签?

java - Android 初学者 : java. lang.NullPointerException

java - 如何使用 adb 从多个连接的设备卸载 APK?

java - 在 Eclipse 中粘贴多行 Java 字符串