简体   繁体   中英

Location not always appearing

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.

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);

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