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

时间:2016-01-08 11:43:22

标签: android google-maps google-maps-android-api-2

如果你看谷歌导航,它总是让驾驶员标记靠近底部,当你移动相机时,它提供将其重置回底部。我想知道如何在给定标记的情况下实现相同的目标。

There was a similar question before,但所提供的答案并未考虑地图倾斜导致投影错误。

enter image description here

2 个答案:

答案 0 :(得分:4)

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

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

因此,我们将pdding顶部设置为地图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);

答案 1 :(得分:2)

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

例如,您可以使用2个地理位置latlng信息,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));