百度地图定位模块



本文转自http://blog.csdn.net/jwzhangjie/article/details/8924198

最近使用百度地图,查看了官方的说明,然后做了一个Demo,作为入门,如果看了我之前的代码,有一个习惯就是使用代码写布局,感觉这样比较快,习惯而已。

源码如下:

  1. package com.zhangjie.local;  
  2.   
  3. import android.os.Bundle;  
  4. import android.os.Vibrator;  
  5. import android.app.Activity;  
  6. import android.app.Service;  
  7. import android.util.DisplayMetrics;  
  8. import android.util.Log;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.widget.Button;  
  13. import android.widget.LinearLayout;  
  14. import android.widget.RelativeLayout;  
  15. import android.widget.TextView;  
  16.   
  17. import com.baidu.location.BDLocation;  
  18. import com.baidu.location.BDLocationListener;  
  19. import com.baidu.location.LocationClient;  
  20. import com.baidu.location.LocationClientOption;  
  21. import com.baidu.location.BDNotifyListener;//假如用到位置提醒功能,需要import该类  
  22. public class Local extends Activity implements OnClickListener{  
  23.   
  24.     @Override  
  25.     protected void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         getDisplayMetrics();  
  28.         initLayout();  
  29.         setContentView(Parent);  
  30.         myListener = new MyLocationListener();  
  31.         mLocationClient = new LocationClient(getApplicationContext());  
  32.         mLocationClient.registerLocationListener(myListener);  
  33.         //设置定位参数包括:定位模式(单次定位,定时定位),返回坐标类型,是否打开GPS等等  
  34.         option = new LocationClientOption();  
  35.         option.setOpenGps(true);  
  36.         option.setAddrType("all");//返回定位结果包含地址信息  
  37.         option.setCoorType("bd0911");//返回的定位结果是百度经纬度,默认值gcj02  
  38.         option.setScanSpan(5000);//设置发起请求的时间间隔为5000ms  
  39.         option.disableCache(true);//禁止开启缓存定位  
  40.         option.setPoiNumber(5);//最多返回POI个数  
  41.         option.setPoiDistance(1000);//poi查询距离  
  42.         option.setPoiExtraInfo(true);//是否需要POI的电话和地址等详细信息  
  43.         mLocationClient.setLocOption(option);  
  44.         mLocationClient.start();  
  45.     }  
  46.       
  47.     /** 
  48.      * 初始化布局 
  49.      */  
  50.     public void initLayout(){  
  51.         Parent = new RelativeLayout(this);  
  52.         bottomLayout = new LinearLayout(this);  
  53.         bottomLayout.setId(10);  
  54.         contentTextView = new TextView(this);  
  55.         contentTextView.setText(R.string.content);  
  56.         localButton = new Button(this);  
  57.         localButton.setText(R.string.localrequest);  
  58.         localButton.setId(11);  
  59.         localButton.setOnClickListener(this);  
  60.         poiButton = new Button(this);  
  61.         poiButton.setText(R.string.poirequest);  
  62.         poiButton.setId(12);  
  63.         poiButton.setOnClickListener(this);  
  64.         notifyButton = new Button(this);  
  65.         notifyButton.setText(R.string.notify);  
  66.         notifyButton.setId(13);  
  67.         notifyButton.setOnClickListener(this);  
  68.         offlineButton = new Button(this);  
  69.         offlineButton.setText(R.string.offine);  
  70.         offlineButton.setId(14);  
  71.         offlineButton.setOnClickListener(this);  
  72.           
  73.         //設置底部佈局的button  
  74.         int disten = (Screen_width - dip2px(buttonWidth) * 4) / 5;  
  75.         LinearLayout.LayoutParams buttonInBottomLayoutParams = new LinearLayout.LayoutParams(dip2px(buttonWidth), dip2px(buttonHeight));  
  76.         buttonInBottomLayoutParams.leftMargin = disten;  
  77.         bottomLayout.addView(localButton, buttonInBottomLayoutParams);  
  78.         bottomLayout.addView(poiButton, buttonInBottomLayoutParams);  
  79.         bottomLayout.addView(notifyButton, buttonInBottomLayoutParams);  
  80.         bottomLayout.addView(offlineButton, buttonInBottomLayoutParams);  
  81.         RelativeLayout.LayoutParams bottomInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);  
  82.         bottomInParentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);  
  83.         Parent.addView(bottomLayout, bottomInParentLayoutParams);  
  84.         //設置contentTextView佈局  
  85.         RelativeLayout.LayoutParams contentInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);  
  86.         contentInParentLayoutParams.addRule(RelativeLayout.ABOVE, 10);  
  87.         Parent.addView(contentTextView, contentInParentLayoutParams);  
  88.           
  89.     }  
  90.       
  91.     @Override  
  92.     public void onClick(View v) {  
  93.         switch (v.getId()) {  
  94.         case 11:  
  95.             //发起定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。  
  96.             if (mLocationClient != null && mLocationClient.isStarted()) {  
  97.                 mLocationClient.requestLocation();  
  98.             }else {  
  99.                 Log.e("LocSDK3""locClient is null or not started");  
  100.             }  
  101.             break;  
  102.         case 12:  
  103.             //发起POI查询请求。请求过程是异步的,定位结果在上面的监听函数onReceivePoi中获取  
  104.             if (mLocationClient != null && mLocationClient.isStarted()) {  
  105.                 mLocationClient.requestPoi();  
  106.             }  
  107.             break;  
  108.         case 13:  
  109.             if (!clickNotify) {  
  110.                     clickNotify = true;  
  111.                     //位置提醒最多提醒3次,3次过后将不再提醒。 假如需要再次提醒,或者要修改提醒点坐标,都可通过函数SetNotifyLocation()来实现  
  112.                     //位置提醒相关代码  
  113.                     mNotifyer = new NotifyLister();  
  114.                     mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)  
  115.                     mLocationClient.registerNotify(mNotifyer);  
  116.             }else {  
  117.                 clickNotify = false;  
  118.                 //取消位置提醒  
  119.                 mLocationClient.removeNotifyEvent(mNotifyer);  
  120.             }  
  121.             break;  
  122.         case 14:  
  123.             /* 
  124.              *  发起离线定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。 
  125.              *  getLocTypte = BDLocation.TypteOfflineLocation || BDLocation.TypeOfflineLocationFail 
  126.              *  表示是离线定位请求返回的定位结果 
  127.              */  
  128.             if (mLocationClient != null && mLocationClient.isStarted()) {  
  129.                 mLocationClient.requestOfflineLocation();  
  130.             }  
  131.             break;  
  132.         }  
  133.     }  
  134.       
  135.     //获取屏幕的宽度,高度和密度以及dp / px  
  136.      public void getDisplayMetrics() {  
  137.         DisplayMetrics dm = new DisplayMetrics();  
  138.         dm = getApplicationContext().getResources().getDisplayMetrics();  
  139.         Screen_width = dm.widthPixels;  
  140.         Screen_height = dm.heightPixels;  
  141.         scale = getResources().getDisplayMetrics().density;  
  142.     }  
  143.            
  144.      public int dip2px(float dpValue) {    
  145.         return (int)(dpValue * scale + 0.5f);  
  146.      }  
  147.   
  148.      @Override  
  149.     public boolean onCreateOptionsMenu(Menu menu) {  
  150.         getMenuInflater().inflate(R.menu.local, menu);  
  151.         return true;  
  152.     }  
  153.   
  154.     @Override  
  155.     protected void onStop() {  
  156.         super.onStop();  
  157.         if (mLocationClient != null) {  
  158.             mLocationClient.stop();  
  159.             mLocationClient = null;  
  160.         }  
  161.     }  
  162.   
  163.     public class MyLocationListener implements BDLocationListener{  
  164.   
  165.         //接收异步返回的定位结果,参数是BDLocation类型参数  
  166.         @Override  
  167.         public void onReceiveLocation(BDLocation location) {  
  168.             if (location == null) {  
  169.                 return;  
  170.             }  
  171.             StringBuffer sb = new StringBuffer(256);  
  172.             sb.append("time: ");  
  173.             sb.append(location.getTime());  
  174.             sb.append("\nerror code: ");  
  175.             sb.append(location.getLocType());  
  176.             sb.append("\nlontitude: ");  
  177.             sb.append(location.getLongitude());  
  178.             sb.append("\nradius: ");  
  179.             sb.append(location.getRadius());  
  180.             if (location.getLocType() == BDLocation.TypeGpsLocation) {  
  181.                 sb.append("\nspedd: ");  
  182.                 sb.append(location.getSpeed());  
  183.                 sb.append("\nsatellite: ");  
  184.                 sb.append(location.getSatelliteNumber());  
  185.             }else if(location.getLocType() == BDLocation.TypeNetWorkLocation){  
  186.                 sb.append("\naddr: ");  
  187.                 sb.append(location.getAddrStr());  
  188.             }else if(location.getLocType() == BDLocation.TypeOffLineLocation || location.getLocType() == BDLocation.TypeOffLineLocationNetworkFail){  
  189.                   
  190.             }  
  191.             if (contentTextView != null) {  
  192.                 contentTextView.setText(sb.toString());  
  193.             }  
  194.         }  
  195.   
  196.         //接收异步返回的POI查询结果,参数是BDLocation类型参数  
  197.         @Override  
  198.         public void onReceivePoi(BDLocation arg0) {  
  199.               
  200.         }  
  201.     }  
  202.       
  203.     //BDNotifyListener实现  
  204.     public class NotifyLister extends BDNotifyListener{  
  205.         public void onNotify(BDLocationListener mListener, float distance){  
  206.             if (mVibrator == null) {  
  207.                 mVibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);  
  208.             }  
  209.             mVibrator.vibrate(1000);//振动提醒已到设定位置附近  
  210.         }  
  211.     }  
  212.     public LocationClient mLocationClient = null;  
  213.     LocationClientOption option;  
  214.     public BDLocationListener myListener;  
  215.     public NotifyLister mNotifyer;  
  216.     public Vibrator mVibrator;  
  217.       
  218.     private TextView contentTextView;  
  219.     private Button localButton;  
  220.     private Button poiButton;  
  221.     private Button notifyButton;  
  222.     private Button offlineButton;  
  223.     private RelativeLayout Parent;  
  224.     private LinearLayout bottomLayout;  
  225.       
  226.     public int Screen_width;  
  227.     public int Screen_height;  
  228.     public float scale;  
  229.     public int buttonWidth = 130;//dp  
  230.     public int buttonHeight = 50;//dp  
  231.       
  232.     public boolean clickNotify = false;  
  233.   
  234. }  
