Currently, I use GPS only to get the user's current location in order to return certain results. As in Belgium, if you are inside, most of the time you cannot get a GPS connection. Therefore, I want to get the current location with a Wi-Fi connection.
I found this example: get current location (gps / wifi)
But
I'm not sure which lines tell the device which connection to choose. I assume this is one thing: String provider = myLocationManager.getBestProvider(criteria,true);What should I add to my code.
When I check that inside this, I always get a null value, I don’t quite understand why.
My code looks like this:
public void getOwnLngLat(){
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
final LocationListener locationListener = new LocationListener(){
public void onLocationChanged(Location location) {
longitude="" + location.getLongitude();
latitude="" + location.getLatitude();
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
};
locationManager.requestLocationUpdates(locationManager.getBestProvider(new Criteria(), true), 2000, 10, locationListener);
}
source
share