这部分主要阐述如何获取手机定位的经纬度数据,逻辑较简单,所以下面以代码为主
//对于定位服务权限判断 private void init_right() { if (mBluetoothAdapter.isEnabled()) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("This app needs location access"); builder.setMessage("Please grant location access so this app can detect Bluetooth."); builder.setPositiveButton(android.R.string.ok, null); builder.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); } } }); builder.show(); } if(this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_FINE_LOCATION); } } } }
首先我们必须了解部分权限自从Android6.0以后需要动态申请,这里主要最重要的是requestPermissions,如果6.0之前的,那么直接Manifiest里面添加权限就好。
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case PERMISSION_REQUEST_COARSE_LOCATION: { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { // Log.d(TAG, "coarse location permission granted"); finish(); } else { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Functionality limited"); builder.setMessage("Since location access has not been granted this app will not be able to discover beacons when in the background."); builder.setPositiveButton(android.R.string.ok, null); builder.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { } }); builder.show(); } break; } case PERMISSION_REQUEST_FINE_LOCATION:{ if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { // Log.d(TAG, "coarse location permission granted"); finish(); } else { final AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Functionality limited"); builder.setMessage("Since location access has not been granted this app will not be able to discover beacons when in the background."); builder.setPositiveButton(android.R.string.ok, null); builder.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { } }); builder.show(); } break; } } }
这里是回调,如果申请成功,直接关闭,否则跳出错误提示框。
//开启实时定位 private void init_location(){ //获取定位服务 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); //获取当前可用的位置控制器 List<String> list = locationManager.getProviders(true); provider = LocationManager.NETWORK_PROVIDER; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ if (this.checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. mLocation = locationManager.getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 1000, 2, locationListener); } }else { mLocation = locationManager.getLastKnownLocation(provider); locationManager.requestLocationUpdates(provider, 1000, 2, locationListener); } } LocationListener locationListener = new LocationListener() { @Override public void onStatusChanged(String arg0, int arg1, Bundle arg2) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String arg0) { // TODO Auto-generated method stub } @Override public void onProviderDisabled(String arg0) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location arg0) { // TODO Auto-generated method stub // 更新当前经纬度 mLocation = arg0; } };
首先获取当前可用的位置提供者,可以通过网络或者高精度GPS,这里我选择的网络,然后就是判断申请权限有没有拿到。
mLocation一直获取着读到的最新的可获取的Location。
后面的更新参数:第一个是提供者,第二个是更新周期(ms),第三个是更新距离(m),第四个是监听器。
我们在监听到位置变更时获得新的Location。
case R.id.btn_location_lazer: Location location = sDrawerActivity.getLocation(); tvLocation.setText("" + Double.toString(location.getLongitude()) + "-" + Double.toString(location.getLatitude())); break;
通过对应的getLongtitude和getLatitude方法来获取就行了。