Android 获取经纬度

需求

获取手机自带的经纬度

代码

1、配置列表中加入权限

    
    

2、代码


public class LocationActivity extends Activity {

    //入口是getLocation

    /**
     * 定位:权限判断
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private void getLocation() {
        //检查定位权限
        ArrayList permissions = new ArrayList<>();
        if (ActivityCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
        }
        if (ActivityCompat.checkSelfPermission(WebViewActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
        }

        //判断
        if (permissions.size() == 0) {//有权限,直接获取定位
            getLocationLL();
        } else {//没有权限,获取定位权限
            requestPermissions(permissions.toArray(new String[permissions.size()]), 2);

            LogUtil.d("*************", "没有定位权限");
        }
    }

    /**
     * 定位:获取经纬度
     */
    private void getLocationLL() {
        LogUtil.d("*************", "获取定位权限1 - 开始");
        Location location = getLastKnownLocation();
        if (location != null) {
            //传递经纬度给网页
            String result = "{code: '0',type:'2',data: {longitude: '" + location.getLongitude() + "',latitude: '" + location.getLatitude() + "'}}";
            wvShow.loadUrl("javascript:callback(" + result + ");");

            //日志
            String locationStr = "维度:" + location.getLatitude() + "\n"
                    + "经度:" + location.getLongitude();
            LogUtil.d("*************", "经纬度:" + locationStr);
        } else {
            Toast.makeText(this, "位置信息获取失败", Toast.LENGTH_SHORT).show();
            LogUtil.d("*************", "获取定位权限7 - " + "位置获取失败");
        }
    }

    /**
     * 定位:得到位置对象
     * @return
     */
    private Location getLastKnownLocation() {
        //获取地理位置管理器
        LocationManager mLocationManager = (LocationManager) getApplicationContext().getSystemService(LOCATION_SERVICE);
        List providers = mLocationManager.getProviders(true);
        Location bestLocation = null;
        for (String provider : providers) {
            Location l = mLocationManager.getLastKnownLocation(provider);
            if (l == null) {
                continue;
            }
            if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                // Found best last known location: %s", l);
                bestLocation = l;
            }
        }
        return bestLocation;
    }

    /**
     * 定位:权限监听
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 2://定位
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    LogUtil.d("*************", "同意定位权限");
                    getLocationLL();
                } else {
                    Toast.makeText(this, "未同意获取定位权限", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }


}

日志:

D/*************: 经纬度:维度:10.213194
    经度:100.592831

 

你可能感兴趣的:(【,Android,工具封装,】)