Google Maps v2 centers and rotates the camera according to two markers

I am afraid of how to make a movement with the camera to fit my two markers and keep them level. Thus, this implies zooming in to match them and rotating the camera to match markers on the same line.

The following two photos will clarify my question:

Current stateDesired state

So what I have done so far:

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

But this only makes the two markers suitable on the screen, so I need to know how to rotate the camera with the right angle to get the last picture.

Can someone help me with this?

Thank!

+3
source share
3 answers

In - almost - pseudo code, try something like this:

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

To get the angle:

float xDiff = x2 - x1;
float yDiff = y2 - y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
+4

, , .

  • ( )

    private LatLng midPoint(double lat1, double long1, double lat2,double long2)
    {
    
    return new LatLng((lat1+lat2)/2, (long1+long2)/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;
    }
    
  • .

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

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

+7

You can also find the bearing of two points using Location.distanceBetween . This method returns the starting and ending bearings. You can find the difference between them here.

+1
source

All Articles