LocationApplication.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.baidu.baidulocationdemo;
  2. import com.baidu.location.BDLocation;
  3. import com.baidu.location.BDLocationListener;
  4. import com.baidu.location.LocationClient;
  5. import com.baidu.location.Poi;
  6. import android.app.Application;
  7. import android.app.Service;
  8. import android.os.Vibrator;
  9. import android.util.Log;
  10. import android.widget.TextView;
  11. import java.util.List;
  12. /**
  13. * 主Application,所有百度定位SDK的接口说明请参考线上文档:http://developer.baidu.com/map/loc_refer/index.html
  14. *
  15. * 百度定位SDK官方网站:http://developer.baidu.com/map/index.php?title=android-locsdk
  16. */
  17. public class LocationApplication extends Application {
  18. public LocationClient mLocationClient;
  19. public MyLocationListener mMyLocationListener;
  20. public TextView mLocationResult,logMsg;
  21. public TextView trigger,exit;
  22. public Vibrator mVibrator;
  23. @Override
  24. public void onCreate() {
  25. super.onCreate();
  26. mLocationClient = new LocationClient(this.getApplicationContext());
  27. mMyLocationListener = new MyLocationListener();
  28. mLocationClient.registerLocationListener(mMyLocationListener);
  29. mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
  30. }
  31. /**
  32. * 实现实时位置回调监听
  33. */
  34. public class MyLocationListener implements BDLocationListener {
  35. @Override
  36. public void onReceiveLocation(BDLocation location) {
  37. //Receive Location
  38. StringBuffer sb = new StringBuffer(256);
  39. sb.append("time : ");
  40. sb.append(location.getTime());
  41. sb.append("\nerror code : ");
  42. sb.append(location.getLocType());
  43. sb.append("\nlatitude : ");
  44. sb.append(location.getLatitude());
  45. sb.append("\nlontitude : ");
  46. sb.append(location.getLongitude());
  47. sb.append("\nradius : ");
  48. sb.append(location.getRadius());
  49. if (location.getLocType() == BDLocation.TypeGpsLocation){// GPS定位结果
  50. sb.append("\nspeed : ");
  51. sb.append(location.getSpeed());// 单位:公里每小时
  52. sb.append("\nsatellite : ");
  53. sb.append(location.getSatelliteNumber());
  54. sb.append("\nheight : ");
  55. sb.append(location.getAltitude());// 单位:米
  56. sb.append("\ndirection : ");
  57. sb.append(location.getDirection());
  58. sb.append("\naddr : ");
  59. sb.append(location.getAddrStr());
  60. sb.append("\ndescribe : ");
  61. sb.append("gps定位成功");
  62. } else if (location.getLocType() == BDLocation.TypeNetWorkLocation){// 网络定位结果
  63. sb.append("\naddr : ");
  64. sb.append(location.getAddrStr());
  65. //运营商信息
  66. sb.append("\noperationers : ");
  67. sb.append(location.getOperators());
  68. sb.append("\ndescribe : ");
  69. sb.append("网络定位成功");
  70. } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果
  71. sb.append("\ndescribe : ");
  72. sb.append("离线定位成功,离线定位结果也是有效的");
  73. } else if (location.getLocType() == BDLocation.TypeServerError) {
  74. sb.append("\ndescribe : ");
  75. sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因");
  76. } else if (location.getLocType() == BDLocation.TypeNetWorkException) {
  77. sb.append("\ndescribe : ");
  78. sb.append("网络不同导致定位失败,请检查网络是否通畅");
  79. } else if (location.getLocType() == BDLocation.TypeCriteriaException) {
  80. sb.append("\ndescribe : ");
  81. sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机");
  82. }
  83. sb.append("\nlocationdescribe : ");// 位置语义化信息
  84. sb.append(location.getLocationDescribe());
  85. List<Poi> list = location.getPoiList();// POI信息
  86. if (list != null) {
  87. sb.append("\npoilist size = : ");
  88. sb.append(list.size());
  89. for (Poi p : list) {
  90. sb.append("\npoi= : ");
  91. sb.append(p.getId() + " " + p.getName() + " " + p.getRank());
  92. }
  93. }
  94. logMsg(sb.toString());
  95. Log.i("BaiduLocationApiDem", sb.toString());
  96. }
  97. }
  98. /**
  99. * 显示请求字符串
  100. * @param str
  101. */
  102. public void logMsg(String str) {
  103. try {
  104. if (mLocationResult != null)
  105. mLocationResult.setText(str);
  106. } catch (Exception e) {
  107. e.printStackTrace();
  108. }
  109. }
  110. }