Google地图 - 底部有标记的相机位置

Nim*_*a G 8 android google-maps google-maps-android-api-2

如果您查看谷歌导航,它始终保持驾驶员标记接近底部,当您移动相机时,它提供将其重置回底部.我想知道如何给出一个标记同样的东西.

之前有一个类似的问题,但提供的答案并不认为地图是倾斜的,这导致错误的投影.

在此输入图像描述

小智 5

移动时当前位置标记始终位于屏幕底部。为此我们必须设置

map.setPadding(0,320,0,0);
Run Code Online (Sandbox Code Playgroud)

因此,我们将 pdding top 设置为地图 320,然后它会占用屏幕顶部的一些空间。在你的最终代码中是这样的

 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);
Run Code Online (Sandbox Code Playgroud)


Nik*_*lia 2

尼玛,有不同的方法可以通过调整相机位置的值来实现这种行为。

例如,您有 2 个可用的地理位置纬度信息,UserLocation 和 DestinationLocation 找到该位置的中点并将相机目标设置在该位置。然后,您可以通过指定方位值将相机移动到覆盖两个地理位置的缩放级别,并在顶部和底部进行适当的填充。

    //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));
Run Code Online (Sandbox Code Playgroud)