27

I am using Google Maps v2 in my application. When the user pans or zooms on the screen I would like to get the area of the map based on which I want to fetch the POI only in the screen view part.
I went through the documentation but could not find any help.

JJD
  • 50,076
  • 60
  • 203
  • 339
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

2 Answers2

61

You need to use Projection and VisibleRegion classes in order to get visible LatLng region.
So your code would look something like:

LatLngBounds curScreen = googleMap.getProjection()
    .getVisibleRegion().latLngBounds;
JJD
  • 50,076
  • 60
  • 203
  • 339
Pavel Dudka
  • 20,754
  • 7
  • 70
  • 83
  • i tried this i am getting only northwest and southwest value. any idea how to get the other two values ? – Harsha M V Feb 05 '13 at 05:57
  • 2
    northeast and southwest you mean? If so - that's how the bounding box is represented (top-right and bottom-left corners of the viewport). Given these two points it is pretty straight forward to calculate other 2 points. Right? The simplest way to check whether certain POI(LatLng) is within the box - is to call `LatLngBounds.contains(LatLng point)` interface. However, displaying big amount of Markers on a map is a separate story – Pavel Dudka Feb 05 '13 at 08:17
  • how can i get get the other two points, am sorry am confused. Ant about showin a lot of markers is there any optimized way of doing that ? – Harsha M V Feb 05 '13 at 08:22
  • 6
    well, top-left corner would have `[curScreen.southwest.longitude;curScreen.northeast.latitude]` coordinates and bottom-right corner would have `[curScreen.northeast.longitude;curScreen.southwest.latitude]` coordinates. You can treat latitude as `Y` axis and longitude as `X` axis – Pavel Dudka Feb 05 '13 at 08:34
  • as far as I know, there is no native way to clusterize markers on a map. There is a good article though about this topic: https://developers.google.com/maps/articles/toomanymarkers – Pavel Dudka Feb 05 '13 at 08:34
  • @HarshaMV your question seem doesn't signifies your reputations – stackunderflow Mar 05 '15 at 04:16
  • @Dagon see the date it was asked ;-) – Harsha M V Mar 05 '15 at 05:39
  • @HarshaMV i see. that was toooo long ago. is that the day when you're still at primary school? actually i refer to your question comment. anyway thank you for your post and also for the answer. it solved my today's problem – stackunderflow Mar 05 '15 at 05:45
  • It is returning 0.0 both latitude and longitude,any idea?? – kgandroid Jul 16 '15 at 05:55
  • @kgandroid usually it means that current location is not known at the moment – Pavel Dudka Jul 16 '15 at 06:12
  • 1
    Plase have a look: http://stackoverflow.com/questions/31446401/latlng-bound-returns-0-0 – kgandroid Jul 16 '15 at 06:25
1

In Android(Kotlin) you can find LatLng this way, in every time zoom or. gesture detect

private var mMap: GoogleMap? = null
..........



    mMap?.setOnCameraMoveStartedListener { reasonCode ->
        if (reasonCode == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) {
            val curScreen: LatLngBounds =  mMap!!.getProjection().getVisibleRegion().latLngBounds
            var northeast=curScreen.northeast
            var southwest=curScreen.southwest
            var center=curScreen.center
            Log.v("northeast LatLng","-:"+northeast)
            Log.v("southwest LatLng","-:"+southwest)
            Log.v("center LatLng","-:"+center)
        }
    }
Shirsh Shukla
  • 5,491
  • 3
  • 31
  • 44