| | |
| | | import android.annotation.SuppressLint; |
| | | import android.content.Context; |
| | | import android.content.pm.PackageManager; |
| | | import android.location.Address; |
| | | import android.location.Geocoder; |
| | | import android.location.Location; |
| | | import android.location.LocationListener; |
| | | import android.location.LocationManager; |
| | | import android.os.Bundle; |
| | | |
| | | |
| | | |
| | | |
| | | import java.io.IOException; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.List; |
| | | import java.util.Locale; |
| | | |
| | | import androidx.annotation.NonNull; |
| | | import androidx.core.app.ActivityCompat; |
| | | |
| | | import com.hdl.photovoltaic.other.HdlLogLogic; |
| | |
| | | return Singleton.INSTANCE; |
| | | } |
| | | |
| | | public String getCountryCode(Context context) throws IOException { |
| | | LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); |
| | | if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { |
| | | // 处理权限请求 |
| | | return null; |
| | | } |
| | | Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); |
| | | if (location == null) { |
| | | location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER); |
| | | } |
| | | if (location != null) { |
| | | Geocoder geocoder = new Geocoder(context, Locale.getDefault()); |
| | | List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); |
| | | if (addresses != null && !addresses.isEmpty()) { |
| | | return addresses.get(0).getCountryCode(); |
| | | } |
| | | } |
| | | |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * 开启定位 |
| | | * |
| | | */ |
| | | @SuppressLint("MissingPermission") |
| | | public void getLocation(Context context) { |
| | |
| | | //2.获取位置提供器,GPS或是NetWork |
| | | List<String> providers = locationManager.getProviders(true); |
| | | // locationProvider = LocationManager.GPS_PROVIDER; |
| | | if (providers.contains(LocationManager.NETWORK_PROVIDER)) { |
| | | //如果是网络定位 |
| | | locationProvider = LocationManager.NETWORK_PROVIDER; |
| | | } else if (providers.contains(LocationManager.GPS_PROVIDER)) { |
| | | if (providers.contains(LocationManager.GPS_PROVIDER)) { |
| | | //如果是GPS定位 |
| | | locationProvider = LocationManager.GPS_PROVIDER; |
| | | } else if (providers.contains(LocationManager.NETWORK_PROVIDER)) { |
| | | //如果是网络定位 |
| | | locationProvider = LocationManager.NETWORK_PROVIDER; |
| | | } else { |
| | | return; |
| | | } |
| | |
| | | showLocation(location); |
| | | } else { |
| | | // 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace |
| | | locationManager.requestLocationUpdates(locationProvider, 60*1000, 10, mListener); |
| | | //60秒更省电 |
| | | locationManager.requestLocationUpdates(locationProvider, 30 * 1000, 10, mListener); |
| | | } |
| | | } |
| | | |
| | |
| | | BigDecimal latitude = BigDecimal.valueOf(location.getLatitude()); |
| | | String lon = longitude.setScale(6, RoundingMode.HALF_DOWN).doubleValue() + ""; |
| | | String lat = latitude.setScale(6, RoundingMode.HALF_DOWN).doubleValue() + ""; |
| | | // HdlLogLogic.print("纬度--->" + lon, false); |
| | | // HdlLogLogic.print("经度--->" + lat, false); |
| | | HdlLogLogic.print("纬度--->" + lon, false); |
| | | HdlLogLogic.print("经度--->" + lat, false); |
| | | // if(!TextUtils.isEmpty(lon)&&!TextUtils.isEmpty(lat)){ |
| | | // DataController.getInstance().lonLatUpdate(lon,lat,new IResponseCallBack<String>() { |
| | | // @Override |
| | |
| | | |
| | | // 如果位置发生变化,重新显示 |
| | | @Override |
| | | public void onLocationChanged(Location location) { |
| | | public void onLocationChanged(@NonNull Location location) { |
| | | showLocation(location); |
| | | } |
| | | }; |