百度地图API制作自定义的放大缩小控件ZoomControlView
2015-06-11 23:05
330 查看
接着上一篇:上一篇博客讲的是用百度地图的API制作 Marker 和 自定义的弹出框,这一篇则是讲对于百度地图的放大缩小控制:
老规矩先来张截图:
![](https://img-blog.csdn.net/20150611231437698?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDkxMTU3Ng==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
接下来一大波代码来袭。。。
zoom_control_layout.xml
zoomin_selector.xml
zoomout_selector.xml
代码很容易理解,大家自己看吧,用的话这样就可以
老规矩先来张截图:
接下来一大波代码来袭。。。
package com.jsbtclient.cusViews; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.MapStatus; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.example.jsbtclient.R; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; public class ZoomControlView extends LinearLayout implements View.OnClickListener{ private MapView mapView; private BaiduMap baiduMap;//百度地图对象控制器 private MapStatus mapStatus;//百度地图状态 private ImageView zoomOut, zoomIn; private float MaxLevel; private float MinLevel; public ZoomControlView(Context context) { super(context); initZoomControlView(context); } public ZoomControlView(Context context, AttributeSet attr) { super(context, attr); initZoomControlView(context); } private void initZoomControlView(Context context) { LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.zoom_control_layout, this); zoomIn = (ImageView) view.findViewById(R.id.zoom_control_plus); zoomOut = (ImageView) view.findViewById(R.id.zoom_control_reduce); zoomIn.setOnClickListener(this); zoomOut.setOnClickListener(this); } /** * set {@link MapView} */ public void setMapView(MapView mapView) { if(mapView != null) { this.mapView = mapView; baiduMap = mapView.getMap(); mapStatus = baiduMap.getMapStatus(); MaxLevel = baiduMap.getMaxZoomLevel(); MinLevel = baiduMap.getMinZoomLevel(); }else{ throw new NullPointerException("you should call setMapView(MapView mapView) at first"); } } @Override public void onClick(View view) { switch (view.getId()) { case R.id.zoom_control_plus: baiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn());//放大 break; case R.id.zoom_control_reduce: baiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut());//缩小 break; default: break; } mapStatus = mapView.getMap().getMapStatus(); refreshZoomControlView(); } private void refreshZoomControlView() { float zoom = mapStatus.zoom; if(zoom> MinLevel && zoom< MaxLevel) { if(!zoomIn.isEnabled()){ zoomIn.setEnabled(true); //设置为可点击 } if(!zoomOut.isEnabled()){ zoomOut.setEnabled(true); } }else if(zoom == MinLevel) { zoomOut.setEnabled(false); zoomIn.setEnabled(true); }else { zoomIn.setEnabled(false); zoomOut.setEnabled(true); } } }
zoom_control_layout.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/zoom_control_plus" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:contentDescription="@string/app_name" android:src="@drawable/zoomin_selector" /> <ImageView android:id="@+id/zoom_control_reduce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_weight="1" android:contentDescription="@string/app_name" android:src="@drawable/zoomout_selector" /> </LinearLayout>
zoomin_selector.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/zoomin_pressed"></item> <item android:state_enabled="false" android:drawable="@drawable/zoomin_pressed"></item> <item android:drawable="@drawable/zoomin_normal"></item> </selector>
zoomout_selector.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/zoomout_pressed"></item> <item android:state_enabled="false" android:drawable="@drawable/zoomout_pressed"></item> <item android:drawable="@drawable/zoomout_normal"></item> </selector>
代码很容易理解,大家自己看吧,用的话这样就可以
zoomControlView = (ZoomControlView) view.findViewById(R.id.map_zoomcontrol); zoomControlView.setMapView(mapView); mapView.showZoomControls(false);//隐藏原生的控制按钮
相关文章推荐
- DuiVision开发教程(18)-弹出窗
- 没有html、head、body的页面
- 2015061110 - 我为什么离开百度?
- 栈的应用---后缀运算法则
- 上课笔记第二十九天、mysql、mysql编译安装及组成、客户端、变量和类型
- Cookie/Session机制详解
- Spring启动后执行
- boost源码剖析之:多重回调机制signal(下)
- Git指南
- 如何提高代码质量
- duilib中各控件响应的消息类型
- [转]Java实现定时任务的三种方法
- web请求编码的有关问题
- iOS之多线程
- LeetCode之“动态规划”:Decode Ways
- 《鸟哥的Linux私房菜》读书笔记:X window介绍及启动流程
- 返回到顶部按钮
- GMM高斯混合模型学习笔记(EM算法求解)
- 2015061109 - 如何管理自己的人脉?
- cocos2d-js添加百度appx的插屏广告(通过jsb反射机制)