How to get latitude / longitude range in Google Map V2 for Android

I have a task to move my application to Google Maps Android API V2. Now I need to get the latitude / longitude range. I used MapView.getLatitudeSpan() and MapView.getLongitudeSpan() in previous versions of the API. Now I can not find something like this in V2.

Does anyone have the same problem?

+14
android google-maps google-maps-android-api-2
Dec 6 '12 at 7:45
source share
4 answers

You can use the following code to get the lat / lng range:

 VisibleRegion vr = mMap.getProjection().getVisibleRegion(); double left = vr.latLngBounds.southwest.longitude; double top = vr.latLngBounds.northeast.latitude; double right = vr.latLngBounds.northeast.longitude; double bottom = vr.latLngBounds.southwest.latitude; 

Hope this helps.

+24
Dec 09
source share

First get the projection using GoogleMap.getProjection() . You can then call Projection.getVisibleRegion() to get a VisibleRegion that has LatLngBounds.

The reason the LatitudeSpan and Longitude range no longer makes sense is because the map can now be rotated and tilted, and therefore the viewport is no longer a rectangle with latitude / longitude on the map.

+6
Dec 6
source share

This method works for me:

 CameraPosition camPos2 = mapa.getCameraPosition(); LatLng pos = camPos2.target; Toast.makeText(MainActivity.this,"Lat: " + pos.latitude + " - Lng: " +pos.longitude, Toast.LENGTH_LONG).show(); 

Strike>




Oops, I misunderstood the question, I mean that I did not see the word "span". According to the API, the correct one would be

First get grades:

 LatLngBounds bounds = gMap.getProjection().getVisibleRegion().latLngBounds; 

And then ask if any point is within the boundaries:

 LatLng point = new LatLng (latitude, longitude); if(bounds.contains(point)){ //do something } 
+3
May 13 '13 at 14:32
source share

Here is the answer

 LatLngBounds bounds = googleMap.getProjection().getVisibleRegion().latLngBounds; if (bounds.contains(ROMA)) { marker = googleMap.addMarker( new MarkerOptions() .position(ROMA) .title("Hello") .snippet("Nice Place") .icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)) ); System.out.println("Marker added"); } 

Add marker only when it falls into the visible area

+1
Mar 25 '13 at 10:01
source share



All Articles