Android 百度定位SDKv4.2及6.0_百度定位实例_安卓定位实例
介绍
由于项目需要、前几天一直在研究百度定位的功能、通过不断的实践终于有结果了、不愿意独享
现在我把我的研究成果和大家分享一下、其实百度的 API 已经相当不错了
这之所以要写出来、一是自己做一个笔记、二是以方便一起朋友少走弯路
导入库文件
在相关下载最新的库文件、将liblocSDK4d.so文件拷贝到libs/armeabi目录下
将locSDK4.2.jar文件拷贝到工程的libs目录下、并在工程属性->Java Build Path->Libraries中选择“Add JARs”
选定locSDK4.2.jar、确定后返回、这样您就可以在程序中使用百度定位SDK了
设置AndroidManifest.xml
在application标签中声明service组件、每个app拥有自己单独的定位service
<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote"> </service>
声明使用权限
<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> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.WRITE_SETTINGS" />
设置AcessKey
使用SDK4.2需要在Mainfest.xml设置Accesskey、设置有误会引起定位和地理围栏服务不能正常使用
必须进行Accesskey的正确设置、设置AccessKey、在application标签中加入
<!-- meta-data需要写在application中 --> <meta-data android:name="com.baidu.lbsapi.API_KEY" android:value="3yoD80PlKwMoP9yHHvmhS77k" />
使用基本定位功能
基本定位功能指的根据用户实际需求、返回用户当前位置的基础定位服务
包含GPS和网络定位(Wi-Fi和基站定位)功能、基本定位功能同时还支持结合定位结果的反地理编码功能
离线定位、位置提醒功能和地理围栏功能
package com.baidu.baidulocationdemo; 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; import com.baidu.location.LocationClientOption.LocationMode; public class LocationActivity extends Activity{ private LocationClient mLocationClient; private TextView LocationResult; private Button startLocation; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.location); mLocationClient = new LocationClient(this.getApplicationContext()); mLocationClient.registerLocationListener(new MyLocationListener()); LocationResult = (TextView)findViewById(R.id.textView1); startLocation = (Button)findViewById(R.id.addfence); startLocation.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub InitLocation(); if(startLocation.getText().equals(getString(R.string.startlocation))){ mLocationClient.start(); startLocation.setText(getString(R.string.stoplocation)); }else{ mLocationClient.stop(); startLocation.setText(getString(R.string.startlocation)); } } }); } @Override protected void onStop() { // TODO Auto-generated method stub mLocationClient.stop(); super.onStop(); } private void InitLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式 //option.setCoorType(tempcoor);//返回的定位结果是百度经纬度,默认值gcj02 int span=1000; option.setScanSpan(span);//设置发起定位请求的间隔时间为5000ms option.setIsNeedAddress(true); mLocationClient.setLocOption(option); } /** * 实现实位回调监听 */ public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("error code : "); sb.append(location.getLocType()); sb.append("latitude : "); sb.append(location.getLatitude()); sb.append("lontitude : "); sb.append(location.getLongitude()); sb.append("radius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation){ sb.append("speed : "); sb.append(location.getSpeed()); sb.append("satellite : "); sb.append(location.getSatelliteNumber()); sb.append("direction : "); sb.append("addr : "); sb.append(location.getAddrStr()); sb.append(location.getDirection()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){ sb.append("addr : "); sb.append(location.getAddrStr()); //运营商信息 sb.append("operationers : "); sb.append(location.getOperators()); } LocationResult.setText(sb.toString()); Log.i("dwtedx", sb.toString()); } } }
到这里一个简单的百度定位demo就可以实现了、那么需要注意的是一定要在 libs 下面引入 armeabi/liblocSDK4d.so
若资源对你有帮助、扫描下方的二维码、关注DD博客微信公众号(ddblogs)吧
最后给贴上Demo的源代码、希望对大家有用、有兴趣的哥们可以下载看看
源代码下载链接: http://dwtedx.com/download.html?bdkey=s/1o689PBO 密码: m65a