【问题标题】:How to get latitude and longitude from android devide?如何从android设备获取纬度和经度?
【发布时间】:2018-11-18 14:12:48
【问题描述】:

我有一个应用程序已经请求运行时权限来激活本地化权限,并且我已经准备好接收经度和纬度。我现在的问题只是从设备本身获取纬度和经度。

这是我的方法,假设该方法转到 else 语句,因为我已经有权限处理工作

 if (!checkPermissions()) {
        requestPermissions()
    } else {

        //get latitude and long here
    }

【问题讨论】:

    标签: android google-maps kotlin


    【解决方案1】:

    这是一个示例代码

    1.LocationManager & 监听器

    private LocationManager locationManager;
    private LocationListener locationListener;
    

    2.设置管理器和监听器

    locationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
    
    locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            double latitude = location.getLatitude();
            double longitude = location.getLongitude();
        }
    }
    
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        String locationProvider = LocationManager.GPS_PROVIDER;
        currentLocation = locationManager.getLastKnownLocation(locationProvider);
        if (currentLocation != null) {
            double lng = currentLocation.getLongitude();
            double lat = currentLocation.getLatitude();
            Log.d("Log", "longtitude=" + lng + ", latitude=" + lat);
        }
    

    【讨论】:

      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多