Android百度地图开发—定位显示以及自定义缩放功能(三)

                                  Android百度地图开发—定位显示自定义缩放功能

     转载请注明:http://blog.csdn.net/smile5005/article/details/40023891

     本文使用的百度地图接口是v3.0版本,利用该接口可以定位当前所处位置,并在百度地图上进行显示,同时鉴于原始Demo中百度地图放大缩小按钮不够美观,故对其进行自定义显示。接下来,我们一步步的实现:

1、首先定义布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:orientation="vertical" >
    
   <!-- 显示百度地图的View -->
   <com.baidu.mapapi.map.MapView      
     android:id="@+id/bmapView"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" /> 
  
    <RelativeLayout  
        android:layout_width="match_parent"
    	android:layout_height="match_parent" 
    	android:orientation="vertical">
    
    <!-- 切换显示模式-->
    <Button
        android:id="@+id/btn_map_mode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="5dip" 
        android:layout_marginBottom="50dip" />
   
	    <LinearLayout
	       android:layout_width="wrap_content"
	       android:layout_height="wrap_content"
	       android:layout_alignParentBottom="true"
	       android:layout_alignParentRight="true"
	       android:layout_alignWithParentIfMissing="false"
	       android:layout_marginBottom="50dip"
	       android:layout_marginRight="10dip"
	       android:orientation="vertical" >
	       
<span style="font-size:18px;">          </span> <!-- 地图的放大缩小-->
	       <Button
	           android:id="@+id/btn_zoom_in"
	           android:layout_width="fill_parent"
	           android:layout_height="fill_parent"
	           android:layout_marginLeft="2dip"
	           android:layout_marginRight="2dip"
	           android:layout_weight="1.0"
	           android:background="@drawable/zoom_in"/>
	       
	       <Button
	           android:id="@+id/btn_zoom_out"
	           android:layout_width="fill_parent"
	           android:layout_height="fill_parent"
	           android:layout_marginLeft="2dip"
	           android:layout_marginRight="2dip"
	           android:layout_weight="1.0"
	           android:background="@drawable/zoom_out"/>
	    </LinearLayout>
	   
   </RelativeLayout>
   
</RelativeLayout>

 

2:接下来在Activity中进行显示,代码如下:

package com.mr.blogtest;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
import com.baidu.mapapi.model.LatLng;
import com.mr.baidu.LocationHelper;
import com.mr.baidu.LocationHelper.OnLocationListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ZoomControls;

public class MainActivity extends Activity implements OnLocationListener {

