Android百度地图放大缩小按钮状态更新

/**
 * 应用程序Acitivity基础类
 * 实现地图UI及其功能
 * Created by Lezg on 2014/5/3.
 */
public class BaseActivity extends Activity {

    AppContext mApp;
    MapView mMapView;
    MyLocationOverlay mLocationOverlay;

    private ImageButton zoominBtn;
    private ImageButton zoomoutBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mApp = AppContext.getInstance();
    }

    //初始化地图UI
    void initMapView() {
        zoominBtn = (ImageButton) findViewById(R.id.zoominBtn);
        zoomoutBtn = (ImageButton) findViewById(R.id.zoomoutBtn);
        Button requestLacBtn = (Button) findViewById(R.id.locationBtn);
        Button deleteBtn = (Button) findViewById(R.id.delete_btn);
        mMapView = (MapView) findViewById(R.id.bmapsView);
        mMapView.getController().setZoom(16);
        mMapView.getController().enableClick(true);
        // 定位图层初始化
        mLocationOverlay = new MyLocationOverlay(mMapView);
        mLocationOverlay.enableCompass();
        View.OnClickListener zoomOnClickListener = new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.zoominBtn:
                        mMapView.getController().zoomIn();// 放大
                        break;
                    case R.id.zoomoutBtn:
                        mMapView.getController().zoomOut();// 缩小
                        break;
                    default:
                        break;
                }
            }
        };
        zoominBtn.setOnClickListener(zoomOnClickListener);
        zoomoutBtn.setOnClickListener(zoomOnClickListener);
        requestLacBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AddLocOverlay(true);
            }
        });
        deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mMapView.getOverlays().clear();
                AddLocOverlay(false);
            }
        });
        InitMapViewListener();
    }

    // 用于处理地图事件回调
    private void InitMapViewListener() {
        MKMapViewListener mapViewListener = new MKMapViewListener() {
            int lastLevel = -1;

            @Override
            public void onMapMoveFinish() {
            }

            @Override
            public void onMapLoadFinish() {
            }

            // 动画结束更新地图放大缩小按键状态
            @Override
            public void onMapAnimationFinish() {
                int zoomLevel = Math.round(mMapView.getZoomLevel());
                if (lastLevel < 0 || lastLevel != zoomLevel) {
                    refreshZoomButton(zoomLevel);
                    lastLevel = zoomLevel;
                }
            }

            @Override
            public void onGetCurrentMap(Bitmap arg0) {
            }

            @Override
            public void onClickMapPoi(MapPoi arg0) {
            }
        };
        mMapView.regMapViewListener(mApp.mBMapManager, mapViewListener);
        int zoomLevel = Math.round(mMapView.getZoomLevel());
        refreshZoomButton(zoomLevel);
    }

    private void refreshZoomButton(int level) {
        int maxLevel = mMapView.getMaxZoomLevel();
        int minLevel = mMapView.getMinZoomLevel();
        if (level < maxLevel && level > minLevel) {
            if (!zoominBtn.isEnabled()) {
                zoominBtn.setEnabled(true);
            }
            if (!zoomoutBtn.isEnabled()) {
                zoomoutBtn.setEnabled(true);
            }
        } else if (level == maxLevel) {
            zoominBtn.setEnabled(false);
        } else if (level == minLevel) {
            zoomoutBtn.setEnabled(false);
        }

    }

    //定位
    void AddLocOverlay(boolean flag) {
        if (mLocationOverlay != null) {
            mMapView.getOverlays().remove(mLocationOverlay);
            mLocationOverlay.setData(mApp.locData);
            mMapView.getOverlays().add(mLocationOverlay);
            mMapView.refresh();
            mMapView.getController().setZoom(16);
            mMapView.getController().animateTo(mApp.locGeoPoint);
        }
        if (flag) {//flag=true定位 flag=false添加定位图层
            int radius = (int) mApp.radius;
            String strRadius = String.valueOf(radius);
            String str = "我的位置:";
            String myLocation = str + "(精确到" + strRadius + "米)" + "\n"
                    + mApp.curAddrStr;
            showToast(myLocation);
        }
    }

    void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值