Calculate the area of the shown map - android

Given a MapFragment with any zoom level at any time, is it possible to detect the area covered by the map?
For instace, I'm at Lat: 38.766667 and Lon: -9.15 with the zoom level at 15.0f, how do I calculate the area covered or how do I obtain the top left corner and bottom right coordinates?

you can get the corners of the view area by doing:
googleMap.getProjection().getVisibleRegion().
//farLeft;
//farRight;
//nearLeft;
//nearRight;
//or
//latLngBounds;
Keep in mind that if you have tilting/inclined view the real view area is not a rectangle but a trapezoid.
Have a look at the documentation here to check what is best for you, latLngBounds gives the "smallest" rectangle, that is not the exact area!
https://developers.google.com/android/reference/com/google/android/gms/maps/model/VisibleRegion

Related

Keep location centered, when changing Google Maps padding in Android

Let's assume at the center of the map is the Eiffel Tower, when I change the bottom padding to half of the screen height, the Eiffel Tower keeps at it's position. What I want is to move the Eiffel Tower to middle of upper half of the screen. Can someone help me?
Based from this documentation, you can add padding around the edges of the map using the GoogleMap.setPadding() method. The map will continue to fill the entire container, but text and control positioning, map gestures, and camera movements will behave as if it has been placed in a smaller space.
Here is my sample code:
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setPadding(0,0,0,1000);
LatLng eiffel = new LatLng(48.858093, 2.294694);
mMap.moveCamera(CameraUpdateFactory.newLatLng(eiffel));
mMap.addMarker(new MarkerOptions().position(eiffel).title("Marker in Eiffel"));
}
Here is the screenshot:
Without padding:
With padding:
As you can see, I have set a padding at the bottom and the Google logo is on the middle of the screen which proves that the padding is working. The marker is also at the middle of upper screen.
You can also check on this video tutorial the way on how to use map padding with the Google Maps Android API.
Hope it helps! :)

Get the LatLngBouds from a camera position

I want to get the LatLng of the top right corner and bottom left corner of a Google Map frame to create a LatLngBounds. Is it possible to do that?
This can easily be determined using the Map View API:
yourMapFragment.getMap().getProjection().getVisibleRegion().latLngBounds
Which the docs state is:
The smallest bounding box that includes the visible region defined in this class.
Alternatively, you can also get the individual corners of the projection by replacing latLngBounds with farLeft, farRight, nearLeft or nearRight
In your case, you can simply use
LatLng bottomLeft =
yourMapFragment.getMap().getProjection().getVisibleRegion().nearLeft;
LatLng topRight =
yourMapFragment.getMap().getProjection().getVisibleRegion().farRight;
Of course. You have the Visible Region Object, that has the coordinates of all the four angles. Please refer to documentation.

Area Circle Overlay

How do I draw a area circle overlay underneath a marker?
For instance you would give the area circle an origin coordinate (Latitude, Longitude) and a radius relative to the origin.
So when you zoom in and out of the map the circle will scale with the zoom level correctly.
There is no standard Overlay in osmdroid for that, but it's quite easy to implement that by copying and simplifying this one: DirectedLocationOverlay

Defining a CameraUpdate with LatLngBounds with different padding values

Is there a function that allows specifying default padding values for different edges of the MapFragment, i.e. similar to
CameraUpdateFactory.newLatLngBounds(LatLngBounds bounds, int padding)
which already exists, but instead something like
CameraUpdateFactory.newLatLngBounds(LatLngBounds bounds, int paddingTop, int paddingLeft, int paddingBottom, int paddingRight)?
Why I need this: In my MapFragment I have a half transparent layer on top. Therefore I need a wider padding at the top than at the bottom.
I found this similar question: Offseting the center of the MapFragment for an animation moving both the target lat/lng and the zoom level. However, getting the projection and scrolling by X pixels is not enough in my case, because I don't only want to scroll (pan). I use a CameraUpdate with newLatLngBounds(bounds, padding), which is supposed to both zoom and pan. So I have a different projection before the camera update.
I hope I made the question clear!
This is an example of set padding to map...
Sets padding on the map.
This method allows you to define a visible region on the map, to signal to the map that portions of the map around the edges may be obscured, by setting padding on each of the four edges of the map. Map functions will be adapted to the padding. For example, the zoom controls, compass, copyright notices and Google logo will be moved to fit inside the defined region, camera movements will be relative to the center of the visible region, etc.
*the size is in pixels
GoogleMap.setPadding(     10,      20,      20,     50);
    all are Int                       left,        top,       right,  bottom
from #MaciejGorsky, here is the documentation...
https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap?hl=pl#setPadding(int, int, int, int)

Android Google Map filled circle

I am trying to make a map overlay that just shows a solid circle from the map center who's radius is a range in meters.
I can't figure out how to calculate the circle's radius. I can get the map center, but I haven't had any luck figuring out how to convert meters into the proper units for the circle's radius.
Thanks for any help.
I've got no experience of doing this in a gMaps context, but the MapView class (whose getProjection() I'm assuming you've used to work out where your point is on the screen) has getLatitudeSpan() and getLongitudeSpan() which you can use against the screen res to calculate the pixel-radius of your circle.

Categories

Resources