使用百度LBS android API 获取当前位置

本文介绍如何使用百度地图SDK在Android应用中实现位置服务。主要步骤包括:在AndroidManifest.xml中配置必要的权限和服务,设置AccessKey,初始化LocationClient并设置定位参数。文中还提供了关键代码示例。
1. AndroidManifest.xml中添加
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote">
</service>
2.添加权限
<!-- 这个权限用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<!-- 这个权限用于访问GPS定位-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位-->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<!-- 获取运营商信息,用于支持提供运营商信息相关的接口-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位-->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<!-- 用于读取手机当前的状态-->
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<!-- 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<!-- 访问网络,网络定位需要上网-->
<uses-permission android:name="android.permission.INTERNET" />
<!—SD卡读取权限,用户写入离线定位数据-->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>
<!--允许应用读取低级别的系统日志文件 -->
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
3.设置key
设置AccessKey,在application标签中加入
<meta-data
            android:name="com.baidu.lbsapi.API_KEY"
            android:value="key" />//key:开发者申请的key
4.
<pre name="code" class="java"><span style="white-space:pre">		</span>public LocationClient mLocationClient = null;//核心类
		public String logMsg = null;
		private double latitude = 0;
		private double longitude = 0;

		public PlaceholderFragment() {
		}

		private void initLocation() { //设置定位参数的方法
			LocationClientOption option = new LocationClientOption();
			option.setLocationMode(LocationMode.Hight_Accuracy);
			option.setCoorType("gcj02");
			int span = 1000;
			option.setScanSpan(span);
			option.setIsNeedAddress(true);

			mLocationClient.setLocOption(option);
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);

			mLocationClient = new LocationClient(getActivity()
					.getApplicationContext());  // 声明LocationClient类
			mLocationClient.registerLocationListener(new BDLocationListener() { //设置监听,重写回调方法,在方法中,其中的location为返回的位置信息,通过get方法可以获取各种信息

				@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("\nlatitude : ");
					latitude = location.getLatitude();
					sb.append(latitude);
					sb.append("\nlontitude : ");

					longitude = location.getLongitude();
					sb.append(longitude);
					sb.append("\nradius : ");
					sb.append(location.getRadius());
					if (location.getLocType() == BDLocation.TypeGpsLocation) {
						sb.append("\nspeed : ");
						sb.append(location.getSpeed());
						sb.append("\nsatellite : ");
						sb.append(location.getSatelliteNumber());
					} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
						sb.append("\naddr : ");
						sb.append(location.getAddrStr());
					}

					location.getLocType();
					Log.i("11111111111111111", sb.toString());
					logMsg = sb.toString();
				}

			}); // 注册监听函数

			initLocation();
			mLocationClient.start();//开启服务

			Button button_1 = (Button) rootView.findViewById(R.id.button_1);
			button_1.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {

					Toast.makeText(getActivity(),
							"纬度:" + latitude + ",经度" + longitude, 0).show();

					Intent intent = new Intent(getActivity(),
							MyBaseMapActivity.class);
					intent.putExtra("x", latitude);
					intent.putExtra("y", longitude);
					startActivity(intent);

				}
			});
			return rootView;
		}


		@Override
		public void onDestroyView() {
			mLocationClient.stop();//结束服务
			super.onDestroyView();
		}
</pre><pre>

5.更多详细细节参考 http://developer.baidu.com/map/index.php?title=android-locsdk/guide/v5-0

6.打开百度地图 

		Intent intent = getIntent();
		if (intent.hasExtra("x") && intent.hasExtra("y")) {
			Bundle bundle = intent.getExtras();
			double double1 = bundle.getDouble("x");
			double double2 = bundle.getDouble("y");
			

			LatLng latLng = new LatLng(double1, double2);
			mapview = new MapView(this,
					new BaiduMapOptions().mapStatus(new MapStatus.Builder()
							.target(latLng).build()));

		}else{
			mapview = new MapView(this, new BaiduMapOptions());
		}
		
		setContentView(mapview);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值