Make polygons perpendicular to bearing in google maps android

三世轮回 提交于 2019-12-11 14:57:03

问题


I'm creating polygons on a map

 getMap().addPolygon(
            getPolygonOptions(point1, point2, widthInMeters)
                    .fillColor(Color.YELLOW));

The position of the corners is calculated by:

public static PolygonOptions getPolygonOptions(LatLng point1, LatLng point2, double widthInMeters) {


    double distance  = SphericalUtil.computeDistanceBetween(point1, point2) + 6;

   float bearing = location.getBearing();

    double bears = bearing;


    LatLng corner1 = SphericalUtil.computeOffset(point2, widthInMeters / 2, bears + 90);
    LatLng corner2 = SphericalUtil.computeOffset(point2, widthInMeters / 2, bears - 90);
    LatLng corner3 = SphericalUtil.computeOffset(corner2, distance, bears);
    LatLng corner4 = SphericalUtil.computeOffset(corner1, distance, bears);

    return new PolygonOptions().add(corner1, corner2, corner3, corner4);
}

The intent was for the polygons to be perpendicular to the bearing. Certain directions on the map the polygons are nearly that way, but as you can see in this picture, in some directions the polygons end up slanted compared to the current bearing. The current bearing is shown by the blue arrow and the magenta polyline. Can anyone identify whats wrong with my corner calculations?


回答1:


It may be problem of units which are returning from two functions.

Here, SphericalUtil.computeDistanceBetween(point1, point2) returns the values in meters.

and

Google's location documentation suggests,

location.getBearing() returns values in degrees.

Get the bearing, in degrees. Bearing is the horizontal direction of travel of this device, and is not related to the device orientation. It is guaranteed to be in the range (0.0, 360.0] if the device has a bearing.

If this location does not have a bearing then 0.0 is returned.

Please, check whether the location is heaving bearing pr not before using location.getBearing()

So, you have convert those degrees into meters and then implement it in your logic. It will solve your issue.



来源:https://stackoverflow.com/questions/22585189/make-polygons-perpendicular-to-bearing-in-google-maps-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!