本文章主要实现定位精简化,和代码浓缩以及增强应用的性能
第一步:百度地图第三方加jar下载
第二部:关键jar:
这两个jar包主要是为了实现定位而用的
第三部:
定位精简型代码:
<permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
<uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.CAMERA" />
//这部十分关键:要在androidxmmlapplication和activity一样等级
<service
android:name="com.baidu.location.f"
android:enabled="true"
android:process=":remote" >
</service>
//定位主要代码:
/**
<span style="white-space:pre"> </span> * 获取位置信息
<span style="white-space:pre"> </span> */
<span style="white-space:pre"> </span>private void initLocation() {
<span style="white-space:pre"> </span>if (checkNet(getActivity())) {
<span style="white-space:pre"> </span>mLocationClient = new LocationClient(getActivity()); // 声明LocationClient类
<span style="white-space:pre"> </span>mLocationClient.registerLocationListener(mBDLocationListener); // 注册监听函数
<span style="white-space:pre"> </span>// 获取定位地址,配置返回的信息中包含的内容BDLocation bdLocation
<span style="white-space:pre"> </span>LocationClientOption option = mLocationClient.getLocOption();
<span style="white-space:pre"> </span>option.setOpenGps(true); // 打开gps
<span style="white-space:pre"> </span>option.setCoorType("bd09ll"); // 设置坐标类型
<span style="white-space:pre"> </span>option.setAddrType("all"); //<span style="color:#ff0000;">这一步十分关键,他关系到你 获取的地址里面是否有 addres这个属性</span>
<span style="white-space:pre"> </span>option.setScanSpan(1500); <span style="color:#ff6666;"> //定位的这个地方是周期性的进行的定位的,最后在后期获取地址之后进行定位停止</span><pre name="code" class="html"><span style="color:#ff6666;"><span style="white-space:pre"> </span>//mLocationClient.stop();</span>
<span style="color:#ff6666;"><span style="white-space:pre"> </span>//要不然要占用大量的内存,已经会出现多线程浪费流量的情况, </span>
<span style="white-space:pre"> </span>mLocationClient.setLocOption(option);
<span style="white-space:pre"> </span>mLocationClient.start();
<span style="white-space:pre"> </span>locate();
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>Toast.makeText(getActivity(), "网络不可用,请检查下!!", Toast.LENGTH_LONG)
<span style="white-space:pre"> </span>.show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// 定位
<span style="white-space:pre"> </span>private void locate() {
<span style="white-space:pre"> </span>if (!mLocationClient.isStarted()) {
<span style="white-space:pre"> </span>mLocationClient.start();
<span style="white-space:pre"> </span>} else {
<span style="white-space:pre"> </span>mLocationClient.requestLocation(); //这一个很好,他可以帮助你不用再进行数据大量加载,如果你已经开启了定位只是在原有的基础上进行简/<span style="white-space:pre"> </span> <span style="white-space:pre"> </span> //单的数据获取
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>public BDLocationListener mBDLocationListener = new BDLocationListener() {
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onReceiveLocation(BDLocation bdLocation) { //<span style="font-family: Arial, Helvetica, sans-serif;">bdLocation 包含了很多数据属性</span>
<span style="white-space:pre"> </span>if (bdLocation == null) {
<span style="white-space:pre"> </span>return;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (bdLocation.getAddrStr() != null) {
<span style="white-space:pre"> </span>mAddress = bdLocation.getAddrStr();
<span style="white-space:pre"> </span>bdLocation.getCity();
<span style="white-space:pre"> </span>area = bdLocation.getDistrict();
<span style="white-space:pre"> </span>// if (area.length() > 0)
<span style="white-space:pre"> </span>mAddrLongitude = bdLocation.getLongitude();
<span style="white-space:pre"> </span>mAddrLatitude = bdLocation.getLatitude();
<span style="white-space:pre"> </span>//mCanbeShowOnMaps = DiaryConfig.CanBeShowOnMaps;
<span style="white-space:pre"> </span>System.out.println("area:" + mAddress + bdLocation
<span style="white-space:pre"> </span>+ bdLocation.getCity() + area);
<span style="white-space:pre"> </span>if (area.length() > 0) {
<span style="white-space:pre"> </span>area = bdLocation.getCity().substring(0,
<span style="white-space:pre"> </span>bdLocation.getCity().length() - 1);
<span style="white-space:pre"> </span>Log.i(TAG, "位置信息:" + mAddress + "," + area + ","
<span style="white-space:pre"> </span>+ mAddrLongitude + "," + mAddrLatitude + ","
<span style="white-space:pre"> </span>+ mCanbeShowOnMaps);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if (!("".equals(mAddress))) {
<span style="white-space:pre"> </span>// Toast.makeText(getActivity(), ""+index,
<span style="white-space:pre"> </span>// Toast.LENGTH_LONG).show();
<span style="white-space:pre"> </span>//<span style="color:#ff0000;"> 定位次数为两次一共3秒</span>
<span style="white-space:pre"> </span><span style="color:#ff6666;">indexlocate++;
<span style="white-space:pre"> </span>tvLocation.setText(mAddress);
<span style="white-space:pre"> </span>if (mLocationClient.isStarted() && indexlocate > 2) {
<span style="white-space:pre"> </span>mLocationClient.stop();
<span style="white-space:pre"> </span>}</span>
<span style="white-space:pre"> </span>// mLocationClient = new LocationClient(getActivity()); //
<span style="white-space:pre"> </span>// 声明LocationClient类
<span style="white-space:pre"> </span>// mLocationClient.registerLocationListener(mBDLocationListener);
<span style="white-space:pre"> </span>// // 注册监听函数
<span style="white-space:pre"> </span>// // 获取定位地址,配置返回的信息中包含的内容BDLocation bdLocation
<span style="white-space:pre"> </span>// LocationClientOption option =
<span style="white-space:pre"> </span>// mLocationClient.getLocOption();
<span style="white-space:pre"> </span>// option.setOpenGps(true); // 打开gps
<span style="white-space:pre"> </span>// option.setCoorType("bd09ll"); // 设置坐标类型
<span style="white-space:pre"> </span>// option.setAddrType("all");
<span style="white-space:pre"> </span>// option.setScanSpan(200000);
<span style="white-space:pre"> </span>// mLocationClient.setLocOption(option);
<span style="white-space:pre"> </span>// mLocationClient.start();
<span style="white-space:pre"> </span>// locate();
<span style="white-space:pre"> </span>//if (!hasweather) {
<span style="white-space:pre"> </span>//<span style="white-space:pre"> </span>initWeather();
<span style="white-space:pre"> </span>//}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>// initWeather();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>public void onReceivePoi(BDLocation bdLocation) {
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>};