Search code examples
androidgoogle-mapsgoogle-maps-android-api-2mercator

LatLang value of the physical center in the visible GoogleMap region considering mercator projection


I want to calculate the center and radius of the circle in the middle of the screen with the radius being half of the width of the visible region.

visibleRegion.latLngBounds.getCenter()

or

cameraPosition.target

values for the center has an offset from the physical center when I zoom out a lot.

in the following picture I calculated the black shaded circle as follow:

   VisibleRegion visibleRegion = mMap.getProjection().getVisibleRegion();
   LatLng nearLeft = visibleRegion.nearLeft;
   LatLng nearRight = visibleRegion.nearRight;
   mCircle.setCenter(visibleRegion.latLngBounds.getCenter());
   mCircle.setRadius(distanceFrom(nearLeft.latitude, nearLeft.longitude, nearRight.latitude, nearRight.longitude) / 4);




 public double distanceFrom(double lat1, double lng1, double lat2, double lng2)
    {
        // Return distance between 2 points, stored as 2 pair location;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLng = Math.toRadians(lng2 - lng1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2) * Math.sin(dLng / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double dist = EARTH_RADIOUS * c;
        return new Double(dist * METER_CONVERSION).floatValue();
    }

Im looking for the radius and center of the green circle.enter image description here


Solution

  • For anyone who's interested I solved it this way: I calculate the pixel position of the center and also a pixel position of a point on the circle at the start using GoolgeMap.getProjection(). Now as the camera changes I draw the circle centered at that pixel position by convverting the Point() back to LatLang and with the radius equals to the distance between the center point and the point on the circle in meter.