android 导入android.jar ,获取经纬度

前言:项目中用到了获取当前的经纬度,提供一个封装好的方法,直接调用即可。

工具类Utils:

public class LocationUtils {
    private static final String TAG = "LocationUtils";
    private UpdateLocationListener updateLocationListener;
    private LocationManager manager;
//            经度=120.107754, 纬度=30.287623

    /**
     * 这是普通的网络定位以及gps定位,我这里优先考虑网络定位,因为网络定位局限性比较小
     * 并且速度快,第三个参数代表是否是内网,因为使用内网的sim卡是无法进行网络定位的
     */
    public void normalLocation(Activity activity, UpdateLocationListener updateLocationListener, boolean insideSim) {
        LocationUtils.this.updateLocationListener = updateLocationListener;
        manager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        if (insideSim) {
            if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                        && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    return;
                }
                //如果在GPS是开启的情况下并且选择是仅使用GPS定位服务,还是定位一直失败的话,请重启手机
//                ToastUtils.showToast(activity,"如果是在室内请走到窗户旁边,这样方便GPS定位");
                manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1,
                        locationListener);
            } else {
                AlertDialog.Builder builder = new AlertDialog.Builder(activity);
                builder.setMessage("您的GPS没有打开,请打开GPS并且选择仅适用GPS选项");
              
   builder.setPositiveButton("去打开", new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialogInterface, int i) {
         openGPS(activity);
      }
      });
   builder.setNegativeButton("忽略", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialogInterface, int i) {
        ToastUtils.showToast(activity,"已忽略");
     }
    });
AlertDialog dialog = builder.show(); dialog.setCancelable(false); } } else { if (manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) { //优先考虑网络定位 // 备注:参数2和3,如果参数3不为0,则以参数3为准;参数3为0,则通过时间来定时更新;两者为0,则随时刷新 // 1秒更新一次,或最小位移变化超过1米更新一次; // 注意:此处更新准确度非常低,推荐在service里面启动一个Thread,在run中sleep(10000);然后执行handler.sendMessage(),更新位置 if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locationListener); } else if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener); } else { ToastUtils.showToast(activity, "请打开GPS或网络"); } } } // 位置监听 private LocationListener locationListener = new LocationListener() { /** * 位置信息变化时触发 */ public void onLocationChanged(Location location) { if (location != null && updateLocationListener != null) { updateLocationListener.update(location);// ToastUtils.showToast(BaseApplication.getContext(),"获取到了定位信息"); } } /** * GPS状态变化时触发 */ public void onStatusChanged(String provider, int status, Bundle extras) { } /** * GPS开启时触发 */ public void onProviderEnabled(String provider) { } /** * GPS禁用时触发 */ public void onProviderDisabled(String provider) { } }; public interface UpdateLocationListener { void update(Location location); } public void reset() { updateLocationListener = null; if (manager != null && locationListener != null) manager.removeUpdates(locationListener); } public static final void openGPS(Activity context) { //跳转到开启GPS界面 Intent locationIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); context.startActivityForResult(locationIntent, 50); } public void requestResult(int resultCode, final Activity activity) { if (resultCode == 50) { if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { ToastUtils.showToast(activity, "如果是在室内请走到窗户旁边,这样方便GPS定位"); if (ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { return; } manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener); } else { ToastUtils.showToast(activity, "还是没有打开GPS,无法使用拍照和录音功能"); } } else { Log.i(TAG, "code != 50"); } }}

然后直接写个initPermission方法调用即可。

 private void initPermission2() {
        LocationUtils locationUtils = new LocationUtils();
       if (EasyPermissions.hasPermissions(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            locationUtils.normalLocation(getActivity(), location -> this.location = location, true);
        } else {
           ToastUtils.showToast(getActivity(), "没有定位权限");
    }}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页