标签:
定位我们使用的是百度 Android 定位SDKv4.0,我们先了解下定位原理和定位精度
定位原理
使用百度Android定位SDK必须注册GPS和网络使用权限。定位SDK采用GPS、基站、Wi-Fi信号进行定位。当应用程序向定位SDK发起定位请求时,定位SDK会根据应用的定位因素(GPS、基站、Wi-Fi信号)的实际情况(如是否开启GPS、是否连接网络、是否有信号等)来生成相应定位依据进行定位。
用户可以设置满足自身需求的定位依据:
若用户设置GPS优先,则优先使用GPS进行定位,如果GPS定位未打开或者没有可用位置信息,且网络连接正常,定位SDK则会返回网络定位(即Wi-Fi与基站)的最优结果。为了使获得的网络定位结果更加精确,请打开手机的Wi-Fi开关。
定位精度
了解了百度定位的原理和定位精度之后,接下来我们就来使用百度定位SDKv4.0吧
一 . 导入库文件
在使用百度定位SDKv4.0之前,我们要下载最新的库文件,下载地址:点击下载相关库文件,将liblocSDK4.so文件拷贝到libs/armeabi目录下。将locSDK4.0.jar文件拷贝到工程的libs目录下
二 . 一个获取当前位置的工具类
package com.smarteye.baidumap; import java.util.Calendar; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.location.LocationClientOption.LocationMode; import com.smarteye.adapter.BVCU_PUCFG_GPSData; import com.smarteye.adapter.BVCU_WallTime; import com.smarteye.coresdk.BVPU; import android.content.Context; import android.util.Log; public class BaiduLocationTools { private LocationClient locationClient; private LocationListener listener; public BaiduLocationTools(Context context) { locationClient = new LocationClient(context.getApplicationContext()); listener = new LocationListener(); locationClient.registerLocationListener(listener); LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Device_Sensors); option.setOpenGps(true); option.setTimeOut(10 * 1000); option.setCoorType("bd09ll"); option.setScanSpan(1000); option.setIsNeedAddress(true); option.setNeedDeviceDirect(true); locationClient.setLocOption(option); } public void startLocationStart() { locationClient.start(); } public void stopLocationStart() { if (locationClient != null) { locationClient.stop(); } } private class LocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location != null) { Log.i("BaiduLocationTools", "location.getLocType----->" + location.getLocType()); GlobalTool.BAIDU_to_WGS84(location); double latitude = location.getLatitude(); double longitude = location.getLongitude(); BVCU_PUCFG_GPSData data = new BVCU_PUCFG_GPSData(); BVCU_WallTime time = new BVCU_WallTime(); Calendar calendar = Calendar.getInstance(); time.iDay = (char) calendar.get(Calendar.DAY_OF_MONTH); time.iHour = ((char) calendar.get(Calendar.HOUR)); time.iMinute = ((char) calendar.get(Calendar.MINUTE)); time.iMonth = ((char) (calendar.get(Calendar.MONTH) + 1)); time.iSecond = ((char) calendar.get(Calendar.SECOND)); time.iYear = ((short) calendar.get(Calendar.YEAR)); data.stTime = time; data.iLatitude = ((int) (latitude * 10000000)); data.iLongitude = ((int) (longitude * 10000000)); Log.i("BaiduLocationTools", "iLatitude------>" + data.iLatitude); Log.i("BaiduLocationTools", "iLongitude------>" + data.iLongitude); data.bOrientationState = 1; data.bAntennaState = 1; BVPU.InputGPSData(data); } } } }
标签:
原文地址:http://blog.csdn.net/u010794950/article/details/45395103