转载注明出处:http://blog.csdn.net/zhshulin/article/details/41378927
百度地图SDK3.0今年6月5号更新之后,全面升级SDK接口设计,不兼容老版本。
本文简介:百度地图自带缩放控件很丑,即使在9月18号发布的3.1.1版本中开放了调整位置的接口依然无法满足我的需要,而且百度地图自己的产品就是位于右侧上下排列的两个缩放按钮,看着舒服,用着方便。故而本文说明的就是模仿这种实现的一个简单方法。
这是一个大神的解决方案,他用的百度地图版本为2.3,接口和当前3.0之后的很多不一样,修改之后发现没有metersToEquatorPixels()方法,故而无法直接实现,但是今天发现百度地图SDK3.1.1已经新增了这种方法,故而有需求的同学可以参考他的解决方案,不过需要自己改一改。
http://blog.csdn.net/xiaanming/article/details/11821523
以下是我的解决方案,非常简单:
首先看一下主Activity中的布局文件,很简单的相对布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- 添加地图控件 -->
<com.baidu.mapapi.map.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" />
<RelativeLayout
android:id="@+id/ZoomControlView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20.0dip"
android:layout_marginRight="5.0dip" >
<Button
android:id="@+id/zoomin"
android:layout_width="40dip"
android:layout_height="40dip"
android:background="@drawable/zoomin_seletor" />
<Button
android:id="@+id/zoomout"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_below="@+id/zoomin"
android:background="@drawable/zoomout_seletor" />
</RelativeLayout>
</RelativeLayout>
接下来就是隐藏百度默认的缩放控件,然后为自己的控件添加点击事件:
隐藏缩放控件在3.0版本中zoomControlsEnabled(boolean enabled)设为false没效果,不知道后面更新3.1.1中有没有解决,有兴趣的可以自己试试。我的解决方法是:
/**
* 隐藏缩放控件
*
* @param mapView
*/
private void hideZoomView(MapView mapView) {
baiduMap = mapView.getMap();
// 隐藏缩放控件
int childCount = mapView.getChildCount();
View zoom = null;
for (int i = 0; i < childCount; i++) {
View child = mapView.getChildAt(i);
if (child instanceof ZoomControls) {
zoom = child;
break;
}
}
zoom.setVisibility(View.GONE);
}
hideZoomView(mapView);// 隐藏缩放控件
zoomInBtn = (Button) v.findViewById(R.id.zoomin);
zoomOutBtn = (Button) v.findViewById(R.id.zoomout);
zoomInBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float zoomLevel = baiduMap.getMapStatus().zoom;
L.i(Float.toString(zoomLevel));
if(zoomLevel<=18){
// MapStatusUpdateFactory.zoomIn();
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomIn());
zoomOutBtn.setEnabled(true);
}else{
Toast.makeText(getActivity(), "已经放至最大!", Toast.LENGTH_SHORT).show();
zoomInBtn.setEnabled(false);
}
}
});
zoomOutBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
float zoomLevel = baiduMap.getMapStatus().zoom;
if(zoomLevel>4){
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomOut());
zoomInBtn.setEnabled(true);
}else{
zoomOutBtn.setEnabled(false);
Toast.makeText(getActivity(), "已经缩至最小!", Toast.LENGTH_SHORT).show();
}
}
});这样我们就得到了我们想要的效果了。
原文:http://blog.csdn.net/zhshulin/article/details/41378927