-4

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.

Gopal Singh Sirvi
  • 4,539
  • 5
  • 33
  • 55
user2653926
  • 610
  • 7
  • 23

3 Answers3

0

You can get Location from

onLocationChanged(Location location)
Ciro Rizzo
  • 492
  • 4
  • 8
  • So, your device has GPS, Data Network switched on, permission in Manifest set up fine, provider in Location Manager using GPS and Network and you still do not receive any data onLocationChanged? – Ciro Rizzo Jul 02 '15 at 09:19
  • I think something else is missing in your case... If you send much more code probably we can find a solution to that issue – Ciro Rizzo Jul 02 '15 at 09:21
0

Have you got Google Maps API key and setup everything in google api console?

Ke Di
  • 345
  • 1
  • 12
0
 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.

Neeraj Verma
  • 703
  • 6
  • 15
  • you have made your code too much complex here. post your complete code with libraries and all implementations.Moreover,if you can upload your manifest that'll be much better – Neeraj Verma Jul 02 '15 at 09:21
  • try this [link](http://stackoverflow.com/questions/31071966/android-access-fine-location-permission-not-being-granted-or-something) – Neeraj Verma Jul 02 '15 at 09:27