Search code examples
javaandroidgpslocationmanagerandroid-location

Get user location Android


I'm trying to get the user location based on the Network Provider, but the event never occures and the UpdateLocation() method never gets called. Here's the code:

public void UpdateLocation(Location location)
{
    GeoPoint geoPoint = new GeoPoint((int)(location.getLatitude() * 1E6),(int)(location.getLongitude() * 1E6));
    MapController controller = mapView.getController();
    controller.setCenter(geoPoint);
}
public void GetLocation()
{
     // Acquire a reference to the system Location Manager
    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates
    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          UpdateLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
      };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
}

What could be the problem? Am I doing something wrong here?


Solution

  • If I recall correctly, NETWORK_PROVIDER uses wifi signals to figure out location.

    Couple things to verify:

    • Is wifi on?
    • Is the device registering a wifi signal? More than one? (one may not be enough)
    • Is "Use Wireless Networks" checked under "Location Services" in Settings?