Get current position with GPS AND WIFI

13,705

Solution 1

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

Solution 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.

Share:
13,705
Hannelore
Author by

Hannelore

Updated on June 04, 2022

Comments

  • Hannelore
    Hannelore almost 2 years

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