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.
Related
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
I want to display a google map in my application that shows one complete half of the globe, containing two markers that are about 20,000 km from one another in the same view.
I tried using the property getMinZoomLevel(), but it's not sufficient - it shows the first marker surrounded by an area only about the size of half a continent. Is there any way of getting the wide view that I want?
Code example for last known user location (one of the markers), where zoomLevel is getMinZoomLevel():
map.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPositionFromLocationWithZoom(UserLocation.getInstance(App.getInstance().getApplicationContext()).getLastKnownLocation(), zoomLevel)));
Try something like.
private LatLngBounds bounds;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(pointA);
builder.include(pointB);
//and other latlng points as you like...
bounds = builder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(
bounds, 20));
animateCamera animates the camera to specific point location. if you have more then one point (LatLngBounds) it will expand the map to be visible all points.
Hope to be helpful.
The Google Map provides over its projection a method, which converts Point to Geo Position.
If I pass e.x. Point p = new Point(-1000,1000), I get a position like -89.3425,140.0345. The point is outside the visible region but I could not found any information if that method has a limitation to the current visible region.
Has someone a source code or more information about that ?
Create a Rect with the size of your MapView and use Rect.contains(x,y) to check if the given point is inside the MapView
Remember:
The screen location is specified in screen pixels (not display pixels)
relative to the top left of the map (not the top left of the whole
screen).
I've looked on the net but couldn't find an accurate answer. I have an ImageView to use as a marker centred on map, Its position not change when I pan the map. I want to get the latlng of position when I stop panning on the map. I guess I have to use projection but because of I am not so expert in android I got stuck. Can anybody help please.
Use map.getProjection().fromScreenLocation(Point) and map.getProjection().toScreenLocation(LatLng) to convert between screen position and geo location.
Use the Projection via map.getProjection().
This will allow you to convert between screen coordinates and LatLngs. The pixels returned are relative to the View containing the map.
For Maps API v2
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;
Also you can use,
LatLngBounds curScreen = mMap.getProjection()
.getVisibleRegion().latLngBounds;
Hope this will help.
It's not easy explaining my problem but I will try.
I have an android GoogleMap, on top of it, I have an ImageView positioned at its center at all times. If I drag/pan the map, the pin will always be in the center of the GoogleMap.
Now, I add a marker, somewhere on the map. I want to zoom such that the center point remains in the center of the map, and the marker is visible within the map, and to the highest zoom level.
The problem is if I simply check if the marker is within boundaries of the map or not, and then keep zooming in/out till it is, this process will always repeat itself, i.e. trying to zoom in and if the marker became outside, then zoom out.
The problem is I rely on an OnCameraChange listener which will keep calling itself everytime I zoom in or out, hence, the process of zooming in/out will keep occuring indefinitely
journeyGoogleMap.setOnCameraChangeListener(new OnCameraChangeListener()
{
#Override
public void onCameraChange(final CameraPosition position)
{
Basically, what I need is a function where I can provide the center LatLng and the markerLatLng and it will automatically calculate the LatLngBounds making sure my center is within the center of the LatLng bounds, and then I can simply use
public static CameraUpdate newLatLngBounds (LatLngBounds bounds, int width, int height, int padding)
as shown in the link below
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/CameraUpdateFactory#newLatLngBounds(com.google.android.gms.maps.model.LatLngBounds,int, int,int)
If you need anymore clarification please do tell me
You need to calculate LatLngBounds from two points:
Your marker position and
Place on the opposite side of your current center.
The second is calculated like this:
LatLng other = new LatLng(2 * center.latitude - position.latitude, 2 * center.longitude - position.longitude);
See LatLngBoundsUtils.fromCenterAndPositions for a general solution.
Use googleMap.getProjection().getVisibleRegion() to get all four corners of screen as LatLng values forming trapezium. Calculate where trapezium intersects with line drawn through center and your marker. Scale factor is (distance from center to marker / distance from center to intersection point). Now just scale trapezium with this scale factor relative to center. This is new visible regison.
You may also use getVisibleRegion().latLngBounds to simplify calculation, but note that some areas of returned rectangle are actually not visible.