0

I am trying to get the location of a user through LocationManager.NETWORK_PROVIDER but it always returns a blank string. Only sometimes I get the valid data. Is there any way to make this method re-iterate until it gives me a location? I would think that location would come quickly and there would be no need for this method. I have tried re-calling it until it doesn't display a blank string, but nothing comes up.

Here is the method I call

   public String getLocation(Locale locale, Context context, boolean city, boolean postal, boolean state_prov) throws IOException{
       LocationManager locMan = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
       LocationListener locList = new MyLocList();
       Geocoder gC = new Geocoder(context,locale);
       Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);
       locMan.requestLocationUpdates(locMan.NETWORK_PROVIDER, 500, 200, locList);
       Location networkLocation = locMan.getLastKnownLocation(locMan.NETWORK_PROVIDER);
       if (city)
           return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getSubAdminArea());
       else if (postal)
           return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getPostalCode());
       else if (state_prov)
           return (gC.getFromLocation(networkLocation.getLatitude(), networkLocation.getLongitude(), 1).get(0).getAdminArea());
       else
           return "Nothing";
    }

I usually ask for the city or postal code, but it most of the time it comes up empty. Using the

    Location gpsLocation = locMan.getLastKnownLocation(locMan.GPS_PROVIDER);

results in a NullPointer, so I am using the NETWORK_PROVIDER

I have set these permissions:

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>

And this is what I call it with

    String data = getLocation(Locale.getDefault(),getBaseContext(),true,false,false);

I keep on getting a blank string as the result.

I asked a question before about not receiving the city, but now it seems that no data is coming, and if it is, its not consistently available.

Fasih Awan
  • 1,891
  • 4
  • 17
  • 24

1 Answers1

1

In your code, you are asking for "last known location", not for "current location". Some other application asked for location before and you are getting the same result as that application. If there was no request for location prior to launching your application, "last known location" will be null.

Correct way is to register listeners and wait for location update to arrive. The result is then stored as "last known location".

This answer should help you: What is the simplest and most robust way to get the user's current location in Android?

EDIT: To clarify, here is proper usage of MyLocation class mentioned in the link above. You can't have the location immediately, instead place your code in the callback method and it will get called when the location data become available.

LocationResult locationResult = new LocationResult(){
    @Override
    public void gotLocation(Location location){
        //Got the location!
        //Here goes your code
        //This method will be called when location data arrive
    }
};
MyLocation myLocation = new MyLocation();
myLocation.getLocation(this, locationResult);
Community
  • 1
  • 1
foxter
  • 355
  • 1
  • 5
  • Okay so using a bit of that code I have it in my application. However, even after calling the `myLocation.getLocation(this,locationResult);` it still sometimes stays blank when I get the string via `Location location = myLocation.locMan.getLastKnownLocation(myLocation.getWorkingProvider());` – Fasih Awan Aug 23 '12 at 20:58
  • 1
    @FasihAwan You don't need to call `getLastKnownLocation()`. Just use location data you receive in the callback. Write your code where the `//Got the location!` comment is. Location data will be stored in `location` variable. – foxter Aug 23 '12 at 21:25
  • Ah yeah okay god it. I was hoping I could make some getters and use those outside that gotLocation method. But it works and I can continue on with my development. Many thanks foxter – Fasih Awan Aug 24 '12 at 20:14