package com.zhangjie.local;

import android.os.Bundle;
import android.os.Vibrator;
import android.app.Activity;
import android.app.Service;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.BDNotifyListener;//假如用到位置提醒功能,需要import该类
public class Local extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getDisplayMetrics();
		initLayout();
		setContentView(Parent);
		myListener = new MyLocationListener();
		mLocationClient = new LocationClient(getApplicationContext());
		mLocationClient.registerLocationListener(myListener);
		//设置定位参数包括:定位模式(单次定位,定时定位),返回坐标类型,是否打开GPS等等
		option = new LocationClientOption();
		option.setOpenGps(true);
		option.setAddrType("all");//返回定位结果包含地址信息
		option.setCoorType("bd0911");//返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);//设置发起请求的时间间隔为5000ms
		option.disableCache(true);//禁止开启缓存定位
		option.setPoiNumber(5);//最多返回POI个数
		option.setPoiDistance(1000);//poi查询距离
		option.setPoiExtraInfo(true);//是否需要POI的电话和地址等详细信息
		mLocationClient.setLocOption(option);
		mLocationClient.start();
	}
	
	/**
	 * 初始化布局
	 */
	public void initLayout(){
		Parent = new RelativeLayout(this);
		bottomLayout = new LinearLayout(this);
		bottomLayout.setId(10);
		contentTextView = new TextView(this);
		contentTextView.setText(R.string.content);
		localButton = new Button(this);
		localButton.setText(R.string.localrequest);
		localButton.setId(11);
		localButton.setOnClickListener(this);
		poiButton = new Button(this);
		poiButton.setText(R.string.poirequest);
		poiButton.setId(12);
		poiButton.setOnClickListener(this);
		notifyButton = new Button(this);
		notifyButton.setText(R.string.notify);
		notifyButton.setId(13);
		notifyButton.setOnClickListener(this);
		offlineButton = new Button(this);
		offlineButton.setText(R.string.offine);
		offlineButton.setId(14);
		offlineButton.setOnClickListener(this);
		
		//設置底部佈局的button
		int disten = (Screen_width - dip2px(buttonWidth) * 4) / 5;
		LinearLayout.LayoutParams buttonInBottomLayoutParams = new LinearLayout.LayoutParams(dip2px(buttonWidth), dip2px(buttonHeight));
		buttonInBottomLayoutParams.leftMargin = disten;
		bottomLayout.addView(localButton, buttonInBottomLayoutParams);
		bottomLayout.addView(poiButton, buttonInBottomLayoutParams);
		bottomLayout.addView(notifyButton, buttonInBottomLayoutParams);
		bottomLayout.addView(offlineButton, buttonInBottomLayoutParams);
		RelativeLayout.LayoutParams bottomInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		bottomInParentLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
		Parent.addView(bottomLayout, bottomInParentLayoutParams);
		//設置contentTextView佈局
		RelativeLayout.LayoutParams contentInParentLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		contentInParentLayoutParams.addRule(RelativeLayout.ABOVE, 10);
		Parent.addView(contentTextView, contentInParentLayoutParams);
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case 11:
			//发起定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。
			if (mLocationClient != null && mLocationClient.isStarted()) {
				mLocationClient.requestLocation();
			}else {
				Log.e("LocSDK3", "locClient is null or not started");
			}
			break;
		case 12:
			//发起POI查询请求。请求过程是异步的,定位结果在上面的监听函数onReceivePoi中获取
			if (mLocationClient != null && mLocationClient.isStarted()) {
				mLocationClient.requestPoi();
			}
			break;
		case 13:
			if (!clickNotify) {
					clickNotify = true;
					//位置提醒最多提醒3次,3次过后将不再提醒。 假如需要再次提醒,或者要修改提醒点坐标,都可通过函数SetNotifyLocation()来实现
					//位置提醒相关代码
					mNotifyer = new NotifyLister();
					mNotifyer.SetNotifyLocation(42.03249652949337,113.3129895882556,3000,"gps");//4个参数代表要位置提醒的点的坐标,具体含义依次为:纬度,经度,距离范围,坐标系类型(gcj02,gps,bd09,bd09ll)
					mLocationClient.registerNotify(mNotifyer);
			}else {
				clickNotify = false;
				//取消位置提醒
				mLocationClient.removeNotifyEvent(mNotifyer);
			}
			break;
		case 14:
			/*
			 *  发起离线定位请求。请求过程是异步的,定位结果在上面的监听函数onReceiveLocation中获取。
			 *	getLocTypte = BDLocation.TypteOfflineLocation || BDLocation.TypeOfflineLocationFail
			 *  表示是离线定位请求返回的定位结果
			 */
			if (mLocationClient != null && mLocationClient.isStarted()) {
				mLocationClient.requestOfflineLocation();
			}
			break;
		}
	}
	
	//获取屏幕的宽度,高度和密度以及dp / px
	 public void getDisplayMetrics() {
  		DisplayMetrics dm = new DisplayMetrics();
  		dm = getApplicationContext().getResources().getDisplayMetrics();
  		Screen_width = dm.widthPixels;
  		Screen_height = dm.heightPixels;
  		scale = getResources().getDisplayMetrics().density;
	}
		 
	 public int dip2px(float dpValue) {  
        return (int)(dpValue * scale + 0.5f);
     }

	 @Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.local, menu);
		return true;
	}

	@Override
	protected void onStop() {
		super.onStop();
		if (mLocationClient != null) {
			mLocationClient.stop();
			mLocationClient = null;
		}
	}

	public class MyLocationListener implements BDLocationListener{

		//接收异步返回的定位结果,参数是BDLocation类型参数
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null) {
				return;
			}
			StringBuffer sb = new StringBuffer(256);
			sb.append("time: ");
			sb.append(location.getTime());
			sb.append("\nerror code: ");
			sb.append(location.getLocType());
			sb.append("\nlontitude: ");
			sb.append(location.getLongitude());
			sb.append("\nradius: ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation) {
				sb.append("\nspedd: ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite: ");
				sb.append(location.getSatelliteNumber());
			}else if(location.getLocType() == BDLocation.TypeNetWorkLocation){
				sb.append("\naddr: ");
				sb.append(location.getAddrStr());
			}else if(location.getLocType() == BDLocation.TypeOffLineLocation || location.getLocType() == BDLocation.TypeOffLineLocationNetworkFail){
				
			}
			if (contentTextView != null) {
				contentTextView.setText(sb.toString());
			}
		}

		//接收异步返回的POI查询结果,参数是BDLocation类型参数
		@Override
		public void onReceivePoi(BDLocation arg0) {
			
		}
	}
	
	//BDNotifyListener实现
	public class NotifyLister extends BDNotifyListener{
		public void onNotify(BDLocationListener mListener, float distance){
			if (mVibrator == null) {
				mVibrator = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
			}
			mVibrator.vibrate(1000);//振动提醒已到设定位置附近
		}
	}
	public LocationClient mLocationClient = null;
	LocationClientOption option;
	public BDLocationListener myListener;
	public NotifyLister mNotifyer;
	public Vibrator mVibrator;
	
	private TextView contentTextView;
	private Button localButton;
	private Button poiButton;
	private Button notifyButton;
	private Button offlineButton;
	private RelativeLayout Parent;
	private LinearLayout bottomLayout;
	
	public int Screen_width;
	public int Screen_height;
	public float scale;
	public int buttonWidth = 130;//dp
	public int buttonHeight = 50;//dp
	
	public boolean clickNotify = false;

}

