百度LBS定位怎么用

PHPz
Freigeben: 2020-06-24 17:42:35
Original
3230 Leute haben es durchsucht

百度LBS定位怎么用

百度LBS定位怎么用?

up主最近在写一个新项目,关于借款的一个app,正好更具需求用到了定位,其实对于一般程序员来说,定位可能是最基本的了吧,之前我在项目中用到了高德定位,这次根据需求选择了百度定位,话不多说,直接走起;

1.在Application标签中声明SERVICE组件。

2.声明使用权限,这里复制Api文档内容即可:

812b676485d3ba8aeae90f531229d89.png

3.那就开始配置你的appkey了;

4.我是写到一个工具类里边在MainActivity里边oncreat方法下添加这样的一句话即可;

006e57efa928d7d13ea8ddd09936b3b.png

5.最后我们在log日志里看一下,可以在把全部位置信息传给后台,这里我需要的就是一个city显示。

c38db752ca495b7e2bc78111deb1608.png

6。这样的就完成了一个定位,这是第一次写,逻辑一定是对的,可能会表述不太明白,我会继续努力的,感谢!!!

备注:以下是我的代码

public classMyLocationUtil {
public staticLocationClientmLocationClient=null;
public staticBDLocationListenermyListener=newMyLocationListener();
public static voidgetLocation(Context context) {
mLocationClient=newLocationClient(context);//声明LocationClient类
mLocationClient.registerLocationListener(myListener);//注册监听函数
initLocation();
mLocationClient.start();
}
/**
*设置定位参数
*高精度定位模式:这种定位模式下,会同时使用网络定位和GPS定位,优先返回最高精度的定位结果;
低功耗定位模式:这种定位模式下,不会使用GPS进行定位,只会使用网络定位(WiFi定位和基站定位);
仅用设备定位模式:这种定位模式下,不需要连接网络,只使用GPS进行定位,这种模式下不支持室内环境的定位。
*/
private static voidinitLocation(){
LocationClientOption option =newLocationClientOption();
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy
);//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备
option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系
intspan=1000;
option.setScanSpan(0);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的
option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要
option.setOpenGps(true);//可选,默认false,设置是否使用gps
option.setLocationNotify(true);//可选,默认false,设置是否当GPS有效时按照1S/1次频率输出GPS结果
option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京天安门附近”
option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到
option.setIgnoreKillProcess(false);//可选,默认true,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认不杀死
option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集
option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤GPS仿真结果,默认需要
mLocationClient.setLocOption(option);
}
public static classMyLocationListenerimplementsBDLocationListener {
@Override
public voidonReceiveLocation(BDLocation location) {
mLocationClient.stop();
//Receive Location
StringBuffer sb =newStringBuffer(256);
sb.append("periods : ");
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){// GPS定位结果
sb.append("\nspeed : ");
sb.append(location.getSpeed());//单位:公里每小时
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\nheight : ");
sb.append(location.getAltitude());//单位:米
sb.append("\ndirection : ");
sb.append(location.getDirection());//单位度
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ndescribe : ");
sb.append("gps定位成功");
}else if(location.getLocType() == BDLocation.TypeNetWorkLocation){//网络定位结果
sb.append("\naddr : ");
sb.append(location.getAddrStr());
sb.append("\ncity:");
sb.append(location.getCity());
//运营商信息
sb.append("\noperationers : ");
sb.append(location.getOperators());
sb.append("\ndescribe : ");
sb.append("网络定位成功");
}else if(location.getLocType() == BDLocation.TypeOffLineLocation) {//离线定位结果
sb.append("\ndescribe : ");
sb.append("离线定位成功,离线定位结果也是有效的");
}else if(location.getLocType() == BDLocation.TypeServerError) {
sb.append("\ndescribe : ");
sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
}else if(location.getLocType() == BDLocation.TypeNetWorkException) {
sb.append("\ndescribe : ");
sb.append("网络不同导致定位失败,请检查网络是否通畅");
}else if(location.getLocType() == BDLocation.TypeCriteriaException) {
sb.append("\ndescribe : ");
sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
sb.append("\nlocationdescribe : ");
sb.append(location.getLocationDescribe());//位置语义化信息
List list = location.getPoiList();// POI数据
if(list !=null) {
sb.append("\npoilist size = : ");
sb.append(list.size());
for(Poi p : list) {
sb.append("\npoi= : ");
sb.append(p.getId() +" "+ p.getName() +" "+ p.getRank());
}
}
Log.i("yunxiao",sb.toString());
}}
}
Nach dem Login kopieren

更多相关知识,请访问PHP中文网

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!