Friday, August 24, 2012

Re: [android-developers] Location data not consistently available and sometimes gives null data

If you can do without needing a zipcode and can just work with raw GPS
coordinates you could overide public void onGpsStatusChanged(int
event). You'll get regular updates while your app is running.

On Wed, Aug 22, 2012 at 2:04 PM, Fasih Awan <fasihawan@gmail.com> wrote:
> 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.
>
> Also depending on the location I sometimes get the subAdminArea from the
> geocoder, but other times it gives me null
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment