4

I'm struggling how to make a move with the camera to fit my two markers and keep them on the same level. So this implies modify zoom to fit them and turn camera to fit the markers on the same line.

The next two pics will clarify my question:

Current stateDesired state

So, what I done so far is this:

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 make the two markers fit on the screen, so I need to know how to rotate the camera with the correct angle to get the last pic.

Can anyone help me on this?

Thanks!

Marcel
  • 2,094
  • 3
  • 23
  • 37

4 Answers4

10

I am not sure how much i am correct but i am just sharing my idea.

  1. Find the middle point(latitude longitude) of two marker

    private LatLng midPoint(double lat1, double long1, double lat2,double long2)
    {
    
    return new LatLng((lat1+lat2)/2, (long1+long2)/2);
    
    }
    
  2. Calculate angle between middle point and any other marker position.

    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;
    }
    
  3. Rotate map to that angle.

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

M D
  • 47,665
  • 9
  • 93
  • 114
Md. Monsur Hossain Tonmoy
  • 11,045
  • 2
  • 22
  • 19
  • This solution is worked for me. I did some changes to the above code but I managed to attach two locations to the Camera position target. – gsm Nov 13 '20 at 03:52
  • https://stackoverflow.com/a/64815270/8168140 solution – gsm Nov 13 '20 at 04:09
5

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

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 retrieve the angle:

float xDiff = x2 - x1;
float yDiff = y2 - y1;
return Math.Atan2(yDiff, xDiff) * 180.0 / Math.PI;
Léon Pelletier
  • 2,701
  • 2
  • 40
  • 67
1

You can also find the bearing of two points using Location.distanceBetween. This method returns both initial and final bearing. You can find what is the difference between them here

Mojo Risin
  • 8,136
  • 5
  • 45
  • 58
1

Camara movement part CameraPosition

CameraPosition cPosition = CameraPosition(
          zoom: 8,
          // tilt: 90,
          // bearing: -30,
          target: midPoint(_point1.latitude, _point1.longitude, _point2.latitude,
              _point2.longitude),
        );

Target CameraPosition

LatLng midPoint(double lat1, double long1, double lat2, double long2) {
    return new LatLng((lat1 + lat2) / 2, (long1 + long2) / 2);
  }

After that add to map controller

controller.animateCamera(CameraUpdate.newCameraPosition(cPosition));
gsm
  • 2,348
  • 17
  • 16