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?
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.
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.
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
}
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