(五)GPRS定位的实现

前段时间在弄GPRS定位的问题,使用google的地图定位,大家也都知道,google现在在中国境内有很多限制,而且国外刷机严重,难免将google的各种服务给刷掉,所以最终采用百度的定位系统,完美实现。现在有时间了,给大家讲一讲,代码并不多。
我还是先说说google的定位吧,说不定有些仁兄需要的呢!
首先判断机器的GPRS模块是否正常,如果不正常,那没办法了,哪家的定位系统都不能用。
[html]   view plain   copy
  print?
  1. LocationManager alm = (LocationManager) this  
  2.         .getSystemService(Context.LOCATION_SERVICE);  
  3. if (alm.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER)) {  
  4.     Toast.makeText(this, "GPS模块正常", Toast.LENGTH_SHORT).show();  
  5.     return;  
  6. }  
设置开启GPRS页面
[html]   view plain   copy
  print?
  1. Toast.makeText(this, "请开启GPS!", Toast.LENGTH_SHORT).show();  
  2. Intent intent = new Intent(Settings.ACTION_SECURITY_SETTINGS);  
  3. startActivityForResult(intent, 0); // 此为设置完成后返回到获取界面  

设置省电模式,获得最好的定位方式
[html]   view plain   copy
  print?
  1. locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);  
  2. gprs_view = (TextView) findViewById(R.id.gprs_view);  
  3. Criteria criteria = new Criteria();  
  4. // 获得最好的定位效果  
  5. criteria.setAccuracy(Criteria.ACCURACY_COARSE);  
  6. criteria.setAltitudeRequired(false);  
  7. criteria.setBearingRequired(false);  
  8. criteria.setCostAllowed(false);  
  9. // 使用省电模式  
  10. criteria.setPowerRequirement(Criteria.POWER_LOW);  
  11. // 获得当前的位置提供者  
  12. provider = locationManager.getBestProvider(criteria, false);  
  13. ser.append(provider);  
  14. locationManager.requestLocationUpdates(provider, 2000, 10, this);  

获得上次location对象
[html]   view plain   copy
  print?
  1. // 使用网络定位,获得上次定位的location对象  
  2.             if (location == null) {  
  3.                 location = locationManager  
  4.                         .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);  
  5.                 provider = LocationManager.NETWORK_PROVIDER;  
  6.             }  

然后定位
[html]   view plain   copy
  print?
  1. String latLongString;  
  2. if (location != null) {  
  3.     double lat = location.getLatitude();  
  4.     double lng = location.getLongitude();  
  5.     latLongString = "纬度:" + lat + "\n经度:" + lng;  
  6.     Geocoder gc = new Geocoder(context);  
  7.     List<Address> addresses = null;  
  8.     try {  
  9.         addresses = gc.getFromLocation(location.getLatitude(),  
  10.                 location.getLongitude(), 1);  
  11.     } catch (IOException e) {  
  12.         // TODO Auto-generated catch block  
  13.         e.printStackTrace();  
  14.     }  
  15.     ser.append("\n" + addresses.get(0).getCountryName());  
  16. } else {  
  17.     latLongString = "无法获取地理信息";  
  18. }  
  19. ser.append("\n" + "您当前的位置是:\n" + latLongString);  

实现LocationListener接口,并在onLocationChanged和onProviderDisabled方法中实现updateWithNewLocation方法
以期待在未获得location对象时,不断获取直到取到为止
[html]   view plain   copy
  print?
  1. private void updateWithNewLocation(Location location) {  
  2.     // TODO Auto-generated method stub  
  3.     if (location == null) {  
  4.         locationManager.requestLocationUpdates(provider, 2000, (float) 0.1,  
  5.                 this);  
  6.     }  
  7. }  

以上是我弄到的关于用google开发服务的资料,实际上次定位的位置很难得到,实现定位,比较困难,也许是笔者使用的是水货,刷过机的原因吧。

下面是百度的定位,可以说都能实现吧
首先请大家看效果图,是实现了的!PS:朝鲜金胖子,看到我的经纬度乱来啊!

百度的定位相对来说要简单的多,为什么呢,因为它只有两三个方法,一般国内的手机GPS功能有被“阉割”的可能,所以一般GPS定位取不到位置,通用的还是GPRS网络定位功能。
如图,导入项目所需包


然后在manifest.xml中加入权限,以及定义Service
[html]   view plain   copy
  print?
  1. <span style="font-size:18px;">    <application  
  2.         android:name="com.baidu.locSDK.test.Location"  
  3.         android:icon="@drawable/icon"  
  4.         android:label="@string/app_name" >  
  5.         <activity  
  6.             android:name="mainActivity"  
  7.             android:configChanges="orientation|keyboardHidden"  
  8.             android:label="@string/app_name" >  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.   
  12.                 <category android:name="android.intent.category.LAUNCHER" />  
  13.             </intent-filter>  
  14.         </activity>  
  15.   
  16.         <service  
  17.             android:name="com.baidu.location.f"  
  18.             android:enabled="true"  
  19.             android:process=":remote" >  
  20.         </service>  
  21.     </application>  
  22.   
  23.     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />  
  24.     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />  
  25.     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
  26.     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
  27.     <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />  
  28.     <uses-permission android:name="android.permission.READ_PHONE_STATE" />  
  29.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
  30.     <uses-permission android:name="android.permission.INTERNET" />  
  31.     <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />  
  32.     <uses-permission android:name="android.permission.READ_LOGS" />  
  33.     <uses-permission android:name="android.permission.VIBRATE" /></span>  

