-1

Hello i need get location Latitude and location longitude only one no need listener location, any example?

per example in MainActivity.java

   Location location = Myexample.location;

Myexample.class extends service

// code need here regards!

scunliffe
  • 62,582
  • 25
  • 126
  • 161
Luis
  • 535
  • 2
  • 14
  • possible duplicate : http://stackoverflow.com/questions/7979230/how-to-read-location-only-once-with-locationmanager-gps-and-network-provider-a – dlimos Dec 19 '14 at 16:10
  • Look here http://stackoverflow.com/a/5314266/2159780 You just need to use LocationManager – grig Dec 19 '14 at 16:10

1 Answers1

1
LocationManager locationManager = context.getSystemService(Context.LOCATION_SERVICE);
String provider = LocationManager.GPS_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(provider);

if (null != lastKnownLocation) {
    Toast.makeText(getActivity().getApplicationContext(),String.valueOf(lastKnownLoc‌​ation.getLatitude() + lastKnownLocation.getLongitude() ),Toast.LENGTH_SHORT).show();
}

But check what does really getLastKnownLocation returns and does it fits in your application context. Otherwise you have to use listener pattern to obtain latest location.

But I strongly do not recommend to use getLastKnownLocation if you want to get latest location. You could try implement something like this:

class MyFragment extends Fragment {

    private void makeUseOfNewLocation(Location location) {
        // some magic code which uses location object
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        LocationManager locationManager = (LocationManager)activity.getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                makeUseOfNewLocation(location);
                locationManager.removeUpdates (this);
            }

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

            public void onProviderEnabled(String provider) {}

            public void onProviderDisabled(String provider) {}

        };

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
}

It ensures that you always get a location object and only once.

Robertas Setkus
  • 3,075
  • 6
  • 31
  • 54
  • java.lang.SecurityException: "gps" location provider requires ACCESS_FINE_LOCATION permission. – Luis Dec 19 '14 at 16:26
  • LocationManager locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE); String provider = LocationManager.GPS_PROVIDER; Location lastKnownLocation = locationManager.getLastKnownLocation(provider); Toast.makeText(getActivity().getApplicationContext(),String.valueOf(lastKnownLocation.getLatitude() + lastKnownLocation.getLongitude() ),Toast.LENGTH_SHORT).show(); – Luis Dec 19 '14 at 16:29
  • java.lang.NullPointerException – Luis Dec 19 '14 at 16:30
  • Have you read method documentation? It is said that _If the provider is currently disabled, null is returned._ and I think there is other circumstances when you could get `null`. So you must to check if it is not `null`. – Robertas Setkus Dec 19 '14 at 16:41
  • Good answer work, but locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); params 0 dont work the listener is in 3 seg :/ i need inmmediate – Luis Dec 19 '14 at 18:07
  • http://stackoverflow.com/questions/10484239/android-requestlocationupdates-minimumtime-minimumdistance-dont-really-work – Luis Dec 19 '14 at 18:10