	private MapView  mMapView=null;
	private BaiduMap mBaiduMap = null;
	private LocationHelper mLocationHelper;
	private LocationMode mCurrentMode;
	private Button mRequestLocButton;
	private BitmapDescriptor mCurrentMarker=null;
	private boolean mIsFirstLoc=true;
	private Button mZoomInButton;
	private Button mZoomOutButton;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initView();
		initData();
	}

	private void initView(){
		
	mMapView=(MapView)findViewById(R.id.bmapView);
		
	//隐藏放大缩小控件;如果需要隐藏其他控件可采用同样方式.
	int childCount = mMapView.getChildCount();
        View zoom = null;
        for (int i = 0; i < childCount; i++) {
           View child = mMapView.getChildAt(i);
           if (child instanceof ZoomControls) {
                   zoom = child;
                   break;
           }
        }
        zoom.setVisibility(View.GONE);
        
	mRequestLocButton=(Button)findViewById(R.id.btn_map_mode);
	mZoomInButton=(Button)findViewById(R.id.btn_zoom_in);
	mZoomOutButton=(Button)findViewById(R.id.btn_zoom_out);
	}
	
	private void initData(){
		mCurrentMode = LocationMode.NORMAL;
		mRequestLocButton.setText("普通");
		mBaiduMap=mMapView.getMap();

		OnClickListener btnClickListener = new OnClickListener(){

			@Override
			public void onClick(View v) {
				if(v.equals(mRequestLocButton)){
					performMode();
				}else if(v.equals(mZoomInButton)){
					performZoomIn();
				}else if(v.equals(mZoomOutButton)){
					performZoomOut();
				}
			}
		};
		mRequestLocButton.setOnClickListener(btnClickListener);
		mZoomInButton.setOnClickListener(btnClickListener);
		mZoomOutButton.setOnClickListener(btnClickListener);
		
		// 开启定位图层,定位初始化;
		mBaiduMap.setMyLocationEnabled(true);
		//打开交通状况图;
		mBaiduMap.setTrafficEnabled(true);
		
		//开始定位,此定位帮助类是对百度定位的封装,可以参照之前的Blog;
		mLocationHelper=new LocationHelper(this);
		mLocationHelper.setOnLocationListener(this);
		mLocationHelper.start();
	}
	
	@Override
	public void onReceiveLocation(BDLocation location) {
		// map view 销毁后不在处理新接收的位置
		if (location == null || mMapView == null)
			return;
		
		//在百度地图上进行定位数据显示;
		MyLocationData locData = new MyLocationData.Builder()
				.accuracy(location.getRadius())
				// 此处设置开发者获取到的方向信息,顺时针0-360
				.direction(100).latitude(location.getLatitude())
				.longitude(location.getLongitude()).build();
		mBaiduMap.setMyLocationData(locData);
		
		if (mIsFirstLoc) {
			mIsFirstLoc = false;
			LatLng ll = new LatLng(location.getLatitude(),location.getLongitude());
			MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
			mBaiduMap.animateMapStatus(u);
		}
	}

	/**
	 * 处理显示模式;
	 */
	private void performMode(){
		
		switch (mCurrentMode) {
		case NORMAL:
			mRequestLocButton.setText("跟随");
			mCurrentMode = LocationMode.FOLLOWING;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
							mCurrentMode, true, mCurrentMarker));
			break;
		case COMPASS:
			mRequestLocButton.setText("普通");
			mCurrentMode = LocationMode.NORMAL;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
							mCurrentMode, true, mCurrentMarker));
			break;
		case FOLLOWING:
			mRequestLocButton.setText("罗盘");
			mCurrentMode = LocationMode.COMPASS;
			mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(
							mCurrentMode, true, mCurrentMarker));
			break;
		}
	}
	
	/**
	 * 处理放大;
	 */
	private void performZoomIn(){
		
		float zoomMaxLevel=mBaiduMap.getMaxZoomLevel();
		float zoomLevel=mBaiduMap.getMapStatus().zoom;
		
		zoomLevel++;
		if(zoomLevel>zoomMaxLevel){
			zoomLevel=zoomMaxLevel;
		}
		
		MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(zoomLevel);
		mBaiduMap.animateMapStatus(u);
	}
	
	/**
	 * 处理缩小;
	 */
	private void performZoomOut(){
		float zoomMinLevel=mBaiduMap.getMinZoomLevel();
		float zoomLevel=mBaiduMap.getMapStatus().zoom;
		
		zoomLevel--;
		if(zoomLevel<zoomMinLevel){
			zoomLevel=zoomMinLevel;
		}
		MapStatusUpdate u = MapStatusUpdateFactory.zoomTo(zoomLevel);
		mBaiduMap.animateMapStatus(u);
	}
	
	@Override
	public void onPause() {
		mMapView.onPause();
		super.onPause();
	}

	@Override
	public void onResume() {
		mMapView.onResume();
		super.onResume();
	}

	@Override
	public void onDestroy() {
		mBaiduMap.setMyLocationEnabled(false);
		mLocationHelper.stop();
		mMapView.onDestroy();
		mMapView = null;
		super.onDestroy();
	}
}
3、最后显示的结果如下所示:


 

 好了,今天就写到这里了,如果有什么不妥之处,可以在下方留言,敬请各位多多赐教。  

  






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值