android定位二(百度定位SDK)

时间:2022-09-04 10:23:36

核心类

LocationClient类

用来发起定位,添加取消监听

LocationClientOption类

用来设置定位方式,包括是否启用缓存,使用gps,时间间隔等。

BDLocation类

定位结果的封装,包含坐标和错误码等信息。

BDLocationListener接口类

获取定位结果

BDNotifyListener 类

作用:位置提醒接口类,用于设定位置提醒点,以及实现监听函数。这个应该是适用于地图。


使用方法

定位是以service方式在运行,所以需要在manifest.xml中声明service组件

<service android:name="com.baidu.location.f" android:enabled="true" android:process=":remote" 
android:permission="android.permission.BAIDU_LOCATION_SERVICE">
<intent-filter>
<action android:name="com.baidu.location.service_v2.6"></action>
</intent-filter>
</service>

声明权限,看起来挺多的

<permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></permission>
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE"></uses-permission>
<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>

看一个demo中的源码

package location.demo;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class main extends Activity {

private LocationClient mLocationClient = null;
private Button mStartBtn = null;
private TextView mTextView = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

mStartBtn = (Button)findViewById(R.id.button1);
mTextView = (TextView)findViewById(R.id.textview1);

mLocationClient = new LocationClient(this);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);//打开gps
option.setCoorType("bd09ll");//设置坐标类型为bd09ll
option.setPriority(LocationClientOption.NetWorkFirst);//设置网络优先
option.setProdName("locSDKDemo2");//设置产品线名称
option.setScanSpan(5000);//定时定位,每隔5秒钟定位一次。
mLocationClient.setLocOption(option);
mLocationClient.registerLocationListener(new 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("\naddr : ");
sb.append(location.getAddrStr());
}
sb.append("\nsdk version : ");
sb.append(mLocationClient.getVersion());
mTextView.setText(sb.toString());
}

public void onReceivePoi(BDLocation location){
//return ;
}
});


mStartBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mLocationClient == null) return ;
if (mLocationClient.isStarted()){
mLocationClient.stop();
mStartBtn.setText("开始");
} else {
mLocationClient.start();
mStartBtn.setText("关闭");
}
}
});

}

@Override
public void onDestroy(){
if (mLocationClient != null && mLocationClient.isStarted()){
mLocationClient.stop();
mLocationClient = null;
}
super.onDestroy();
}




}