zoukankan      html  css  js  c++  java
  • Android笔记之使用LocationManager获取经纬度

    LocationManager.getLastKnownLocation(String provider)有可能返回null,概率还挺高

            findViewById(R.id.llMain).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AndPermission.with(MainActivity.this).runtime().permission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                            .onGranted(new Action<List<String>>() {
                                @Override
                                public void onAction(List<String> data) {
                                    boolean fineLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
                                    boolean coarseLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
                                    if (fineLocationPermissionGranted && coarseLocationPermissionGranted) {
                                        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                                        Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                                        if (location == null) {
                                            new AlertDialog.Builder(MainActivity.this).setMessage("无法获取位置").show();
                                        } else {
                                            double longitude = location.getLongitude();
                                            double latitude = location.getLatitude();
                                            Log.d(TAG, longitude + ", " + latitude);
                                            new AlertDialog.Builder(MainActivity.this).setMessage("经度:" + longitude + "
    纬度:" + latitude).show();
                                        }
                                    }
                                }
                            })
                            .onDenied(new Action<List<String>>() {
                                @Override
                                public void onAction(List<String> data) {
                                    new AlertDialog.Builder(MainActivity.this).setMessage("位置权限被拒绝").show();
                                }
                            }).start();
                }
            });

    以下是通过requestSingleUpdate获取经纬度,不过,LocationManager.requestSingleUpdate是异步的,获取到Location的时间不好控制,如果对经纬度的要求不是必须的,还是建议使用第一种

            findViewById(R.id.btnGetLongitudeAndLatitude).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AndPermission.with(MainActivity.this).runtime().permission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)
                            .onGranted(new Action<List<String>>() {
                                @Override
                                public void onAction(List<String> data) {
                                    boolean fineLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED;
                                    boolean coarseLocationPermissionGranted = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED;
                                    if (fineLocationPermissionGranted && coarseLocationPermissionGranted) {
                                        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
                                        locationManager.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, new LocationListener() {
                                            @Override
                                            public void onLocationChanged(Location location) {
                                                double longitude = location.getLongitude();
                                                double latitude = location.getLatitude();
                                                Log.d(TAG, longitude + ", " + latitude);
                                                new AlertDialog.Builder(MainActivity.this).setMessage("经度:" + longitude + "
    纬度:" + latitude).show();
                                            }
    
                                            @Override
                                            public void onStatusChanged(String provider, int status, Bundle extras) {
    
                                            }
    
                                            @Override
                                            public void onProviderEnabled(String provider) {
    
                                            }
    
                                            @Override
                                            public void onProviderDisabled(String provider) {
    
                                            }
                                        }, null);
                                    } else {
                                        new AlertDialog.Builder(MainActivity.this).setMessage("位置权限被拒绝").show();
                                    }
                                }
                            })
                            .onDenied(new Action<List<String>>() {
                                @Override
                                public void onAction(List<String> data) {
                                    new AlertDialog.Builder(MainActivity.this).setMessage("位置权限被拒绝").show();
                                }
                            }).start();
                }
            });
  • 相关阅读:
    ASP.NET Core 企业级开发架构简介及框架汇总
    SQL Server中的联合主键、聚集索引、非聚集索引
    C#进阶系列——WebApi 异常处理解决方案
    C#进阶系列——WebApi 身份认证解决方案:Basic基础认证
    C#进阶系列——WebApi 接口参数不再困惑:传参详解
    实战 Windows Server 2012 群集共享卷
    SQL Server 表和索引存储结构
    SQL Server AlwaysOn架构及原理
    共轭分布
    反向传播BP为什么高效
  • 原文地址:https://www.cnblogs.com/buyishi/p/10557677.html
Copyright © 2011-2022 走看看