-2

I need a method to simply get latitude and longitude. I'm developing my first app... can you tell me if i'm doing it in the right way?

Here is my method:

private void getCoordinates() {
    getCurrentLocation();

    boolean gpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

    if (!gpsEnabled && !netEnabled && location != null) {
        Builder alarm = new AlertDialog.Builder(this);
        alarm.setTitle("Error");
        alarm.setMessage("Localization OFF").create().show();
    } else if (location != null && (gpsEnabled || netEnabled)) {
        lManager.removeUpdates(lListener);
        lat = String.valueOf(location.getLatitude());
        lon = String.valueOf(location.getLongitude());
    } else {
        Builder alarm = new AlertDialog.Builder(this);
        alarm.setTitle("Error");
        alarm.setMessage("Localization OFF").create().show();
    }
}

private void getCurrentLocation() {     
    lManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);   
    lListener = new LocationListener() {
        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
        }

        @Override
        public void onProviderEnabled(String provider) {
        }

        @Override
        public void onProviderDisabled(String provider) {
        }

        @Override
        public void onLocationChanged(Location loc) {
            location = loc;
        }
    };

    LocationProvider gpsProvider = lManager.getProvider(LocationManager.GPS_PROVIDER);
    if (gpsProvider != null) {
        boolean gpsEnabled = lManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean netEnabled = lManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!gpsEnabled && netEnabled) {
            lManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, lListener);
            location = lManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        } else if (gpsEnabled) {
            lManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lListener);
            location = lManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        }   
    } else {
        Builder alarm = new AlertDialog.Builder(this);
        alarm.setTitle("Error");
        alarm.setMessage("GPS provider not supported").create().show();
    }
}

Please fix it if you can see anything wrong or give me any suggestions.

smartmouse
  • 13,912
  • 34
  • 100
  • 166
  • possible duplicate of [How do I get the current GPS location programmatically in Android?](http://stackoverflow.com/questions/1513485/how-do-i-get-the-current-gps-location-programmatically-in-android) – Simas Oct 12 '14 at 14:06
  • Nothing useful there, some answer talk about GPS only and other answer are not complete – smartmouse Oct 12 '14 at 14:34
  • Is there anything specific that doesn't work in your code? – Simas Oct 12 '14 at 14:36
  • I don't know... i made it by myself, it works... but i'm not sure it is coded good. Could i do it better? – smartmouse Oct 12 '14 at 15:10
  • Here is something that may help you - http://developer.android.com/guide/topics/location/strategies.html#BestEstimate – Confuse Oct 12 '14 at 15:25

1 Answers1

0

The problem is when you setup a listener for the location it does not receive a location instantly- especially with a GPS location. So your location is not returning the current coordinates. You can call the getLastKnownLocation in your location manager but this could be an old or non-existant location. The other option is to call something from

@Override
  public void onLocationChanged(Location loc) {
       someCallBack(loc);
   }

Have a look at my NoteLocation example on github Link

Braedon
  • 3
  • 2