Android groundoverlay picture not rotated

About placing images on maps with boundaries, the Google Maps document states:

But in my code, I have this:

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

The image does not rotate, but is always drawn straight north and tilted. Changing the coverage angle will only tilt more and will never rotate a little

How to set GMAPs to rotate it, or is there any external tool to rotate it?

resolvent:

I solved it myself. I don't think Google's document is very clear. It points out that there are two possible ways to locate groundoverlay:

I'm using the second method, or give it the geographical location of the two images I want to apply. It's not clear that the second method can't rotate, and every difference in scale will be presented as a skewed image. In fact, my idea is that given two points on a plain, we happen to have an image that can fit both points without cracks, But it was just spinning, and I was disappointed to find that it didn't work

However, if you have enough patience to calculate according to your own size (in meters) and the rotation of the image, you can determine an angle or center of the image, then use the first method (using latlng) and clearly indicate the anchor point, for example, (0,0) represents the upper left corner, (0.5,0.5) represents the center and the image height in meters (this is a good way to find it: how to convert latitude or longitude to meters?) use trigonometric function to find rotation, that's it

The method to find the image size in pixels is expressed as here

That's my final memory code:

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

Note that the Y coordinate on the map grows from bottom to top, while the Y coordinate on the image grows from top to bottom, so the anchor point is (0,1)

Given two vectors, this is what I do to find their angles, and then the operation of image rotation (using the vector composed of two-point coordinates on the image and the vector composed of the same two-point geographic coordinates):

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>