百度定位

1,,定义变量

private LocationClient mLocationClient;

public MyLocationListener mMyLocationListener;

2,初始化地图控件

mLocationClient = new LocationClient(this.getApplicationContext());
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);// 位置寄存器
mLocationClient.start();

3、初始化地图

private void InitLocation() {
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);// 设置定位模式(高精度)
option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,百度加密经纬度坐标
option.setScanSpan(1000);// 设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);// 反向地理编码
mLocationClient.setLocOption(option);

}

4、在线程中执行低位结果集

public class MyLocationListener implements BDLocationListener {


@Override
public void onReceiveLocation(BDLocation location) {


StringBuffer sb = new StringBuffer(256);
sb.append("time : ");
sb.append(location.getTime()); // 获取定位时间
sb.append("\n纬度 : ");
sb.append(location.getLatitude());// 纬度
sb.append("\n经度 : ");
sb.append(location.getLongitude());// 经度
sb.append("\ncitycode : ");// 城市编码
sb.append(location.getCityCode());
sb.append("\ncity : ");// 城市
sb.append(location.getCity());
sb.append("\nDistrict : ");// 区
sb.append(location.getDistrict());
sb.append("\nStreet : ");// 街道
sb.append(location.getStreet());
sb.append("\naddr : ");// 地址信息
sb.append(location.getAddrStr());
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("\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("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
}
// System.out.println("当前市级位置:" + location.getCity());
// System.out.println("当前县级位置:" + location.getDistrict());
String countyname = location.getDistrict();
String cityname = location.getCity();
// data.setText(location.getTime());
city.setText(location.getDistrict());
if (NetHelper.IsHaveInternet(mContext)) {
initData(cityname, countyname);  
} else {
Toast.makeText(mContext, "请检查网络连接...", 1000).show();
}
}
}

代码就这些了,需要以来的第三方包去百度开发者平台下载基础定位包


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雒成龙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值