3

I'm currently using GPS only to get current location of the user to return certain results. As in Belgium, if you're inside, most of the time you can't get GPS-connection. So I want to get the current location with the wifi-connection.

I've found this example: get the current location (gps/wifi) But

I'm not sure which lines tell the device which connection to choose. I'm guessing it's this one: String provider = myLocationManager.getBestProvider(criteria,true); That I've to add to my code.

When I check what's inside of this, I always get a null-value, I don't quite understand why.

My code looks currently like this:

public void getOwnLngLat(){
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
final LocationListener locationListener = new LocationListener(){

public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub
    longitude="" + location.getLongitude();
    latitude="" + location.getLatitude();
}

public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status,
                Bundle extras) {
    // TODO Auto-generated method stub

   }        
};
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), 2000, 10, locationListener);
}
Community
  • 1
  • 1
Hannelore
  • 763
  • 5
  • 11
  • 23

2 Answers2

3

you are using locationmanagers getbestprovider method which simply gets any location provider which most closely matches your criteria. if you want to get a specific location provider then use the getProvider method. the parameter should be one of the results of the getProviders method. some links: http://developer.android.com/guide/topics/location/obtaining-user-location.html http://developer.android.com/reference/android/location/LocationManager.html

Arno den Hond
  • 627
  • 1
  • 7
  • 24
  • So if I understand you well, if my GPS isn't enabled, the app will automatically choose wifi (is this is enabled) to do the trick? – Hannelore May 06 '11 at 08:24
  • yes, you got it! use the getProviders method (parameter true!) to see which location providers are enabled. – Arno den Hond May 06 '11 at 08:32
  • I've tested this, on my Samsung Galaxy Ace, and when I turn my GPS off and my wifi on (I'm connected to an open wifi), my p-variable is null... – Hannelore May 06 '11 at 08:47
  • A String variable which contains `locationManager.getBestProvider(new Criteria(), true); ` – Hannelore May 07 '11 at 15:26
2

If you want to get updates from a specific provider you can use the requestLocationUpdates (String provider, long minTime, float minDistance, LocationListener listener) method of LocationManager. A list of available providers can be obtained using the getProviders(boolean enabledOnly) method.

Matt Colliss
  • 1,394
  • 1
  • 11
  • 21
  • What do you mean with updates? (English is not my mothertongue) I just want to get latitude and longitude of my current location. I've found another example: http://stackoverflow.com/questions/3145089/what-is-the-simplest-and-most-robust-way-to-get-the-users-current-location-in-an/3145655#3145655 Will implementing this help me forward? – Hannelore May 06 '11 at 08:17
  • A location provider (e.g. GPS) will deliver location updates (latitude, longitude, time, accuracy etc) to your LocationListener object. These updates are started when you call the requestLocationUpdates method and will continue periodically until you call the removeUpdates method. – Matt Colliss May 06 '11 at 08:21