您的位置:首页 > 其它

百度地图API制作自定义的放大缩小控件ZoomControlView

2015-06-11 23:05 330 查看
接着上一篇:上一篇博客讲的是用百度地图的API制作 Marker 和 自定义的弹出框,这一篇则是讲对于百度地图的放大缩小控制:

老规矩先来张截图:



接下来一大波代码来袭。。。

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);//隐藏原生的控制按钮
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: