如何:使用图像的四(4)个坐标将给定的经度和经度绘制成静止图像地图?

时间:2011-09-15 00:17:45

标签: android map geolocation location coordinates

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

从那里开始,我可以成功地给出两个(2)坐标(左上角,右下角)给出的lat& lon,但是由于静止图像地图的角度与真实世界地图角度相比不正确,我的标记被取代了

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

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

有可能实施吗?如果是的话,任何指导和感谢代码片段。

UPDATES1 主要目标是将给定的lat& lon标记为与真实世界地图具有不同角度的图像地图。

UPDATES2 我使用以下代码来计算我的角度。如果获得角度可靠,你会检查它吗?然后将其转换为像素。 注意:此代码仅使用图像的两个坐标加上目标坐标。

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 ;
}

3 个答案:

答案 0 :(得分:1)

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

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

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

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

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

// 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);

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

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

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

答案 1 :(得分:1)

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

x成为旧的经度, y是旧纬度, 和b是角度

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

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

答案 2 :(得分:0)

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