android - 即使打开 GPS,位置也为空

标签 android gps location google-maps-android-api-2

我正在研究谷歌地图。我想获取用户位置并在 map 上显示他。我写了一个 Locationlistener 来获取用户位置。

 public Location getUserLocation(){

    try {
        // getting GPS status
        isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        // getting network status
        isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled

            return null;
        } else {
            this.canGetLocation = true;
            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this);
                Log.d("Network", "Network Enabled");
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,DISTANCE_CALC,TIME_UPDATE, this);
                    Log.d("GPS", "GPS Enabled");
                    if (locationManager != null) {
                        location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return location;
}

getUserLocation()LocationListener 中的函数之一,它将通过 GPS 或网络返回用户位置。我正在从这样的 fragment 中调用此函数。

if (Build.VERSION.SDK_INT >= 23){
            if (checkPermission()) {
                location = locHandler.getUserLocation();
                if(location!=null)
                    showTheMaps(location);
                else
                    AlertBuilder(title,body);
            }else {
                requestPermission();
            }
        }else {
            //The build is less than marshmellow so no need for permission
            location = locHandler.getUserLocation();
            try {
//                double d = location.getLatitude();
                showTheMaps(location);
            }catch (NullPointerException e){
                AlertBuilder(title, body);
            }
       }


public void showTheMaps(Location location){

CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),location.getLongitude()), DataModel.zoomlevel);
googleMap.animateCamera(cameraUpdate);
}

AlertBuilder 函数将显示一个带有按钮的警告框,单击该按钮可关闭应用程序。

正如您从屏幕截图中看到的那样,GPS 已启用,但返回给我的位置为空,应用程序在显示警报对话框后关闭。 enter image description here 可能是什么问题呢?为什么打开 GPS 后位置返回 null?

最佳答案

使用

Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);

来源:https://developer.android.com/training/location/retrieve-current.html

另一种(已弃用)方法是在 map 上设置一个 OnMyLocationChangeListener 以获取位置

mMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        // do something with location
     }
});

更多信息:how to get current location in google map android

关于android - 即使打开 GPS,位置也为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609449/

相关文章:

android - 摩托罗拉 Droid 上的相机预览

android - 位置管理器 : is the "network" provider always enabled?

JavaFX 如何设置 Pane 的位置/坐标?

android - 使用 Android 搜索对话框搜索两个或多个数据库列

java - 进度对话框使异步任务中的 Activity 崩溃

通过 Eclipse 安装时 Android 应用程序崩溃

wcf - 使用 Wcf 服务的 Tcp 服务器

以 jpeg 格式存储 GPS 的 Android 被损坏

Android LocationManager requestLocationUpdates 改变了吗?

c# - 出现上下文菜单时如何获取鼠标位置?