views:

533

answers:

1

I read some tutorials online, and found that LocationManager does this. This is the code I found online. However, I don't get it. Why can't I just get the long/lat? Why go through all the "change" location things...

lm=(LocationManager)this.getSystemService(LOCATION_SERVICE);  
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10, 10, new LocationListener(){

        @Override
        public void onLocationChanged(Location location) {                          
             Log.d("a","onLocationChanged: lat="+location.getLatitude());
             Log.d("a","onLocationChanged: lat="+location.getLongitude());                 
        }

        @Override
        public void onProviderDisabled(String provider) {
             Log.d("a","onProviderDisabled: " +provider);  
        }

        @Override
        public void onProviderEnabled(String provider) {
             Log.d("a","onProviderEnabled: " +provider);
        }

        @Override
        public void onStatusChanged(String provider, int status,Bundle extras) {
             Log.d("a","onStatusChanged: " +provider + status);   
        }

    }); 

By the way, this code doesn't work. When I run it in a timer...nothing happens. Nothing gets logged. I just want the longitude latitude!!

+1  A: 

LocationManager can return a last known location but this is not very precise so you have to request location updates and wait for the first one. Then you can continue receiving them or cancel them. You can request both network and GPS updates.

If you are using a phone to test your code then you have to wait for a GPS fix, and this can take a while. Network provider should be much faster.

If you are using Eclipse, then you have to send artificial GPS updates using DDMS. You can't test a network provider from the emulator (you will never receive an update).

Nikola Smiljanić