Google Maps v2居中并转动相机位置以适合两个标记

Mar*_*cel 3 camera android google-maps google-maps-markers

我正在努力如何使用相机移动以适应我的两个标记,并将它们保持在同一水平.因此,这意味着修改缩放以适应它们并转动相机以适合同一行上的标记.

接下来的两张照片将澄清我的问题:

当前状态期望的状态

那么,到目前为止我所做的是:

public void centerIncidentRouteOnMap(List<LatLng> copiedPoints) {
        double minLat = Integer.MAX_VALUE;
        double maxLat = Integer.MIN_VALUE;
        double minLon = Integer.MAX_VALUE;
        double maxLon = Integer.MIN_VALUE;
        for (LatLng point : copiedPoints) {
            maxLat = Math.max(point.latitude, maxLat);
            minLat = Math.min(point.latitude, minLat);
            maxLon = Math.max(point.longitude, maxLon);
            minLon = Math.min(point.longitude, minLon);
        }
        final LatLngBounds bounds = new LatLngBounds.Builder().include(new LatLng(maxLat, maxLon)).include(new LatLng(minLat, minLon)).build();
        mMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 120));
}
Run Code Online (Sandbox Code Playgroud)

但这只会使两个标记适合屏幕,所以我需要知道如何以正确的角度旋转相机以获得最后一张照片.

谁可以帮我这个事?

谢谢!

Md.*_*moy 9

我不确定我有多正确,但我只是在分享我的想法.

  1. 找到两个标记的中点(纬度经度)

    private LatLng midPoint(double lat1, double long1, double lat2,double long2)
    {
    
    return new LatLng((lat1+lat2)/2, (long1+long2)/2);
    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 计算中点和任何其他标记位置之间的角度.

    private double angleBteweenCoordinate(double lat1, double long1, double lat2,
        double long2) {
    
    double dLon = (long2 - long1);
    
    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(dLon);
    
    double brng = Math.atan2(y, x);
    
    brng = Math.toDegrees(brng);
    brng = (brng + 360) % 360;
    brng = 360 - brng;
    
    return brng;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 将地图旋转到该角度.

    CameraPosition cameraPosition = new CameraPosition.Builder().target(midPoint(lat1,lng1,lat2,lng2)).zoom(14).bearing(angleBteweenCoordinate(lat1,lng1,lat2,lng2)).build();

    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));


Léo*_*ier 5

在几乎伪代码中,尝试类似的操作:

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(Your_target)      // Sets the center of the map
.zoom(YourZoom)                   // Sets the zoom
.bearing(Your_Angle)                // -90 = west, 90 = east
.tilt(Some_Tilt)      

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
Run Code Online (Sandbox Code Playgroud)

要检索角度:

float xDiff = x2 - x1;
float yDiff = y2 - y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
Run Code Online (Sandbox Code Playgroud)