9

If you look at google navigation, it always keeps the driver marker close to bottom and when you move the camera it offers to reset it back to bottom. I'm wondering how to achieve the same thing given a marker.

There was a similar question before, but the offered answers do not consider the map to be tilted which results in wrong projection.

enter image description here

Community
  • 1
  • 1
Nima
  • 6,383
  • 7
  • 46
  • 68

2 Answers2

5

The current location marker always to bottom of the screen while moving also. For that we have to set

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

So, we set pdding top to the map 320 then it takes some space from the top of the screen. In your final 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);
user1788448
  • 89
  • 1
  • 7
2

Nima, there are different way to achieve this behaviour by tweaking values in camera positions.

For instance you have 2 geo location latlng information available with you, UserLocation and DestinationLocation find the midpoint of this location and set the camera target at it. And then you can move the camera to zoom level which cover both geolocation with proper padding of top and bottom by specifying the bearing value.

    //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));
Nikunj Sakhrelia
  • 449
  • 3
  • 12
  • This doesn't answer my question. As you can see in the screenshot the "endLocation" is not in the view therefore the simple LatLngBounds is not what I am looking for – Nima Jan 08 '16 at 13:14
  • well you can always set any other location instead of destination location, consider your image here you can use the location of the next turn or next 100 meter geo point. The solution is setting the proper zoom level and setting the correct bearing or else try to find the solution where you first set the camera target on userlocation which will bring the map focus on user marker and bring it in center of the screen and then move the camera again to top half of map screen xy to projection of map, by setting that target it will move the marker at bottom. – Nikunj Sakhrelia Jan 08 '16 at 14:04
  • http://stackoverflow.com/questions/16764002/how-to-center-the-camera-so-that-marker-is-at-the-bottom-of-screen-google-map This link will help you how to set the camera focus in a way that marker comes at bottom of the screen. – Nikunj Sakhrelia Jan 08 '16 at 14:21
  • That link is what I posted originally in my question, please read my post. – Nima Jan 14 '16 at 08:04