Here is the code using a mock location provider. Basically my question
is, why is the mock provider not returning the previously set mock
location correctly. See last comment in code:
private GeoPoint getUserLocation() {
LocationManager locationManager =
(LocationManager)getSystemService
(Context.LOCATION_SERVICE);
if (!PRODUCTION_ENVIRONMENT){
//Get some mock location data in the game
locationManager.addTestProvider(MOCK_PROVIDER_NAME,
true, false,
true, false, false, false, false, Criteria.POWER_LOW,
Criteria.ACCURACY_COARSE);
Location location = new Location(MOCK_PROVIDER_NAME);
location.setLatitude(32.64480);
location.setLongitude(-16.90967);
location.setTime(new Date().getTime());
locationManager.setTestProviderLocation(MOCK_PROVIDER_NAME,
location);
locationManager.setTestProviderEnabled(MOCK_PROVIDER_NAME, true);
}
//i just want an approximate location so no high expectations
here
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
//this correctly retrieves the mock location provider
String provider = locationManager.getBestProvider(criteria,
true);
if (provider != null) {
//this is where the mock provider returns null ... why?
Location location =
locationManager.getLastKnownLocation(provider);
return new
GeoPoint((int)(location.getLatitude()*E6_LATLONG), (int)
(location.getLongitude()*E6_LATLONG));
}else return null;
}
Might this be a bug in the API?
On 27 Sep., 16:43, Frederik Pfisterer <[email protected]> wrote:
> I am working on an application that requires an approximate position
> of the user, so basically i am fine with any relatively recent
> location data i can get. However I can't seem to get things right
> using mock location data and would therefore appreciate your help.
> The method i'm having tries to set mock location data for the gps
> provider and tries to later retrievie it BUT:
> locationManager.setTestProviderEnabled("gps", true); seems to fail
> because i do not get any active location provider from the location
> manager.
> When I tried using a mock location provider with addTestProvider and
> give it my mock location, the LocationManager returns me this mock
> provider with getProviders(true) but then getLastKnownLocation returns
> null instead of the provided mock location.
>
> Here is my code (see comments as well):
>
> private GeoPoint getUserLocation() {
>
> LocationManager locationManager =
> (LocationManager)getSystemService
> (Context.LOCATION_SERVICE);
>
> if (!PRODUCTION_ENVIRONMENT){
> //Get some mock location data in the game
> Location location = new Location("gps");
> location.setLatitude(32.64480);
> location.setLongitude(-16.90967);
> location.setTime(new Date().getTime());
> locationManager.setTestProviderLocation("gps",
> location);
> locationManager.setTestProviderEnabled("gps", true);
>
> //retrieve the list of active providers for debugging
> purposes, the
> activated mock gps provider is not returned here
> List<String> locationProviders =
> locationManager.getProviders
> (true);
> locationManager.setTestProviderEnabled("network",
> true);
>
> }
>
> //i just want an approximate location so no high expectations
> here
> Criteria criteria = new Criteria();
> criteria.setAccuracy(Criteria.ACCURACY_COARSE);
> criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
>
> //this correctly retrieves a mock location provider but not
> the mock
> enabled gps
> String provider =
> locationManager.getBestProvider(criteria,true);
> if (provider != null) {
> //this is where the mock provider returns null
> Location location =
> locationManager.getLastKnownLocation(provider);
> return new
> GeoPoint((int)(location.getLatitude()*E6_LATLONG), (int)
> (location.getLongitude()*E6_LATLONG));
> }else return null;
> }
>
> }
>
> Thanks for any hints or requests for clarification!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---