即使打开GPS,位置也为空

时间:2017-04-25 11:29:08

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

我正在研究谷歌地图。我想获取用户位置并在地图上显示他。我写了一个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或网络返回用户位置的功能之一。我从这样的片段调用这个函数。

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?

1 个答案:

答案 0 :(得分:1)

使用

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

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

另一种(已弃用)方法是将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