0

I have gone through many posts on SO regarding this issue: Tried everything in Here, Here and Here Nothing works. Everytime location is null. On device and on emulator :(. My GPS is on,internet is on and my manifest has following permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

I use following code to get the location i.e the longitude and latitude of the device...however in every case I get location as null

public void find_Location() {
        Log.d("Find Location", "in find_location");

        String location_context = Context.LOCATION_SERVICE;
        locationManager = (LocationManager) getApplicationContext()
                .getSystemService(location_context);

        List<String> providers = locationManager.getProviders(true);
        for (String provider : providers) {
            locationManager.requestLocationUpdates(provider, 1000, 0,
                    new LocationListener() {
                        public void onLocationChanged(Location location) {}

                        public void onProviderDisabled(String provider) {
                        }

                        public void onProviderEnabled(String provider) {
                        }

                        public void onStatusChanged(String provider,
                                int status, Bundle extras) {
                        }
                    });
            Location location = locationManager.getLastKnownLocation(provider);

            if (location == null) {
                Toast.makeText(LbsGeocodingActivity.this, "Location Not found",
                        Toast.LENGTH_LONG).show();
            } else {
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();
                Toast.makeText(LbsGeocodingActivity.this,
                        "LAT..." + latitude + "\nLONG..." + longitude,
                        Toast.LENGTH_LONG).show();
                // addr=ConvertPointToLocation(latitude,longitude);
                // String temp_c=SendToUrl(addr);

            }
        }
    }
Community
  • 1
  • 1
Abhilasha
  • 929
  • 1
  • 17
  • 37

2 Answers2

1

It takes some time before the position is determined, and it can change over time. It's probably just not available yet when you check it.

When a position fix becomes available, you will be notified in onLocationChanged.

1

How could I be so stupid..My bad....In settings Use Wireless networks was not selected!....That cause all the problems !! Now I am able to get a location.

It took me 16 hrs to get to this...my productivity going down for sure! :(

Abhilasha
  • 929
  • 1
  • 17
  • 37
  • Still this thing doesn't work on my emulator though. Works on device. – Abhilasha Jun 29 '12 at 09:36
  • in emulator you need to send the lat and longitude manually by DDMS device control – Vipin Sahu Jul 23 '12 at 11:51
  • @VipinSahu: Thanks Vipin.I had already done that. As I mentioned the **Use Wireless networks** was unchecked in my device and on Emulator.Once I checked it, everything started working. – Abhilasha Jul 23 '12 at 12:43
  • Still this thing doesn't work on my emulator though. Works on device.... what does it mean – Vipin Sahu Jul 24 '12 at 09:57
  • Works on device meaning and does not work on Emulator means AFAIK, `Location info` is displayed only on device. It does not work on Emulator. – Abhilasha Jul 25 '12 at 12:44
  • i added already that DDMS perspective is used to manually send the location update ..... as your emulator has no gps and network .. it rather uses your pc . so pleas pass the location vie ddms then you will get location on emulator too...... and also emulator location never changes as it stay connected with your pc ........... – Vipin Sahu Jul 25 '12 at 13:33