界面如下:



    
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
百度地图iOS SDK v2.10.0 Sample共有23个Demo,每个Demo的说明如下: ------------------------------------------------------------------------------------- 一、 Demo名称:基本地图功能 文件名: MapViewBaseDemoViewController.mm 简介:创建一张百度地图并管理地图的生命周期 详述: (1)创建一张最简单的百度地图; (2)管理地图的生命周期,具体请参看代码部分的相关注释; ------------------------------------------------------------------------------------- 二、 Demo名称:多地图展示功能 文件名: MultiMapViewDemo.mm 简介:在一个页面中创建多个地图 详述: (1)在一个页面内创建多个相互独立的地图 (2)地图Logo位置变更,支持6个位置 ------------------------------------------------------------------------------------- 三、 Demo名称:图层展示功能 文件名: MapViewDemoViewController.mm 简介:展示普通图、卫星图、路况图和路况卫星图 详述: (1)详情请参考代码部分或官网开发指南相关章节; ------------------------------------------------------------------------------------- 四、 Demo名称:地图操作功能 文件名: MapViewControlDemoViewController.mm 简介:介绍平移和缩放地图,双指操作地图,监听地图点击事件 详述: (1)介绍地图缩放级别、旋转度和俯视度的get和set方法; (2)监听单击、双击和长按地图事件; (3)单击、双击或长按地图获取该点的经纬度坐标; (4)对地图显示内容进行截图; ------------------------------------------------------------------------------------- 五、 Demo名称:UI控制功能 文件名: MapViewUISettingDemoViewController.mm 简介:介绍开关手势功能和显示隐藏UI控件 详述: (1)地图操作开关:平移、缩放; (2)控件显示开关:显示/隐藏比例尺控件; (3)指南针位置控制:显示在地图左上角或者右上角(仅举例),开发者可据实际情况任意改变位置; (4)禁用所有手势:控制是否一并禁止所有手势。 (5)设置边界区域:给地图增加边界。地图可操作区域和控件显示,都被限制在地图设定的边界内; (6)您还可以参照开发文档控制比例尺的显示位置; ------------------------------------------------------------------------------------- 六、 Demo名称:定位功能 文件名: LocationDemoViewController.mm 简介:介绍定位图层的基本用法 详述: (1)介绍如何开始定位和停止定位; (2)默认定位的状态显示为普通态,v2.0.2版本开始提供定位的跟随态和罗盘态; (3)开发者可以自己修改icon_center_point.png和icon_center_point@2x.png资源修改默认的定位图标; (4)支持开发者自行定义精度圈边框和填充区域的颜色。 ------------------------------------------------------------------------------------- 七、 Demo名称:覆盖物功能 文件名: AnnotationDemoViewController.mm 简介:介绍使用SDK内置方法绘制点线面等几何图形和如何添加标注 详述: (1)内置覆盖物标签: a、使用内置方法绘制几何图形; b、通过BMKMapView的addOverlay接口添加一个覆盖物; c、开发者可以实现BMKMapViewDelegate中mapView:viewForOverlay;接口,提供要添加到地图中的 标注的BMKOverlayView,开发者可以扩展此类丰富更多的功 (2)添加标注标签: a、给标注添加落下的动画效果,点击标注可弹出对话框,还可以拖拽标注; b、通过BMKMapView的addAn

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值