简体   繁体   中英

Location object is null

I am new to android programming. I wanted to develop an application that makes use of Location API. I am using Location Manager for the purpose. Code:

        public Location getLocation() {
            try {
                locationManager = (LocationManager) mContext
                        .getSystemService(LOCATION_SERVICE);

                isGPSEnabled = locationManager
                        .isProviderEnabled(LocationManager.GPS_PROVIDER);

                isNetworkEnabled = locationManager
                        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

                if (!isGPSEnabled && !isNetworkEnabled) {
                } else {
                    this.canGetLocation = true;
                    if (isNetworkEnabled) {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("clickindia", "Network");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                            Log.d("message","location"+location);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }

                    if (isGPSEnabled) {
                        if (location == null) {
                            locationManager.requestLocationUpdates(
                                    LocationManager.GPS_PROVIDER,
                                    MIN_TIME_BW_UPDATES,
                                    MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                            Log.d("message", "GPS Enabled");
                            if (locationManager != null) {
                                location = locationManager
                                        .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                                if (location != null) {
                                    latitude = location.getLatitude();
                                    longitude = location.getLongitude();
                                }
                            }
                        }
                    }
                }

All the necessary permissions have been mentioned in the manifest file. I am running the code on the phone and it seems getLastKnownLocation() is not doing anything. Please help I am stuck

I cannot use new FusedLocationApi because my phone has google play services versions lower than what required to support the googleApiClient and LocationServices.

您可以从

onLocationChanged(Location location)

您是否已 获得 Google Maps API 密钥并在google api 控制台中设置了所有内容?

 best = LocationManager.NETWORK_PROVIDER;
 loc = (LocationManager) getSystemService(LOCATION_SERVICE);
 googleMap.setMyLocationEnabled(true);
 loc.requestLocationUpdates(best,1000,0,this);
 location=loc.getLastKnownLocation(best);

Try this. Your code should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM