2

I've been searching around on how to implement a way to get a user's location in Android and I came across this SO question: What is the simplest and most robust way to get the user's current location on Android?

My questions is, what is LocationResult? How would I use it? I am building a similar application in that question where I search for nearby places based on the current location. I am thinking about having a separate class like that for polling location changes so I don't have to repeat the code in different activities. I suspect that LocationResult is for this purpose? Can you show me and example of how to use it in 2 activities in an OnCreate() method?

Community
  • 1
  • 1
Johnathan Au
  • 5,244
  • 18
  • 70
  • 128
  • That question and its answers are nearly three years old at this point. In Android years, that's a minor eternity. You might consider reading the documentation for more up-to-date guidance: http://developer.android.com/guide/topics/location/strategies.html – CommonsWare Mar 05 '13 at 01:35
  • Did you find something? – Castiblanco Sep 10 '13 at 18:21

2 Answers2

0

Johnathan,

the LocationResult is a simple way of retrieving data to handle. It's a abstract class where you can implement subclasses in order to use the data. See the abstract class concept:

Community
  • 1
  • 1
Pablo Darde
  • 5,844
  • 10
  • 37
  • 55
0

That's the code You need:

    protected LocationCallback mLocationCallback = new LocationCallback()
{
    @Override
    public void onLocationAvailability(LocationAvailability locationAvailability) {
        super.onLocationAvailability(locationAvailability);
        if(!locationAvailability.isLocationAvailable())
        {// No location available
            checkLocationSettings();
            Snackbar snackLocationUnavailable = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_unavailable, Snackbar.LENGTH_LONG);
            snackLocationUnavailable.show();
        }
        else
        {
            Snackbar snackLocationAvailable = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_available, Snackbar.LENGTH_LONG);
            snackLocationAvailable.show();
        }
}

    @Override
    public void onLocationResult(LocationResult locationResult) {
        super.onLocationResult(locationResult); 
        mLastLocation = locationResult.getLastLocation();
        mMyLatLng = new LatLng(valueOf(mLastLocation.getLatitude()), valueOf(mLastLocation.getLongitude()));
        meMarker.setPosition(mMyLatLng);
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMyLatLng, 15));
        Snackbar snak2 = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_updated, Snackbar.LENGTH_INDEFINITE);
        snak2.show();
    }
};

...

onCreate(){
// Build GoogleApiClient
    if (mGoogleApiClient == null) {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

//`Build LocationRequest
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(2000)        // 2 seconds, in milliseconds
                .setFastestInterval(1000); // 1 second, in milliseconds
`
//     

Build LocationSettingsRequest
        LocationSettingsRequest.Builder lRbuilder = new LocationSettingsRequest.Builder();
        lRbuilder.addLocationRequest(mLocationRequest);
        mLocationSettingsRequest = lRbuilder.build();
        lRbuilder.setAlwaysShow(true);

// ...

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mLocationCallback, null);

//...

 LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationCallback);
Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
Artur Ex
  • 43
  • 7