【问题标题】:How to: plot a given latitude and longitude into still image map using four (4) coordinates of the image?如何:使用图像的四 (4) 个坐标将给定的纬度和经度绘制到静止图像地图中?
【发布时间】:2011-09-15 00:17:44
【问题描述】:

我创建了这篇文章 -> plot a real world lat lon into different angle still image map

据此,我可以在给定两 (2) 个坐标(左上、右下)的情况下成功标记给定的纬度和经度,但由于静止图像地图的角度与现实世界地图角度相比不正确,我的标记被移位了。

现在,我正在考虑使用图像的四 (4) 个坐标(左上、左下、右上、右下)。因此,我可以在不考虑角度的情况下绘制给定的 lat&lon。

我认为,即使没有 Android 经验也可以回答这个问题。我只是在数学问题上有点慢。

有可能实现吗?如果是,任何指导和代码 sn-ps 表示赞赏。

更新1 主要目标是将给定的纬度和经度标记为与现实世界地图具有不同角度的图像地图。

更新2 我正在使用以下代码来计算我的角度。你会检查它是否可靠地获得角度。然后将其转换为像素。 注意:此代码仅使用图像的两个坐标加上目标坐标。

public static double[] calc_xy (double imageSize, Location target, Location upperLeft, Location upperRight) {
    double newAngle = -1;
    try {
        double angle = calc_radian(upperRight.getLongitude(), upperRight.getLatitude(), 
                upperLeft.getLongitude(), upperLeft.getLatitude(), 
                target.getLongitude(), target.getLatitude());
        newAngle = 180-angle;

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    double upperLeft_Target_dist = upperLeft.distanceTo(target);

    double upperLeft_Right_dist = upperLeft.distanceTo(upperRight);
    double distancePerPx = imageSize /upperLeft_Right_dist;

    double distance = upperLeft_Target_dist * distancePerPx;

    double radian = newAngle * Math.PI/180;

    double[] result = radToPixel(distance, radian);
    return result;
}

public static double[] radToPixel(double distance, double radian) {
    double[] result = {-1,-1};
    result[Functions.Location.PIXEL_X_LON] = distance * Math.cos(radian);
    result[Functions.Location.PIXEL_Y_LAT] = distance * Math.sin(radian);
    return result;
}

public static double calc_radian(Double x1, Double y1, Double x2, Double y2, Double x3, Double y3)
    throws Exception{

    double rad = 0.0;

    if((Double.compare(x1, x2) == 0 && Double.compare(y1, y2) == 0) ||
            (Double.compare(x3, x2) == 0 && Double.compare(y3, y2) == 0))
    {
        Log.d(tag, "Same place") ;
        return rad;
    }

    /* compute vector */
    double BAx = x2 - x1;
    double BAy = y2 - y1;

    double BCx = x3 - x2;
    double BCy = y3 - y2;

    double cosA =  BAx / Math.sqrt( BAx * BAx + BAy * BAy ) ;
    double cosC =  BCx / Math.sqrt( BCx * BCx + BCy * BCy ) ;

    double radA = Math.acos( cosA ) * 180.0 / Math.PI ;
    double radC = Math.acos( cosC ) * 180.0 / Math.PI ;
    if( BAy < 0.0 )
    {
        radA = radA * -1.0 ;
    }
    if( BCy < 0.0 )
    {
        radC = radC * -1.0 ;
    }

    rad = radC - radA ;


    if( rad > 180.0 )
    {
        rad = rad - 360;
    }

    if( rad < -180.0 )
    {
        rad = rad + 360;
    }

    return rad ;
}

【问题讨论】:

  • 看起来角度可能是恒定的。如果是这种情况,您可以简单地对每个纬度/经度点应用变换。
  • 实际上,我认为保持点静止并旋转图像可能会更好。您可以将其旋转到与手持设备的方向一致。
  • @John J Smith ,1) 角度是恒定的。你能帮我应用纬度/经度点的变换吗? 2)轮换方法似乎是最好的方法。如果我们不能转换纬度/经度,我会试试这个……看来这些是我帖子的答案。你会把它作为答案发布吗?这样,人们就可以投票了。
  • 有人使用自定义地图让这些方法正常工作吗?

标签: android map geolocation location coordinates


【解决方案1】:

这看起来像是您想在建筑物或校园的图像上绘制用户的当前地理位置。假设这一点,我的方法是将静止图像“映射”到屏幕上,这可能需要平移变换、旋转变换和缩放变换。此外,您需要知道图像上至少两个点的实际地理位置坐标。鉴于您上一篇文章中的图片,我假设您拥有左下角和右下角的地理坐标。您已经拥有将地理坐标转换为屏幕坐标的信息,因此可以绘制图像,使图像的左下角与您计算的像素坐标相匹配。我将把这个点称为你的锚点。

在这个阶段,您可能有一个角在正确位置的图像,但现在它需要按比例缩小或放大,然后围绕您的锚点旋转。您可以从 mapView 获取当前缩放级别,也可以获取 latitudeSpan,然后计算要应用于图像的比例因子。

最后,如果你有图像两个角的地理坐标,你可以计算出图像应该旋转的角度。这可以使用毕达哥拉斯来计算,或者您可以将笛卡尔坐标转换为极坐标see here。此计算不必由您的应用程序完成 - 它可以单独计算并作为常数输入。现在您可以在固定锚点周围应用旋转变换。

您可能还想使用方便的内置函数,例如采用像素坐标的 mapController.zoomInFixing() 或其他 zoomTo() 或 animateTo() 函数之一。

编辑:如果您不使用地图视图来管理您的地理坐标,那么您可以使用如下代码应用图像转换:

// create a matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate(angle);

// recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, 
                      width, height, matrix, true); 

    // make a Drawable from Bitmap to allow to set the BitMap 
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    ImageView imageView = new ImageView(this);

    // set the Drawable on the ImageView
    imageView.setImageDrawable(bmd);

编辑:使用左上角和右下角坐标,可以计算出如下角度:

角度 = sin-1((right.x - left.y)/sqrt((right.x - left.x)sq + (right.y - left.y)sq))

[其中 sqrt = 平方根;平方=平方

【讨论】:

  • 我没有使用任何 MapController 或 MapView,而是使用简单的 ImageSwitcher 和 ImageView。所以我需要手动计算。
  • 还有其他想法吗?你会提供样品吗?
  • 如果你有图片两点的地理位置坐标,就可以计算出角度。
  • (1)两点是什么? (2) 如何计算图像两点的角度?
  • 图像上的任意两个点 - 理想情况下是左下角和右下角的地理位置坐标。然后你可以使用毕达哥拉斯来计算角度。如果你有这两个坐标,我会帮你计算的。
【解决方案2】:

如果你知道角度是多少,那就是轴的简单笛卡尔旋转。

x 为旧经度, y是老纬度, b 是角度

新的经度 x' = x*cos(b) - y*sin(b)

新的纬度y' = x*sin(b) + y*cos(b)

【讨论】:

    【解决方案3】:

    我不确定我是否理解,但在我看来,您想使用两个点计算您想要的图像角度,然后旋转它并根据点 a 和 b 之间的像素数调整它的大小(两个corners) 使用lat lon 转换为pixels 的方法和距离公式

    【讨论】:

    • >想要计算角度 -> 是的,但我正在考虑使用 4 个坐标(左上、左下、右上、右下)计算它的替代解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多