Question

I'm trying to find the area which is visible on the screen. I can find bounds of the screen but i can't calculate the area in type of km square? Is there a possible way to calculate?

googleMap.setOnCameraChangeListener(new OnCameraChangeListener() {
        @Override
        public void onCameraChange(CameraPosition position) {
            LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds;
            getAreaInTheScreen(bounds);
        }

        private double getAreaInTheScreen(LatLngBounds bounds) {
                double northLat=bounds.northeast.latitude;
                double northLong=bounds.northeast.longitude;
                double southLat=bounds.southwest.latitude;
                double southLong=bounds.southwest.longitude;


                    /// it must return an area

                return area;
        }
    });
Était-ce utile?

La solution

Of course you can. you have all of the needed information. so first of all you should calculate distance from one point to another, let's say SouthWest and SouthEast. southWest coordinates (southLat,southLong) southEast coordinates (southLat, northLong), latitude stays unchanged.

Location l = new Location("southwest");
l.setLatitude(southLat);
l.setLongitude(southLong);
Location l2 = new Location("southeast");
l.setLatitude(southLat);
l.setLongitude(northLong);
double height = l.distanceTo(l2);

Same thing will go for the width of the rectangle, points will be SouthWest and NorthWest

southwest coordinates (southLat,southLong) northwest coordinates (northLat, southLong), longitude stays unchanged. we can use first location object "l" cause points are the same.

Location l3 = new Location("northwest");
l3.setLatitude(northLat);
l3.setLongitude(southLong);
double width = l.distanceTo(l3);

double area = width*height;

Sorry if there's some kind of syntax error, I was using the phone

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top