Google Maps - camera position with a marker below

If you look at Google navigation, it always keeps the driver marker closer to the bottom of the screen, and when you move the camera, it suggests resetting it from the bottom. I am wondering how to achieve the same thing as a marker.

Before that, there was a similar question , but the proposed answers do not take into account the slope of the map, which leads to incorrect projection.

enter image description here

+6
source share
2 answers

Nima, there are different ways to achieve this behavior by adjusting the values ​​in the camera positions.

For example, if you have geographic location information available to you that you can find, UserLocation and DestinationLocation find the midpoint of that location and set the camera target for it. And then you can move the camera to a zoom level that covers both geolocation and proper filling from above and below, determining the value of the bearing.

//First build the bounds using the builder LatLngBounds.Builder builder = new LatLngBounds.Builder(); LatLngBounds bounds; builder.include(userLocation); builder.include(destinationLocation); bounds = builder.build(); // define value for padding int padding =20; //This cameraupdate will zoom the map to a level where both location visible on map and also set the padding on four side. CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds,padding); mMap.moveCamera(cu); // now lets make the map rotate based on user location // for that we will add the bearing to the camera position. //convert latlng to Location object Location startLocation = new Location("startingPoint"); startLocation.setLatitude(userLocation.latitude); startLocation.setLongitude(userLocation.longitude); Location endLocation = new Location("endingPoint"); endLocation.setLatitude(destinationLocation.latitude); endLocation.setLongitude(destinationLocation.longitude); //get the bearing which will help in rotating the map. float targetBearing = startLocation.bearingTo(endLocation); //Now set this values in cameraposition CameraPosition cameraPosition = new CameraPosition.Builder() .target(bounds.getCenter()) .zoom(mMap.getCameraPosition().zoom) .bearing(targetBearing) .build(); mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
+2
source

The current location marker is always at the bottom of the screen while driving. To do this, we must install

 map.setPadding(0,320,0,0); 

So, we install pdding top on card 320, then it takes up some space at the top of the screen. In your last code like this

  CameraPosition cameraPosition = new CameraPosition.Builder() .target(newLatLng) .zoom(18) .bearing(0) .tilt(0) .build(); map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); map.setPadding(0,320,0,0); 
+1
source

All Articles