Android Google Maps: how to get the area that is currently displayed on the device screen?

Is there a way to get the coordinates of the current area that is displayed on the device?

Suppose we want to show "nearby" places that are stored in our own database. So, let's say the user watches the following map clip:

enter image description here

How to get the longitude / latitude of the screen (or the dot in the middle of the screen and the radius that covers everything?). Please keep in mind that the center of the map is usually not the current position, as the user can move the center of the map!

+7
source share
3 answers

Use map.getProjection().getVisibleRegion() . From VisibleRegion you can get LatLngBounds , which is easy to work with. You can also try directly with a region that can be trapezoidal.

+21
source

I found a solution for the Google Map API v2 from several answers:

https: //stackoverflow.com/questions/794954/...

So, you need to execute the Activity from the GoogleMap.OnCameraChangeListener interface

     private static final int REQUEST_CODE_GOOGLE_PLAY_SERVECES_ERROR = -1;
     private static final double EARTH_RADIOUS = 3958.75;  // Earth radius;
     private static final int METER_CONVERSION = 1609;
     private GoogleMap mGoogleMap;

     @Override
     protected void onCreate (Bundle savedInstanceState)
     {
         super.onCreate (savedInstanceState);
         setContentView (R.layout.activity_layout);

         int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable (mContext);
         if (status! = ConnectionResult.SUCCESS)
         {
             Dialog dialog = GooglePlayServicesUtil.getErrorDialog (status, activity,
                     REQUEST_CODE_GOOGLE_PLAY_SERVECES_ERROR);
             dialog.show ();
             mGoogleMap = null;
         }
         else
         {
             mGoogleMap = ((SupportMapFragment) getFragmentManager (). findFragmentById (
                     R.id.fragment_shops_layout_maps_fragment)). GetMap ();

             mGoogleMap.setOnCameraChangeListener (this);
         }
     }

A listener that works when scaling a map. Define as LatLng the positions of the lower left, lower right, upper left, and upper right edges of the map that are displayed on the screen. For the most part of the screen and two points, we can get the radius from the center of the map.

  @Override
     public void onCameraChange (CameraPosition cameraPosition)
     {
         // Listener of zooming;
         float zoomLevel = cameraPosition.zoom;
         VisibleRegion visibleRegion = mGoogleMap.getProjection (). GetVisibleRegion ();
         LatLng nearLeft = visibleRegion.nearLeft;
         LatLng nearRight = visibleRegion.nearRight;
         LatLng farLeft = visibleRegion.farLeft;
         LatLng farRight = visibleRegion.farRight;
         double dist_w = distanceFrom (nearLeft.latitude, nearLeft.longitude, nearRight.latitude, nearRight.longitude);
         double dist_h = distanceFrom (farLeft.latitude, farLeft.longitude, farRight.latitude, farRight.longitude);
         Log.d ("DISTANCE:", "DISTANCE WIDTH:" + dist_w + "DISTANCE HEIGHT:" + dist_h);
     }

The return distance between 2 points, stored as 2 pairs in meters;

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

If you want to get the radius of the area shown on the screen, you just need to divide by 2.

I hope this will be helpful!

+7
source

This calculates the radio in km depending on the width of the map:

 public double calculateVisibleRadius() { float[] distanceWidth = new float[1]; VisibleRegion visibleRegion = map.getProjection().getVisibleRegion(); LatLng farRight = visibleRegion.farRight; LatLng farLeft = visibleRegion.farLeft; LatLng nearRight = visibleRegion.nearRight; LatLng nearLeft = visibleRegion.nearLeft; //calculate the distance between left <-> right of map on screen Location.distanceBetween( (farLeft.latitude + nearLeft.latitude) / 2, farLeft.longitude, (farRight.latitude + nearRight.latitude) / 2, farRight.longitude, distanceWidth ); // visible radius is / 2 and /1000 in Km: return distanceWidth[0] / 2 / 1000 ; } 
0
source

All Articles