Keep location centered, when changing Google Maps padding in Android - 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! :)

Related

Calculate the area of the shown map

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

Cant Zoom Desired Level In Google Maps To Fit 2 Directions In Screen

(I would want to put screenshots put my reputation is low to put them...)
Good Day.I Have two directions(markers) on google maps api(android) between which i want camera to be center,and camera being centered there.But problem is that one marker comes to one edge of screen and another marker to another edge of screen!I don't want it to be like that,i want just to zoom specified level so markers will be center in google maps view and won't be place on edges of screen.I tried to put even 0 zoom level in camera update factory of google maps,but nothing happens at all.Here is my code how do i achieve that
} finally {
com.google.android.gms.maps.model.LatLngBounds.Builder boundsBuilder = new LatLngBounds.Builder();
boundsBuilder.include(new LatLng(Double.parseDouble(tolat), Double.parseDouble(tolong)));
boundsBuilder.include(new LatLng(Double.parseDouble(fromlat), Double.parseDouble(fromlong)));
final LatLngBounds bounds = boundsBuilder.build();
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));
}
you say you tried to put 0 as the zoom level. You mean this call CameraUpdateFactory.newLatLngBounds(bounds, 0) right?
The second parameter is the padding not the zoom level. Set it up (example to 10) and you have a padding to the outer edges.
https://developers.google.com/android/reference/com/google/android/gms/maps/CameraUpdateFactory
I hope it helps.

Google Maps Android V2 padding with rotation around user

I'm developing an app that is using a google map to show your location. I want to rotate the map around the users location. I use getOrientation(R, orientation) (and adjust for true north) to get the device rotation which is working fine but i want the user location to be near the bottom of the screen. I have set the padding on the map to a quarter of the screen height
mMap.setPadding(0,screenHeight/4,0,0);
What hI have noticed is that the rotation is still done around the point at the centre of the screen NOT the padded centre.
This is the code I'm using in onSensorChanged
currentZoom = mMap.getCameraPosition().zoom;
CameraPosition currentPlace = new CameraPosition.Builder()
.target(new LatLng(currentNode.getLatitude(), currentNode.getLongitude()))
.bearing(degrees).tilt(60).zoom(currentZoom).build();
mMap.stopAnimation();
mMap.animateCamera(CameraUpdateFactory.newCameraPosition(currentPlace),40,null);
Is there a way to change the anchor point of camera rotation?
I was trying to solve this problem for several hours. The only solution I found is not very good, but it works:
For example, if you want to move the user 200dp below the center:
1) Set map fragment height to its height + 200dp (Then the map will go 200dp below the screen)
2) This works fine, but it hides Google logo. To preserve the Google logo, set map padding top and bottom to 200dp.
mGoogleMap.setPadding(0, dp2px(200),0 , dp2px(200));
That solves the problem

tricky Android Google Map zoom while maintaining center point

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.

android change bounds of google map

I am working on a project in android which requires me to change the bounds of a google map(from bottom only). Currently I am trying by adding padding at the bottom, but this process is slow and the screen also flickers.
Is there any better way to do this?
I would like the user to be able to drag the bottom of the map to change the bounds.
I see a lot of AJAX/JS resouces, but this is a native component.
Best
I think it will be a huge amount of work to let the user actually drag the bottom of your map view. what is much easier and faster to do is to create to 2 map fragments of your map and on click of the map change the fragment back and foreword between the two.
this way the user can change the size of the map the he works with.
You can zoom map to added points.
Here is some pseudo code:
LatLngBounds.Builder bc = new LatLngBounds.Builder();
for (LatLng item : points) {
bc.include(item);
}
LatLngBounds bounds = bc.build();
#SuppressWarnings("deprecation")
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,
getWindowManager().getDefaultDisplay().getWidth(),
getWindowManager().getDefaultDisplay().getHeight(), 50);
mMap.moveCamera(cu);
Bounds will be changed to added points. Help It hope

Categories

Resources