14

I have a task for move my app to Google Maps Android APIs V2. Now, I need to get Latitude/Longitude span. I used MapView.getLatitudeSpan() and MapView.getLongitudeSpan() in previous version APIs. Now I can't find something like this in V2.

Does anybody have the same problem?

JJD
  • 50,076
  • 60
  • 203
  • 339
Rusfearuth
  • 3,261
  • 5
  • 28
  • 37

4 Answers4

25

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

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;

I hope this helps.

JJD
  • 50,076
  • 60
  • 203
  • 339
Paul Wein
  • 609
  • 5
  • 9
6

First obtain a Projection using GoogleMap.getProjection(). Then you can call Projection.getVisibleRegion() to obtain a VisibleRegion which has a LatLngBounds.

The reason why a LatitudeSpan and Longitude span no longer makes sense is because the map can now be rotated and tilted and so viewport is no longer a latitude/longitude aligned rectangle on the map.

Anthony
  • 4,720
  • 1
  • 22
  • 9
4

This way 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();


Oops, i misunderstood the question, i mean i did not saw "span" word. According the API the correct would be:

First get the bounds:

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

And then ask if any point is in bounds:

LatLng point = new LatLng (latitude, longitude);
if(bounds.contains(point)){
    //do something
}
Ricardo
  • 7,921
  • 14
  • 64
  • 111
1

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 the marker only when it falls in the visible region

JJD
  • 50,076
  • 60
  • 203
  • 339
Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143