gpt4 book ai didi

android - 获取标记边界 Gmap V2 后无法更新倾斜度

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:02:29 25 4
gpt4 key购买 nike

我正在使用它来获取 map 上标记的边界:

markerList.add(m);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markerList) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();
CameraUpdate updatecamera = CameraUpdateFactory.newLatLngBounds(bounds, 50);
map.animateCamera(updatecamera);

获得更新后,我想将 map 倾斜 30 度。

我过去曾用它来倾斜相机,但它不适用于边界功能:

CameraPosition cameraPosition = new CameraPosition.Builder()
.target(meLoc).zoom(6).bearing(0).tilt(30).build();
map.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));

如何在获得边界后添加倾斜度?

谢谢

最佳答案

如您所说,Android Maps API v2 目前不直接支持使用边界和倾斜信息为相机设置动画。

一种解决方法是使用以下步骤:

  1. 用边界做你想执行的相机动画,但忽略倾斜
  2. 监听上面的相机动画结束,获取当前相机位置,然后执行倾斜

这具有将相机移动到边界的效果,同时直视下方(即倾斜 = 0),然后将相机倾斜到位。我发现这在某些用例中效果很好,实际上看起来不错,但在其他用例中有点尴尬。它可能适合也可能不适合您的特定用例。

为了实现这一点,我们假设您有一个主类 MapScreen.java,您的 map 在其中实现。您需要更改它以包含相机监听器界面、对相机位置的引用、对您的 Activity 的引用、您要使用的初始倾斜值(您可以在运行时交替设置)和默认相机填充:

public class MapScreen extends FragmentActivity implements GoogleMap.OnCameraChangeListener {
...
private GoogleMap map;
public static CameraPosition lastCameraPosition;
public static MapScreen mapScreen;
public static float CAMERA_INITIAL_TILT = 30.0f;
public static int CAMERA_PADDING = 100;
...
public void onCreate(Bundle savedInstanceState) {
...
mapScreen = this;
...
//Set up map object here like normal
map = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
...
//Set camera change listener
map.setOnCameraChangeListener(this);
}
...
}

在这个类中,你还需要添加一个方法来在第一个动画停止时监听和保存相机的位置:

@Override
public void onCameraChange(CameraPosition position) {
//Save the last camera position so we can reference it when tilting the camera following animations
lastCameraPosition = position;
}

此外,添加一个您可以引用的内部类,它在第一个动画完成后执行倾斜(稍有延迟以获得正确的 CameraPosition):

public class tiltOnFinishAnimation implements CancelableCallback {

@Override
public void onCancel() {
//Do nothing
}

@Override
public void onFinish() {
//We want to run the tilt animation, but the CameraPosition needed to center the camera in the right place
//is only available after the onFinish() method completes. So, we delay this by 10 ms to let the CameraPosition update
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
mapScreen.runOnUiThread(new Runnable() {
public void run() {
if(lastCameraPosition != null){
//Finish with a tilt
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(lastCameraPosition.target)
.zoom(lastCameraPosition.zoom)
.bearing(lastCameraPosition.bearing)
.tilt(CAMERA_INITIAL_TILT)
.build();
//Perform the tilt!
mapScreen.map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
});
}
}, 10);
}
}

最后,当你想移动相机时,执行你的普通代码,但首先要确定一些 map View 属性,这些属性是根据方向正确设置相机动画所需的,并包含对回调的引用以在第一部分时执行没有倾斜停止的动画:

//Get the View height and width, so we don't exceed the screen size after padding for the camera updates
int width;
int height;
if(mapFragment != null){
width = mapFragment.getView().getWidth();
height = mapFragment.getView().getHeight();
}else{
//If the map fragment hasn't been instantiated yet, use the entire display
if (android.os.Build.VERSION.SDK_INT >= 13){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}else{
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
}
}

if(getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
//Use verticle size for padding
CAMERA_PADDING = (int) (height * 0.2f);
}else{
//Use horizontal size for padding
CAMERA_PADDING = (int) (width * 0.2f);
}

//Your code
markerList.add(m);
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for (Marker marker : markerList) {
builder.include(marker.getPosition());
}
LatLngBounds bounds = builder.build();

//Here's the new code below that triggers first the movement to the bounds, then the tilt (as a callback)
map.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, width, height, CAMERA_PADDING), new tiltOnFinishAnimation());

关于android - 获取标记边界 Gmap V2 后无法更新倾斜度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18855519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com