Little Fluffy Location - Misunderstanding how to get current location

1.1k views Asked by At

I am attempting to use the Little Fluffy Location Library in order to get location information for my little app. I have it running in an async process like so:

    private class ShowLocationTask extends AsyncTask<Void, Void, Boolean> {
        @Override
        protected Boolean doInBackground(Void... params) {
            //LocationLibrary.forceLocationUpdate(getApplicationContext());
            latestInfo = new LocationInfo(getBaseContext());
            latestInfo.refresh(getBaseContext());

            lat = latestInfo.lastLat;
            lng = latestInfo.lastLong;

            return true;
        }

        protected void onPostExecute(Boolean result) {
            if (result != true) {
                Toast.makeText(ACTIVITY.this,
                        "Cannot get your location...", Toast.LENGTH_LONG)
                        .show();
            } else {

                Date date = new Date(latestInfo.lastLocationUpdateTimestamp);

                Toast.makeText(ACTIVITY.this,
                        "Last Updated: " + date, Toast.LENGTH_LONG).show();
                LatLng meLoc = new LatLng(lat, lng);
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(meLoc) // Sets the center of the map to
                        .zoom(ZP).bearing(0).tilt(80).build();
                map.animateCamera(CameraUpdateFactory
                        .newCameraPosition(cameraPosition));
                Toast.makeText(ACTIVITY.this,
                        "..." + lat + " : " + lng, Toast.LENGTH_LONG).show();
                Log.e("LATLON", lat + " : " + lng);
            }
        }

    }

I call that in my onCreate().

userLoc.execute();

I extend my application with this class:

public class FluffyLocation extends Application {

@Override public void onCreate() { super.onCreate();

  LocationLibrary.showDebugOutput(true);

  try {           LocationLibrary.initialiseLibrary(getBaseContext(), 0,
              2 * 60 * 1000, "com.jasonflaherty.snoteldata");         } catch (UnsupportedOperationException ex) {            Log.d("Application",
              "UnsupportedOperationException thrown - the device doesn't have any location providers");       }   }

}

Setup in the Manifest:

    <receiver
        android:name="com.littlefluffytoys.littlefluffylocationlibrary.StartupBroadcastReceiver"
        android:exported="true" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.littlefluffytoys.littlefluffylocationlibrary.PassiveLocationChangedReceiver"
        android:exported="true" />

I know it is running, since I get the TOAST to show info each time the activity runs... I am trying to get the current location each time the user opens the app. I guess I am missing how this works. I was thinking the latestInfo.refresh() would give me the latest info, however, if I move to another location 200 meters away, I still get the same lat/lng and also the same .lastLocationUpdateTimestamp.

I would like to get the current user location and then periodically get an update. This library seems perfect for that reason... but I am not getting the results. Am I not implementing this correctly?

EDIT::::

I didn't implement the broadcast receiver, however, I am not sure how to get it to work for more than one class. I have 3 different ones that would ask for the users location...

1

There are 1 answers

0
user3470700 On

the refresh() method just update the current latestInfo objet. you need ask for update.

LocationLibrary.forceLocationUpdate(MainActivity.this);

and you need let libary a secconds for get the current location. then you can call

latestInfo.LastLat

and show.

implementing the a Broadcast for the library let you access to the method OnLocationChanged, who is called when the LocationListenner of the library get a new location.

    public class FluffyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
    Log.d("FluffyBroadcastReceiver", "onReceive(): received location update");

    final LocationInfo locationInfo = (LocationInfo) intent.getSerializableExtra(LocationLibraryConstants.LOCATION_BROADCAST_EXTRA_LOCATIONINFO);
  //do something whit the new location        
    doSomething(String.valueOf(locationInfo.lastLong),String.valueOf(locationInfo.lastLat),String.valueOf(locationInfo.lastAccuracy));
 }
}

and in the manifest file you have to define this receiver

<receiver android:name="com.YOURPACKAGE.FluffyBroadcastReceiver" android:exported="true" />