主要代码如下,但要先打开网络
[html]   view plain   copy
  print?
  1. mStartBtn.setOnClickListener(new OnClickListener() {  
  2.     @Override  
  3.     public void onClick(View v) {  
  4.         if (!mIsStart) {  
  5.             setLocationOption();  
  6.             mLocClient.start();  
  7.             mStartBtn.setText("开始");  
  8.             mIsStart = true;  
  9.         } else {  
  10.             mLocClient.stop();  
  11.             mIsStart = false;  
  12.             mStartBtn.setText("结束");  
  13.         }  
  14.         Log.d("locSDK_Demo1",  
  15.                 "... mStartBtn onClick... pid=" + Process.myPid()  
  16.                         + " count=" + count++);  
  17.     }  
  18. });  

[html]   view plain   copy
  print?
  1. private void setLocationOption() {  
  2.     LocationClientOption option = new LocationClientOption();  
  3.     option.setOpenGps(mGpsCheck.isChecked()); // gps  
  4.     option.setCoorType(mCoorEdit.getText().toString());  
  5.     option.setAddrType(mAddrEdit.getText().toString());  
  6.     option.setScanSpan(Integer.parseInt(mSpanEdit.getText().toString()));  
  7.     mLocClient.setLocOption(option);  
  8. }  

最终展示出来
[html]   view plain   copy
  print?
  1. public void logMsg(String str) {  
  2.     try {  
  3.         mData = str;  
  4.         if ( mTv != null )  
  5.             mTv.setText(mData);  
  6.     } catch (Exception e) {  
  7.         e.printStackTrace();  
  8.     }  
  9. }  
  10.   
  11. public class MyLocationListenner implements BDLocationListener {  
  12.     @Override  
  13.     public void onReceiveLocation(BDLocation location) {  
  14.         if (location == null)  
  15.             return ;  
  16.         StringBuffer sb = new StringBuffer(256);  
  17.         sb.append("time : ");  
  18.         sb.append(location.getTime());  
  19.         sb.append("\nerror code : ");  
  20.         sb.append(location.getLocType());  
  21.         sb.append("\nlatitude : ");  
  22.         sb.append(location.getLatitude());  
  23.         sb.append("\nlontitude : ");  
  24.         sb.append(location.getLongitude());  
  25.         sb.append("\nradius : ");  
  26.         sb.append(location.getRadius());  
  27.         if (location.getLocType() == BDLocation.TypeGpsLocation){  
  28.             sb.append("\nspeed : ");  
  29.             sb.append(location.getSpeed());  
  30.             sb.append("\nsatellite : ");  
  31.             sb.append(location.getSatelliteNumber());  
  32.         } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){  
  33.             sb.append("\nprovince:");  
  34.             sb.append(location.getProvince());  
  35.             sb.append("\ncity");  
  36.             sb.append(location.getCity());  
  37.             sb.append("\nstreet");  
  38.             sb.append(location.getDistrict());  
  39.             sb.append("\naddr : ");  
  40.             sb.append(location.getAddrStr());  
  41.         }  
  42.         sb.append("\nsdk version : ");  
  43.         sb.append(mLocationClient.getVersion());  
  44.         logMsg(sb.toString());  
  45.     }  
  46.       
  47.     public void onReceivePoi(BDLocation poiLocation) {  
  48.         if (poiLocation == null){  
  49.             return ;  
  50.         }  
  51.         StringBuffer sb = new StringBuffer(256);  
  52.         sb.append("Poi time : ");  
  53.         sb.append(poiLocation.getTime());  
  54.         sb.append("\nerror code : ");  
  55.         sb.append(poiLocation.getLocType());  
  56.         sb.append("\nlatitude : ");  
  57.         sb.append(poiLocation.getLatitude());  
  58.         sb.append("\nlontitude : ");  
  59.         sb.append(poiLocation.getLongitude());  
  60.         sb.append("\nradius : ");  
  61.         sb.append(poiLocation.getRadius());  
  62.         if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation){  
  63.             sb.append("\naddr : ");  
  64.             sb.append(poiLocation.getAddrStr());  
  65.         }   
  66.         if(poiLocation.hasPoi()){  
  67.             sb.append("\nPoi:");  
  68.             sb.append(poiLocation.getPoi());  
  69.         }else{                
  70.             sb.append("noPoi information");  
  71.         }  
  72.         logMsg(sb.toString());  
  73.     }  
  74. }  

就是这样,一个麻烦至极的定位功能完成了!源码下载地址: http://download.csdn.net/detail/liuxian13183/5088512
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值