当前位置:   article > 正文

android原生定位_initlocation({

initlocation({
  1. public class LocationUtils {
  2. public static int GPS_REQUEST_CODE = 999;
  3. private LocationManager locationManager;
  4. private static final int REQUEST_CONTACTS = 1;
  5. private static final String[] PERMISSIONS_CONTACT = new String[]{
  6. Manifest.permission.ACCESS_COARSE_LOCATION,
  7. Manifest.permission.ACCESS_FINE_LOCATION
  8. };
  9. private Location location;
  10. static LocationUtils locationUtils;
  11. private BaseActivity activity;
  12. private double latitude = 0, longitude = 0;
  13. private int count;
  14. private boolean isHavaLocation;
  15. public static LocationUtils getInstance() {
  16. if (locationUtils == null) locationUtils = new LocationUtils();
  17. return locationUtils;
  18. }
  19. public void getLocations(final BaseActivity activity) {
  20. this.activity = activity;
  21. initLocation();
  22. }
  23. private void initLocation() {
  24. if (!isHavaLocation) {
  25. try {
  26. locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
  27. List<String> providers = locationManager.getProviders(true);
  28. String locationProvider = null;
  29. if (providers.contains(LocationManager.NETWORK_PROVIDER)) {
  30. locationProvider = LocationManager.NETWORK_PROVIDER;
  31. } else if (providers.contains(LocationManager.GPS_PROVIDER)) {
  32. locationProvider = LocationManager.GPS_PROVIDER;
  33. }
  34. if (Build.VERSION.SDK_INT >= 23) {
  35. if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
  36. || ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  37. requestSetPermissions();
  38. }
  39. }
  40. if(locationProvider != null){
  41. while (null == location && count < 3 && !isHavaLocation) {
  42. count += 1;
  43. isHavaLocation = false;
  44. location = locationManager.getLastKnownLocation(locationProvider);
  45. }
  46. if (location != null) {
  47. isHavaLocation = true;
  48. longitude = location.getLongitude();
  49. latitude = location.getLatitude();
  50. }//end of if
  51. locationManager.requestLocationUpdates(locationProvider, 100l, 0f, locationListener);
  52. }else {
  53. System.out.println("==================> locationProvider 为null");
  54. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
  55. if(!activity.isDestroyed()){
  56. DubDialogUtils.showOklDialog(activity, "请打开网络或GPS", new DialogInterface.OnClickListener() {
  57. @Override
  58. public void onClick(DialogInterface dialog, int which) {
  59. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
  60. //if(!activity.isDestroyed()) activity.finish();
  61. }
  62. }
  63. });
  64. }
  65. }
  66. }
  67. } catch (Exception e) {
  68. System.out.println("定位异常:" + e.getMessage());
  69. }
  70. }
  71. }
  72. private void requestSetPermissions() {
  73. if (!ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_COARSE_LOCATION) ||
  74. ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.ACCESS_FINE_LOCATION)) {
  75. ActivityCompat.requestPermissions(activity, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
  76. }
  77. }
  78. public LocationListener locationListener = new LocationListener() {
  79. @Override
  80. public void onStatusChanged(String provider, int status, Bundle extras) {
  81. }
  82. @Override
  83. public void onProviderEnabled(String provider) {
  84. }
  85. @Override
  86. public void onProviderDisabled(String provider) {
  87. }
  88. @Override
  89. public void onLocationChanged(Location location) {
  90. location.getAccuracy();//精确度
  91. latitude = location.getLatitude();
  92. longitude = location.getLongitude();
  93. }
  94. };
  95. public void removeLocationManager() {
  96. if (null != locationManager) locationManager.removeUpdates(locationListener);
  97. if (locationUtils != null) locationUtils = null;
  98. isHavaLocation = true;
  99. }
  100. public void openLocationGPS(final BaseActivity activity){
  101. if(null != activity){
  102. if(NotNetWork.isNetworkAvailable(activity)){
  103. //跳转GPS设置界面
  104. Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
  105. activity.startActivityForResult(intent, GPS_REQUEST_CODE);
  106. }
  107. }
  108. private static String gpsInfoConvert(double gpsInfo) {
  109. gpsInfo = Math.abs(gpsInfo);
  110. String dms = Location.convert(gpsInfo, Location.FORMAT_SECONDS);
  111. String[] splits = dms.split(":");
  112. String[] secondAry = (splits[2]).split("\\.");
  113. String seconds;
  114. if (secondAry.length == 0) {
  115. seconds = splits[2];
  116. } else {
  117. seconds = secondAry[0];
  118. }
  119. return splits[0] + "/1," + splits[1] + "/1," + seconds + "/1";
  120. }
  121. /**
  122. * @param latitude 经度
  123. * @param longitude 纬度
  124. * @return 详细位置信息 GeoCoder是基于后台backend的服务,因此这个方法不是对每台设备都适用。
  125. */
  126. public String convertAddress(Context context, double latitude, double longitude) {
  127. Geocoder mGeocoder = new Geocoder(context, Locale.getDefault());
  128. StringBuilder mStringBuilder = new StringBuilder();
  129. try {
  130. List<Address> mAddresses = mGeocoder.getFromLocation(latitude, longitude, 1);
  131. if (!mAddresses.isEmpty()) {
  132. Address address = mAddresses.get(0);
  133. mStringBuilder.append(address.getCountryName()).append(", ").append(address.getAdminArea()).append(", ").append(address.getLocality());
  134. }
  135. } catch (IOException e) {
  136. e.printStackTrace();
  137. }
  138. return mStringBuilder.toString();
  139. }
  140. public interface OnWritePictureExifInfoListener {
  141. void onWritePictureExifInfo(boolean isSuccess);
  142. }
  143. }

清单加上权限及配置

  1. <uses-permission android:name="android.permission.BAIDU_LOCATION_SERVICE" />
  2. <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
  3. <uses-feature android:name="android.hardware.location.gps" />

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/790352
推荐阅读
相关标签
  

闽ICP备14008679号