Android Google Maps get Boundary Co-ordinates
Asked Answered
L

2

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.

Lanciform answered 5/2, 2013 at 5:1 Comment(0)
D
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;
Drews answered 5/2, 2013 at 5:27 Comment(11)
i tried this i am getting only northwest and southwest value. any idea how to get the other two values ?Lanciform
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 storyDrews
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 ?Lanciform
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 axisDrews
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: developers.google.com/maps/articles/toomanymarkersDrews
@HarshaMV your question seem doesn't signifies your reputationsAlphabetic
@Dagon see the date it was asked ;-)Lanciform
@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 problemAlphabetic
It is returning 0.0 both latitude and longitude,any idea??Gouache
@Gouache usually it means that current location is not known at the momentDrews
Plase have a look: https://mcmap.net/q/505573/-latlng-bound-returns-0-0Gouache
J
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)
        }
    }
Jozef answered 28/1, 2021 at 10:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.