How To Get Latitude/longitude Span In Google Map V2 For Android
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 previou
Solution 1:
You can use the following code to get the lat/lng span:
VisibleRegion vr = mMap.getProjection().getVisibleRegion();
doubleleft= vr.latLngBounds.southwest.longitude;
double top = vr.latLngBounds.northeast.latitude;
doubleright= vr.latLngBounds.northeast.longitude;
double bottom = vr.latLngBounds.southwest.latitude;
I hope this helps.
Solution 2:
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.
Solution 3:
This way works for me:
CameraPositioncamPos2= mapa.getCameraPosition();
LatLngpos= 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:
LatLngBoundsbounds= gMap.getProjection().getVisibleRegion().latLngBounds;
And then ask if any point is in bounds:
LatLngpoint=newLatLng (latitude, longitude);
if(bounds.contains(point)){
//do something
}
Solution 4:
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
Post a Comment for "How To Get Latitude/longitude Span In Google Map V2 For Android"