在前面几次我们分别完成了数据检索和线路规划的功能,但是如果我们不知道自己位置,检索起来说是不是会很麻烦呢?
知道自己位置就可以根据自己的位置进行检索和路线规划。
定位服务
在读地图的定位服务是与基本地图服务分开的所以需要引入另外的so文件盒jar包,如果你选择的是复合包的话,只用一如so文件就OK了。
首先在清单文件中加上如下的服务
<span class="sy0" style="color: rgb(51, 153, 51);"><</span>service android<span class="sy0" style="color: rgb(51, 153, 51);">:</span><span class="kw3" style="color: rgb(0, 0, 102);">name</span><span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(51, 102, 204);">"com.baidu.location.f"</span> android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>enabled<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(51, 102, 204);">"true"</span> android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>process<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(51, 102, 204);">":remote"</span><span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"></</span>service<span class="sy0" style="color: rgb(51, 153, 51);">></span>
然后就是加入定位所需要的权限
注意自己去除和地图权限相同的权限
<span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 这个权限用于进行网络定位<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.ACCESS_COARSE_LOCATION"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 这个权限用于访问GPS定位<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.ACCESS_FINE_LOCATION"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 用于访问wifi网络信息,wifi信息会用于进行网络定位<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.ACCESS_WIFI_STATE"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 获取运营商信息,用于支持提供运营商信息相关的接口<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.ACCESS_NETWORK_STATE"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.CHANGE_WIFI_STATE"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 用于读取手机当前的状态<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.READ_PHONE_STATE"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 写入扩展存储,向扩展卡写入数据,用于写入离线定位数据<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.WRITE_EXTERNAL_STORAGE"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> 访问网络,网络定位需要上网<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.INTERNET"</span> <span class="sy0" style="color: rgb(51, 153, 51);">/></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span> SD卡读取权限,用户写入离线定位数据<span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.MOUNT_UNMOUNT_FILESYSTEMS"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span> <span class="sy0" style="color: rgb(51, 153, 51);"><!--</span>允许应用读取低级别的系统日志文件 <span class="sy0" style="color: rgb(51, 153, 51);">--></span> <span class="sy0" style="color: rgb(51, 153, 51);"><</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>name<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(0, 0, 255);">"android.permission.READ_LOGS"</span><span class="sy0" style="color: rgb(51, 153, 51);">></</span>uses<span class="sy0" style="color: rgb(51, 153, 51);">-</span>permission<span class="sy0" style="color: rgb(51, 153, 51);">></span>
依然是 key文件,地图和定位使用同一个key文件
在清单文件的的APPlication节点下配置
<span class="sy0" style="color: rgb(51, 153, 51);"><</span>meta<span class="sy0" style="color: rgb(51, 153, 51);">-</span>data android<span class="sy0" style="color: rgb(51, 153, 51);">:</span><span class="kw3" style="color: rgb(0, 0, 102);">name</span><span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(51, 102, 204);">"com.baidu.lbsapi.API_KEY"</span> android<span class="sy0" style="color: rgb(51, 153, 51);">:</span>value<span class="sy0" style="color: rgb(51, 153, 51);">=</span><span class="st0" style="color: rgb(51, 102, 204);">"key"</span> <span class="sy0" style="color: rgb(51, 153, 51);">/></span>
涉及地图相关的知识,不再赘述
[java] view
plaincopyprint?
- // 获取地图控件引用
- mMapView = (MapView) findViewById(R.id.bmapView);
- map = mMapView.getMap();
- // 设置开启定位自己的位置
- map.setMyLocationEnabled(true);
- // 设置定位点图标,可以使用系统的也可以自定义
- mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
- // 如果使用系统的让mCurrentMarker为空即可
- map.setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true, mCurrentMarker));
基本的定位操作
[java] view
plaincopyprint?
- client = new LocationClient(getApplicationContext()); // 声明LocationClient类
- listener = Mylisetener;
- // 注册监听
- client.registerLocationListener(listener);
- LocationClientOption option = new LocationClientOption();
- option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
- option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
- option.setOpenGps(true);// 打开gps
- // option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
- // option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
- client.setLocOption(option);
具体实现代码
[java] view
plaincopyprint?
- package com.flyou.hellomap;
- import android.app.Activity;
- import android.os.Bundle;
- import com.baidu.location.BDLocation;
- import com.baidu.location.BDLocationListener;
- import com.baidu.location.LocationClient;
- import com.baidu.location.LocationClientOption;
- import com.baidu.mapapi.SDKInitializer;
- import com.baidu.mapapi.map.BaiduMap;
- import com.baidu.mapapi.map.BaiduMapOptions;
- import com.baidu.mapapi.map.BitmapDescriptor;
- import com.baidu.mapapi.map.BitmapDescriptorFactory;
- import com.baidu.mapapi.map.MapStatus;
- import com.baidu.mapapi.map.MapStatus.Builder;
- import com.baidu.mapapi.map.MapStatusUpdate;
- import com.baidu.mapapi.map.MapStatusUpdateFactory;
- import com.baidu.mapapi.map.MapView;
- import com.baidu.mapapi.map.MyLocationConfiguration;
- import com.baidu.mapapi.map.MyLocationConfiguration.LocationMode;
- import com.baidu.mapapi.map.MyLocationData;
- import com.baidu.mapapi.model.LatLng;
- import com.baidu.platform.comapi.map.s;
- public class LocationMapActivity extends Activity {
- private LocationClient client;
- private BDLocationListener listener;
- MapView mMapView = null;
- BitmapDescriptor mCurrentMarker;
- private boolean isFirstLoc = true;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- // 在使用SDK各组件之前初始化context信息,传入ApplicationContext
- // 注意该方法要再setContentView方法之前实现
- SDKInitializer.initialize(getApplicationContext());
- setContentView(R.layout.activity_main);
- // 获取地图控件引用
- mMapView = (MapView) findViewById(R.id.bmapView);
- map = mMapView.getMap();
- // 设置开启定位自己的位置
- map.setMyLocationEnabled(true);
- // 设置定位点图标,可以使用系统的也可以自定义
- mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
- // 如果使用系统的让mCurrentMarker为空即可
- map.setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true, mCurrentMarker));
- location();
- }
- private void location() {
- client = new LocationClient(getApplicationContext()); // 声明LocationClient类
- listener = Mylisetener;
- // 注册监听
- client.registerLocationListener(listener);
- LocationClientOption option = new LocationClientOption();
- option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02
- option.setScanSpan(5000);// 设置发起定位请求的间隔时间为5000ms
- option.setOpenGps(true);// 打开gps
- // option.setIsNeedAddress(true);// 返回的定位结果包含地址信息
- // option.setNeedDeviceDirect(true);// 返回的定位结果包含手机机头的方向
- client.setLocOption(option);
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- // 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
- mMapView.onDestroy();
- // map.setMyLocationEnabled(false);
- }
- @Override
- protected void onResume() {
- super.onResume();
- location();
- // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
- mMapView.onResume();
- client.start();
- }
- @Override
- protected void onPause() {
- super.onPause();
- // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
- mMapView.onPause();
- client.stop();
- }
- BDLocationListener Mylisetener = new BDLocationListener() {
- @Override
- public void onReceiveLocation(BDLocation location) {
- // map view 销毁后不在处理新接收的位置
- if (location == null || mMapView == null)
- return;
- MyLocationData locData = new MyLocationData.Builder().accuracy(location.getRadius())
- // 此处设置开发者获取到的方向信息,顺时针0-360
- .direction(100).latitude(location.getLatitude()).longitude(location.getLongitude()).build();
- map.setMyLocationData(locData);
- if (isFirstLoc) {
- isFirstLoc = false;
- LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
- // 缩放的范围是3-20
- MapStatusUpdate u = MapStatusUpdateFactory.newLatLngZoom(ll, 17f);
- map.animateMapStatus(u);
- }
- }
- };
- private BaiduMap map;
- }
时间: 2024-10-10 23:59:14