圆形叠加到地图距离转换

时间:2014-12-16 18:46:39

标签: android osmdroid

我有一个圆形叠加层,可以根据用户的喜好进行更改:它们有一个半径为“r' r'他们周围和滑块可以改变' r'因此。到目前为止它运作良好。

我的问题是我不知道如何从圆的半径到地图的指标找到正确的转换。例如,给定下面的圆圈,给定半径覆盖的距离是多少?

enter image description here

3 个答案:

答案 0 :(得分:1)

您必须知道所显示地图的比例是多少。拥有地图比例,您可以将像素大小转换为公制尺寸。有了这个比例,您可以将以像素为单位的半径转换为米。

答案 1 :(得分:0)

所以这就是你如何做到的......

Jedil是正确的你必须找到你的屏幕宽度/高度,屏幕的dpi等等。所以,通过this old osmdroid来源查看我发现了这个:

//Get the x and y dpi
this.xdpi = this.context.getResources().getDisplayMetrics().xdpi;
this.ydpi = this.context.getResources().getDisplayMetrics().ydpi;

//Get the screen width/height
this.screenWidth = this.context.getResources().getDisplayMetrics().widthPixels;
this.screenHeight = this.context.getResources().getDisplayMetrics().heightPixels;

 // DPI corrections for specific models
 String manufacturer = null;
 try {
       final Field field = android.os.Build.class.getField("MANUFACTURER");
        manufacturer = (String) field.get(null);
      } catch (final Exception ignore) {
      }

 if ("motorola".equals(manufacturer) && "DROIDX".equals(android.os.Build.MODEL)) {

 // If the screen is rotated, flip the x and y dpi values
 WindowManager windowManager = (WindowManager) this.context
                       .getSystemService(Context.WINDOW_SERVICE);
 if (windowManager.getDefaultDisplay().getOrientation() > 0) {
     this.xdpi = (float) (this.screenWidth / 3.75);
     this.ydpi = (float) (this.screenHeight / 2.1);
 } else {
       this.xdpi = (float) (this.screenWidth / 2.1);
       this.ydpi = (float) (this.screenHeight / 3.75);
 }

 } else if ("motorola".equals(manufacturer) && "Droid".equals(android.os.Build.MODEL)) {
     // http://www.mail-archive.com/android-developers@googlegroups.com/msg109497.html
     this.xdpi = 264;
     this.ydpi = 264;
 }

 // set default max length to 1 inch
 maxLength = 2.54f;

这就是你如何获得常数' (至少在设备的眼中)。转换......

 // calculate dots per centimeter
 int xdpcm = (int) ((float) xdpi / 2.54);
 int ydpcm = (int) ((float) ydpi / 2.54);

 // get length in pixel
 int xLen = (int) (maxLength * xdpcm);
 int yLen = (int) (maxLength * ydpcm);


 // Two points, xLen apart, at scale bar screen location
 IGeoPoint p1 = projection.fromPixels((screenWidth / 2) - (xLen / 2), yOffset);
 IGeoPoint p2 = projection.fromPixels((screenWidth / 2) + (xLen / 2), yOffset);

 // get distance in meters between points
 final int xMeters = ((GeoPoint) p1).distanceTo(p2);

......如果不是几乎完全相同的话,那就得到yMeters(提示:使用screenHeight)。

不可否认,我并不完全确定IGeoPoint...行正在做什么,但我意识到转换是存在的。希望这有助于将来的人。为了更好地理解上述代码,请参阅我发布的链接。

答案 2 :(得分:0)

我认为你要找的是一个米到像素的计算。你可以从投影中得到这个:

// Get projection
Projection proj = mMapView.getProjection();
// How many pixels in 100 meters for this zoom level
float pixels = proj.metersToPixels(100);
// How many meters in 100 pixels for this zoom level
float meters = 1 / proj.metersToPixels(1 / 100);
// You could also get a raw meters-per-pixels value by using TileSystem.GroundResolution()

要记住两件事 - 此值不仅会根据缩放级别而是根据您在地图上的纬度而变化。