【问题标题】:How to convert coordinates from lat/long to float parameter of canvas.drawCircle()?如何将坐标从 lat/long 转换为 canvas.drawCircle() 的 float 参数?
【发布时间】:2014-03-05 01:43:09
【问题描述】:

我需要将我在地图上看到的当前位置(比如 A)映射到我的应用程序上的另一个屏幕(比如没有地图背景的 B)。我在地图上绘制了我当前的位置,并且我知道纬度/经度值。

  Location loc = mProjInstance.locationManager.getLastKnownLocation(Constants.provider2);

给我坐标。

现在在屏幕B上,我一直在使用设备屏幕的中心

    int x = ((MapView)mParent).getWidth()/2;
    int y = ((MapView)mParent).getHeight()/2;

围绕中心 (x,y) 绘制两个同心圆。

现在我需要在 B 上绘制圆圈,而不是使用设备的中心, 但是使用上面提到的 getLastKnownLocation 获得的纬度/经度。

所以我提供了 x 和 y 这样的值

 Location loc1 = PROJ.getInstance().locationManager.getLastKnownLocation(Constants.provider2);  
            x = (int) loc1.getLatitude();
            y = (int) loc1.getLongitude();

          canvas.drawCircle( x, y,innerRadius, mSelectionBrush);
          canvas.drawCircle( x, y,outerRadius, mSelectionBrush);

但是当我尝试这个时,与 A 相比,“B”上的圆圈位于不同的位置。B 将中心绘制在屏幕的左上角,而 A 将其大致绘制在屏幕的中心,我当前的地理位置。

我的问题是如何将纬度/经度值转换为适合 canvas.drawCircle 的浮动参数。

我调试了这些值, 我在屏幕 A 中看到的 x 和 y 的值是 x=240,y=285 - 屏幕中心。 (这是正确的位置)

但是当我以当前(错误)方式进行转换时,对于屏幕 B x=19,y=72 - 朝向屏幕的左上角(不正确)

【问题讨论】:

  • 这完全没有意义。在南半球或西半球,x 或 y 将是负数。我看不出如何将负值传递给 draw 方法。
  • 尼克,我正在尝试围绕我当前的经纬度位置画一个圆圈。
  • 关于什么? - 也许是地图的叠加层?
  • 为什么不使用CircleCircleOptions 类?此外 loc.getLatitude() 返回一个浮点数,我猜想转换为 int 不是你想要的。
  • 那么您需要查看 getProjection 方法 developer.android.com/reference/com/google/android/gms/maps/… 以便将屏幕与地图坐标相关联

标签: android type-conversion android-canvas latitude-longitude geometry


【解决方案1】:

这是我用来在 GoogleMap (v1) 叠加层上显示准确性的叠加层的显着线条(很多省略)。您应该了解总体思路并能够将其适应 v2

public class AccuracyOverlay extends com.google.android.maps.Overlay {

    @Override
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow,
            long when) {

        super.draw(canvas, mapView, shadow);
        float accuracyRad = 100.0f; // 100 metres as an example
        Paint paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAlpha(16);// +lots more set() methods applied
        Point p1 = new Point();
        CommonPosition cp = CommonPosition.getInstance();
        // Last position
        GeoPoint lastFixGp = new GeoPoint(cp.getLatitudeE6(),
                    cp.getLongitudeE6());

        mapView.getProjection().toPixels(lastFixGp, p1);
        canvas.drawCircle(p1.x, p1.y, accuracyRad, paint);
        return false;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多