之前项目中用到了百度定位SDK v3.4,现需要升级到v4.0版本,于是乎去百度开放平台看了Android 定位SDKv4.0文档
链接地址:http://developer.baidu.com/map/geosdk-android.htm
百度Android平台定位 SDK自v4.0版本开始引用了Key验证体系。因此,当您选择使用v4.0及之后版本的定位SDK时,需要先申请且配置Key,并在程序相应位置填写您的Key。(选择使用v3.3及之前版本SDK的开发者,不需要使用Key)
Key机制:每个Key仅且唯一对于1个应用验证有效,即对该Key配置环节中使用的包名匹配的应用有效。因此,多个应用【包括多个包名】需申请多个Key,或者对1个Key进行多次配置。
具体可参考 申请密钥 http://developer.baidu.com/map/geosdk-android-key.htm
PS:百度Android平台定位SDK 文档的确要比高德的文档写的详细完整一些,后面会介绍高德Android平台定位SDK
申请完Key之后就简单很多啦,这里以最新的V4.0版本为例。
1、导入库文件
将liblocSDK4.so文件拷贝到libs/armeabi目录下。将locSDK4.0.jar文件拷贝到工程的libs目录下,并在工程属性->Java Build Path->Libraries中选择“Add JARs”,选定locSDK4.0.jar,确定后返回。
2、设置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.baiduloc.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <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" /> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission> <uses-permission android:name="android.permission.READ_LOGS"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.baiduloc.test.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" > </service> </application> </manifest>
3、使用LocationClient定位
package com.baiduloc.test; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; public class MainActivity extends Activity implements OnClickListener { private static final String TAG = MainActivity.class.getSimpleName(); private TextView tv_address; private LocationClient mLocationClient; private MyLocationListenner myListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationClient = new LocationClient(getApplicationContext()); /**—————————————————————————————————————————————————————————————————— * 这里的AK和应用签名包名绑定,如果使用在自己的工程中需要替换为自己申请的Key * —————————————————————————————————————————————————————————————————— */ mLocationClient.setAK("dMGlsmpHOollXfGZ8jKfMpjQ"); myListener = new MyLocationListenner(); mLocationClient.registerLocationListener(myListener); findView(); } private void findView() { Button bt_start = (Button) findViewById(R.id.bt_start); tv_address = (TextView) findViewById(R.id.tv_address); bt_start.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_start: if (mLocationClient != null){ // if (mLocationClient != null && mLocationClient.isStarted()){ Log.i(TAG, "start position"); setLocationOption(); mLocationClient.start(); mLocationClient.requestLocation(); }else{ Log.d(TAG, "locClient is null or not started"); } break; default: break; } } //设置相关参数 private void setLocationOption(){ LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); //打开gps option.setCoorType("bd09ll"); //设置坐标类型 option.setServiceName("com.baidu.location.service_v2.9"); option.setAddrType("all"); option.setScanSpan(2000); //设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位 // option.setPriority(LocationClientOption.NetWorkFirst); //设置网络优先 option.setPriority(LocationClientOption.GpsFirst); //不设置,默认是gps优先 // option.setPoiNumber(5); //最多返回POI个数 // option.setPoiDistance(1000); //poi查询距离 // option.setPoiExtraInfo(true); //是否需要POI的电话和地址等详细信息 option.disableCache(true); mLocationClient.setLocOption(option); } /** * 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中 */ public class MyLocationListenner implements BDLocationListener { @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 : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); 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("\n省:"); sb.append(location.getProvince()); sb.append("\n市:"); sb.append(location.getCity()); sb.append("\n区/县:"); sb.append(location.getDistrict()); sb.append("\naddr : "); sb.append(location.getAddrStr()); } sb.append("\nsdk version : "); sb.append(mLocationClient.getVersion()); sb.append("\nisCellChangeFlag : "); sb.append(location.isCellChangeFlag()); Log.i(TAG, sb.toString()); tv_address.setText(sb.toString()); } public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null){ return ; } StringBuffer sb = new StringBuffer(256); sb.append("Poi time : "); sb.append(poiLocation.getTime()); sb.append("\nerror code : "); sb.append(poiLocation.getLocType()); sb.append("\nlatitude : "); sb.append(poiLocation.getLatitude()); sb.append("\nlontitude : "); sb.append(poiLocation.getLongitude()); sb.append("\nradius : "); sb.append(poiLocation.getRadius()); if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("\naddr : "); sb.append(poiLocation.getAddrStr()); } if(poiLocation.hasPoi()){ sb.append("\nPoi:"); sb.append(poiLocation.getPoi()); }else{ sb.append("noPoi information"); } Log.i(TAG, sb.toString()); } } @Override public void onStop() { if(mLocationClient!=null){ mLocationClient.stop(); mLocationClient = null; } super.onStop(); } }
Demo下载:http://download.csdn.net/detail/fx_sky/6877595