Android原生获取经纬度,传递给H5

1.加入权限

  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
   <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
   <!-- 用于申请调用A-GPS模块 -->

2.检查权限,动态申请权限

 if (ContextCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED   && ContextCompat.checkSelfPermission(LocationActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            getLocationLL();
    } else {
      ActivityCompat.requestPermissions(LocationActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 0);
    }



	//回调
	@Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            getLocationLL();
        } else {
            if (!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)
            &&!ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION)) {
            //弹出框 出设置界面
                IOSConfirm.Builder builder = new IOSConfirm.Builder(this);
                builder.setMessage(getResources().getString(R.string.no_permission));
                builder.setPositiveButton(getResources().getString(R.string.dialog_ok), (dialog, which) -> {
                    Intent localIntent = new Intent();
                    localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                    startActivity(localIntent);
                    finish();
                    dialog.cancel();
                });
                builder.setNegativeButton(getResources().getString(R.string.dialog_cancel), (dialog, which) -> {
                    dialog.cancel();
                    finish();
                });
                IOSConfirm dialog = builder.createConfirm();
                dialog.show();

            } else {
                finish();

            }
      }

    }

3.获取经纬度

private void getLocationLL() {
    Location location = getLastKnownLocation();
    if (location != null) {
    String locationStr = "纬度:" + location.getLatitude() + "\n"
                + "经度:" + location.getLongitude();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                String method = "javascript:gpsResult('" + locationStr + "')";
          		webView.loadUrl(method);
            }
        });
        LogUtils.d("经纬度:" + locationStr);
        this.finish();
    } else {
        Toast.makeText(Config.appContext, "位置信息获取失败", Toast.LENGTH_SHORT).show();
    }

}


//得到位置对象
private Location getLastKnownLocation() {
    //获取地理位置管理器
    LocationManager mLocationManager = (LocationManager) Config.appContext.getSystemService(LOCATION_SERVICE);
    List<String> providers = mLocationManager.getProviders(true);
    Location bestLocation = null;
    for (String provider : providers) {
        @SuppressLint("MissingPermission") Location l = mLocationManager.getLastKnownLocation(provider);
        if (l == null) {
            continue;
        }
        if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
            bestLocation = l;
        }
    }
    return bestLocation;
}

获取国家的方法,有需要可以加上

public String getAddress(double latitude, double longitude) {

        Geocoder geocoder = new Geocoder(this, Locale.getDefault());
        try {
            List<Address> addresses = geocoder.getFromLocation(latitude,
                    longitude, 1);
            StringBuilder sb = new StringBuilder();

            if (addresses.size() > 0) {
                Address address = addresses.get(0);
                for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                    sb.append(address.getAddressLine(i)).append("\n");
                }
                sb.append(address.getLocality()).append("\n");
                sb.append(address.getLocality()).append("\n");
                sb.append(address.getCountryName());
                addressStr = sb.toString();

                return addressStr;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "获取失败";
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值