使用百度API实现热点(WIFI)、GPS、基站定位

直接上代码。。。嘎嘎

/**
 * 百度基站定位错误返回码
 */
// 61 : GPS定位结果
// 62 : 扫描整合定位依据失败。此时定位结果无效。
// 63 : 网络异常,没有成功向服务器发起请求。此时定位结果无效。
// 65 : 定位缓存的结果。
// 66 : 离线定位结果。通过requestOfflineLocaiton调用时对应的返回结果
// 67 : 离线定位失败。通过requestOfflineLocaiton调用时对应的返回结果
// 68 : 网络连接失败时,查找本地离线定位时对应的返回结果
// 161: 表示网络定位结果
// 162~167: 服务端定位失败
// 502:KEY参数错误
// 505:KEY不存在或者非法
// 601:KEY服务被开发者自己禁用
// 602: KEY Mcode不匹配,意思就是您的ak配置过程中安全码设置有问题,请确保: sha1正确,“;”分号是英文状态;且包名是您当前运行应用的包名
// 501-700:KEY验证失败

public class BaiduActivity extends Activity implements OnClickListener {

	private static final String TAG = "BaiduActivity";
	private TextView mText;
	private TextView mTextPoi;
	private LocationClient mLocationClient = null;
	private BDLocationListener myListener = new MyLocationListener();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_weather);
		mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
		// mLocationClient.setAccessKey("8mrnaFzKu3DoduLnWuB5Lt2w"); //V4.1
		// mLocationClient.setAK("8mrnaFzKu3DoduLnWuB5Lt2w"); //V4.0
		mLocationClient.registerLocationListener(myListener); // 注册监听函数
		setLocationOption();
		mLocationClient.start();// 开始定位
		initWidgets();
	}

	private void initWidgets() {
		mText = (TextView) findViewById(R.id.tv_text);
		mTextPoi = (TextView) findViewById(R.id.tv_text_poi);
		Button btn = (Button) findViewById(R.id.btn_request);
		btn.setOnClickListener(this);
		btn = (Button) findViewById(R.id.btn_request_poi);
		btn.setOnClickListener(this);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mLocationClient.stop();// 停止定位
	}

	/**
	 * 设置相关参数
	 */
	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(true);
		option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
		option.setAddrType("all");// 返回的定位结果包含地址信息
		option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
		option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
		option.disableCache(true);// 禁止启用缓存定位
		option.setPoiNumber(5); // 最多返回POI个数
		option.setPoiDistance(1000); // poi查询距离
		option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息
		option.setPriority(LocationClientOption.NetWorkFirst); // 优先网络定位
		// option.setLocationMode(LocationMode.Battery_Saving);//设置定位模式
		mLocationClient.setLocOption(option);
	}

	public class MyLocationListener implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null)
				return;
			StringBuffer sb = new StringBuffer(256);
			sb.append("当前时间 : ");
			sb.append(location.getTime());
			sb.append("\n错误码 : ");
			sb.append(location.getLocType());
			sb.append("\n纬度 : ");
			sb.append(location.getLatitude());
			sb.append("\n经度 : ");
			sb.append(location.getLongitude());
			sb.append("\n半径 : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation) {
				sb.append("\n速度 : ");
				sb.append(location.getSpeed());
				sb.append("\n卫星数 : ");
				sb.append(location.getSatelliteNumber());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
				sb.append("\n地址 : ");
				sb.append(location.getAddrStr());
			}
			mText.setText(sb.toString());
			Log.d(TAG, "onReceiveLocation " + sb.toString());
		}

		public void onReceivePoi(BDLocation poiLocation) {
			// 将在下个版本中去除poi功能
			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");
			}
			mTextPoi.setText(sb.toString());
			Log.d(TAG, "onReceivePoi " + sb.toString());
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_request:
			if (mLocationClient != null && mLocationClient.isStarted())
				mLocationClient.requestLocation();
			else
				Log.d(TAG, "locClient is null or not started");
			break;
		case R.id.btn_request_poi:
			// 请求POI数据
			if (mLocationClient != null && mLocationClient.isStarted())
				mLocationClient.requestPoi();
			break;
		default:
			break;
		}
	}
}
代码下载BaiduLocation

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用百度地图API实现GPS步行轨迹,首先需要在Android项目中引入百度地图SDK。然后,可以按照以下步骤实现: 1. 在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 2. 在build.gradle文件中添加以下依赖: ```groovy implementation 'com.baidu.lbs:trace:6.2.0' ``` 3. 在布局文件中添加MapView: ```xml <com.baidu.mapapi.map.MapView android:id="@+id/map_view" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 4. 在Activity中初始化MapView: ```java public class MainActivity extends AppCompatActivity { private MapView mMapView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取地图控件引用 mMapView = (MapView) findViewById(R.id.map_view); // 初始化地图 BaiduMap baiduMap = mMapView.getMap(); // 开启定位图层 baiduMap.setMyLocationEnabled(true); } } ``` 5. 实现LocationListener接口,获取GPS位置信息: ```java public class MainActivity extends AppCompatActivity implements LocationListener { private MapView mMapView; private BaiduMap mBaiduMap; private LocationClient mLocationClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取地图控件引用 mMapView = (MapView) findViewById(R.id.map_view); // 初始化地图 mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 初始化定位客户端 mLocationClient = new LocationClient(this); // 注册定位监听器 mLocationClient.registerLocationListener(this); // 配置定位参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setCoorType("bd09ll"); option.setScanSpan(1000); // 每隔1秒定位一次 mLocationClient.setLocOption(option); // 开始定位 mLocationClient.start(); } @Override public void onLocationChanged(BDLocation location) { if (location == null) return; // 获取经纬度 double latitude = location.getLatitude(); double longitude = location.getLongitude(); // 构造定位数据 MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) .direction(location.getDirection()) .latitude(latitude) .longitude(longitude) .build(); // 设置定位数据 mBaiduMap.setMyLocationData(locData); // 更新地图状态 MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(new LatLng(latitude, longitude)); mBaiduMap.animateMapStatus(update); } } ``` 6. 使用百度地图的Trace功能记录步行轨迹: ```java public class MainActivity extends AppCompatActivity implements LocationListener { private MapView mMapView; private BaiduMap mBaiduMap; private LocationClient mLocationClient; private Trace mTrace; private long mServiceId; private String mEntityName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取地图控件引用 mMapView = (MapView) findViewById(R.id.map_view); // 初始化地图 mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 初始化定位客户端 mLocationClient = new LocationClient(this); // 注册定位监听器 mLocationClient.registerLocationListener(this); // 配置定位参数 LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setCoorType("bd09ll"); option.setScanSpan(1000); // 每隔1秒定位一次 mLocationClient.setLocOption(option); // 开始定位 mLocationClient.start(); // 初始化Trace mTrace = new Trace(getApplicationContext(), 123456, "mytrace", true); // 开启服务 mServiceId = mTrace.getServiceId(); // 设置实体名称 mEntityName = "myentity"; } @Override public void onLocationChanged(BDLocation location) { if (location == null) return; // 获取经纬度 double latitude = location.getLatitude(); double longitude = location.getLongitude(); // 构造定位数据 MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) .direction(location.getDirection()) .latitude(latitude) .longitude(longitude) .build(); // 设置定位数据 mBaiduMap.setMyLocationData(locData); // 更新地图状态 MapStatusUpdate update = MapStatusUpdateFactory.newLatLng(new LatLng(latitude, longitude)); mBaiduMap.animateMapStatus(update); // 添加轨迹点 LatLng point = new LatLng(latitude, longitude); TraceLocation traceLocation = new TraceLocation.Builder() .setLatitude(latitude) .setLongitude(longitude) .setSpeed(location.getSpeed()) .setBearing(location.getDirection()) .setTime(System.currentTimeMillis() / 1000) .build(); List<TraceLocation> locations = new ArrayList<>(); locations.add(traceLocation); mTrace.track(locations, mServiceId, mEntityName, new OnTrackListener() { @Override public void onRequestFailedCallback(String s) { Log.e("MainActivity", "轨迹上传失败:" + s); } @Override public void onTraceProcessingCallback(int i, String s) { Log.i("MainActivity", "轨迹上传中:" + s); } @Override public void onTraceCallback(byte[] bytes, String s) { Log.i("MainActivity", "轨迹上传成功:" + s); } }); } } ``` 以上就是使用百度地图API实现GPS步行轨迹的步骤。需要注意的是,为了保证定位精度和轨迹记录效果,可以根据具体需求调整定位参数和轨迹记录频率。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值