Google地图令人不爽地将相机缩小 [英] Google Map Undesirably Resets camera zoom out

查看:104
本文介绍了Google地图令人不爽地将相机缩小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于Google Map的应用程序,允许用户查看标记。我遇到的问题是,当我放大查看地图上的所有标记时,几秒钟后通过地图重置为原始缩放级别,我无法查看所有标记。

I have Google Map based app that allow users to view markers. The problem I am having is that when I zoom in to view all the markers on the map, after a few seconds pass the map resets to the original zoom level and I cannot view all markers.

我希望地图保持在用户缩放级别,但我无法设法想出逻辑。这是我的代码:

I want the map to stay in the user zoom level but I can't manage to come up with the logic. This is the code I have:

@Override
protected void onPostExecute(final ArrayList<Item> arrayList) {
    if(isCancelled()) return;
    if(googleMap!=null) {
        googleMap.clear();
        mMarker2Item.clear();
        LatLngBounds.Builder boundBuilder = new LatLngBounds.Builder();
        for (Item item : arrayList) {
            MarkerOptions opts = new MarkerOptions()
                    .position(item.location())
                    .title(item.name);
            if(item.iconBitmap!=null){
                opts = opts.icon(BitmapDescriptorFactory.fromBitmap(item.iconBitmap));
            }
            Marker newMarker = googleMap.addMarker(opts);
            newMarker.setSnippet(item.vicinity);
            mMarker2Item.put(newMarker, item);
            boundBuilder.include(item.location());
        }
        try {
            CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundBuilder.build(), 200);
            googleMap.moveCamera(cameraUpdate);
            googleMap.animateCamera(cameraUpdate, 1000, null);
        } catch (Exception ex) {

        }
    } else mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            onPostExecute(arrayList);
        }
    }, 500);
}


推荐答案

Activity中的类成员变量(前提是您的AsyncTask是Activity的子类):

Just define a boolean flag as a class member variable in the Activity (provided that your AsyncTask is a subclass of the Activity):

public boolean firstTime = true;

然后,在移动摄像头之前检查布尔标志:

Then, check the boolean flag before you move the camera:

if (firstTime) {
  firstTime = false;
  CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(boundBuilder.build(), 200);
  googleMap.moveCamera(cameraUpdate);
  googleMap.animateCamera(cameraUpdate, 1000, null);
}

这篇关于Google地图令人不爽地将相机缩小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