标签:百度地图 定位 sdk android
在前面几次我们分别完成了数据检索和线路规划的功能,但是如果我们不知道自己位置,检索起来说是不是会很麻烦呢?
知道自己位置就可以根据自己的位置进行检索和路线规划。
定位服务
在读地图的定位服务是与基本地图服务分开的所以需要引入另外的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>
涉及地图相关的知识,不再赘述
-
-
mMapView = (MapView) findViewById(R.id.bmapView);
-
-
map = mMapView.getMap();
-
-
map.setMyLocationEnabled(true);
-
-
-
-
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
-
-
map.setMyLocationConfigeration(new MyLocationConfiguration(LocationMode.NORMAL, true, mCurrentMarker));
基本的定位操作
-
client = new LocationClient(getApplicationContext());
-
listener = Mylisetener;
-
-
client.registerLocationListener(listener);
-
-
LocationClientOption option = new LocationClientOption();
-
option.setCoorType("bd09ll");
-
option.setScanSpan(5000);
-
option.setOpenGps(true);
-
-
-
client.setLocOption(option);
具体实现代码
百度地图开发之获取定位地点
标签:百度地图 定位 sdk android
原文地址:http://blog.csdn.net/u010687392/article/details/45478865