0

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?

Hesham Saeed
  • 5,358
  • 7
  • 37
  • 57
idish
  • 3,190
  • 12
  • 53
  • 85

2 Answers2

1

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?
Nick
  • 8,181
  • 4
  • 38
  • 63
  • I'm running this code on an emulator, not on a device, I'm using a laptop and the Wifi is on. – idish Aug 23 '12 at 22:29
  • It's not going to work on an emulator. There are ways to spoof gps fixes but I don't think I've seen anything that will do NETWORK_PROVIDER unfortunately. Maybe somebody else knows of a simulator for that though. – Nick Aug 23 '12 at 22:30
  • One thing you can do though is switch to listening for GPS and simulate that. It should at least get you far enough to testing other areas of your code like "UpdateLocation". See here: http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator – Nick Aug 23 '12 at 22:32
  • I see, thank you for your help. In a quick look, there's nothing wrong with this code? – idish Aug 23 '12 at 22:33
  • Oh wait, it is not working because I'm running it on an emulator and not on a device? – idish Aug 23 '12 at 22:34
  • Nothing jumped out at me as obviously wrong. Here's another link that may help: http://developer.android.com/guide/topics/location/strategies.html#MockData – Nick Aug 23 '12 at 22:34
  • Yep thats correct - this code will not work on an emulator unless you feed the emulator simulated coordinates. The last link I posted should give you some insight on how you can simulate them. – Nick Aug 23 '12 at 22:35
  • Eh, then I'm just going to run it directly on my device, it takes longer to load my application on a device? – idish Aug 23 '12 at 22:37
  • My personal experience with simulators has been the exact opposite, especially as the SDK level increases. I can't even get a JB simulator to boot. But I digress. Agatha's comment about checking whether the provider is enabled before using it is a good one. What she posted is basically the programmatic way of checking whether use wireless networks is enabled. There's also a way to pop up a dialog from which the user can enable that particular location provider if you find it is disabled. I'm drawing a blank on the details but I'm sure you can find them elsewhere on this site. – Nick Aug 23 '12 at 22:41
  • Alright, thanks alot for your explantories, I'm going to move to device. – idish Aug 23 '12 at 22:58
0

You should check if your location provider is enabled before requesting the location:

locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Give this tutorial a look. It includes sample code

onkar
  • 4,427
  • 10
  • 52
  • 89
agatha
  • 167
  • 1
  • 1
  • 8
  • Please note that you should post the useful points of an answer here, on this site, or your post risks being deleted as ["Not an Answer"](http://meta.stackexchange.com/q/8259). You may still include the link if you wish, but only as a 'reference'. The answer should stand on its own without needing the link. – Andrew Barber Aug 23 '12 at 22:31