3

in my app I am able to use the GPS but the problem is that it always getting me the same location. Everytime I am retrieving the location from that on, it gives me the same value. First time it was fetched and I was online, wirelessly. Then i went out with only the GPS and even if I was a mile away location was the same. I enabled 3g and still the same.

I send my app to a friend of mine, he got a different first time location and then he sees the same no matter where he goes.

Here is my code:

public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
//                  System.out.println("Network enabled");
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
//                  System.out.println("GPS enabled");
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

 @Override
    public void onLocationChanged(Location location) {
//      mAct.playToastMessage("Location Changed");
        getLocation();
//      mAct.setGPSText(location.getLatitude(),location.getLongitude());
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }
ghostrider
  • 5,131
  • 14
  • 72
  • 120
  • please check [**this**](http://rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html) I am getting every time new gps data – swiftBoy Jan 19 '13 at 14:34

2 Answers2

3
 locationManager.requestLocationUpdates(
                          LocationManager.NETWORK_PROVIDER,
                       MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

In this For the parameter of MIN_TIME_BW_UPDATES and MIN_DISTANCE_CHANGE_FOR_UPDATES you have to set the 0 and 0. If it is set to 0,0. It will give you the location update as quick as possible. If you set larger value for the you never get the updated value until your condition satisfied. I hope this could solves your problem.

M Vignesh
  • 1,586
  • 2
  • 18
  • 55
1
@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = “My current location is: “ +

“Latitude = “ + loc.getLatitude() +

“Longitude = “ + loc.getLongitude();


Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();

}

Source : Tutorial on GPS to get Current Location.

Swayam
  • 16,294
  • 14
  • 64
  • 102