微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android-GroundOverlay图片未旋转

关于将图像放置在具有边界的地图上,Google Maps的文档指出:

When the image is drawn on the map it will be rotated to fit the bounds. If the bounds do not match the original aspect ratio, the image will be skewed.

但是在我的代码我有这个:

    GroundOverlayOptions goo2 = new GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromresource(imgRes))
            .positionFromBounds(new LatLngBounds(
                    new LatLng(44.415, 8.937),
                    new LatLng(44.42, 8.942)
            ));
    mMap.addGroundOverlay(goo2);

图像不旋转,但始终向北笔直绘制并倾斜.更改覆盖角只会更倾斜,并且永远不会旋转一点.

如何设置GMaps来旋转它,或者是否存在任何外部工具来旋转它?

解决方法:

我自己解决了.
我认为Google的文档不是很清楚.它指出可以使用两种可能的方法来定位GroundOverlay:

There are two ways to specify the position of the ground overlay:
Using a LatLng to center the overlay, and dimensions in meters to specify the size of the image.
Using a LatLngBounds to specify the north east and south west corners of the image.
You must specify the position of the ground overlay before it is added to the map.

我正在使用第二种方法,或者给它两个我想应用的图像的地理位置.尚不清楚的是,使用第二种方法无法旋转,并且比例上的每个差异都将以偏斜的图像呈现.实际上,我的想法恰恰是,给定一个平原上的两个点,我们恰好有一个图像可以同时适合这两个点而不会产生裂痕,但是只是旋转,我很失望地发现它不起作用.

但是,如果您有足够的耐心来按自己的尺寸(以米为单位)和图像的旋转度进行计算,则可以确定图像的一个角或中心,然后使用第一种方法(使用LatLng)并明确指示锚点,例如(0,0)代表左上角,(0.5,0.5)代表中心,以米为单位的图片高度(这是找到它的好方法How to convert latitude or longitude to meters?),使用三角函数找到旋转,就是这样.

找到以像素为单位的图像大小的方法表示为here.

那是我最终的内存代码

        BitmapFactory.Options dimensions = new BitmapFactory.Options();
        dimensions.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(getResources(), imgRes, dimensions);
        int imgHeightPixels = dimensions.outHeight;

        GroundOverlayOptions goo = new GroundOverlayOptions()
            .image(BitmapDescriptorFactory.fromresource(imgRes))
            .position(imgBottomLeftCorner, imgHeightInPixels)
            .anchor(0, 1)
            .bearing(imgRotation);
        mMap.addGroundOverlay(goo);

请注意,地图上的Y坐标从下向上生长,而图像上的Y坐标从上向下生长,因此锚点为(0,1).

给定两个向量,这是我做的要找到它们的角度,然后是图像旋转的操作(使用由图像上由两点坐标构成的向量与由相同两点地理坐标构成的向量):

private static double calcdistance(double x1, double y1, double x2, double y2) {
    return (Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1)));
}
private static double calcVectorRotation(double x1, double y1, double x2, double y2) {
    return Math.asin( (y1-y2) / calcdistance(x1, y1, x2, y2) );
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