Class Veery

requestLocationUpdate(LocationUpdate callback)

Request Veery to be warned when the user has moved.

This could be used to refresh a Maps activity.

Parameters

Param name Type Usage
callback LocationUpdate Callback that will be triggered

Returns

void

Usage / Example

Inline pattern

 veery.requestLocationUpdate(new Veery.LocationUpdate() {
    @Override
    public void onLocationUpdate(Location loc, long age) {
        if (loc!=null) {
             //
             // do whatever you need with the Location object
             //
        } else {
             //
             // Location service has become unavailable (deactivated by the user)
             //
        }
    }
 });

Interface on activity pattern

The following example implement a Google Maps activity and update it when the user moves

 import android.location.Location;                  
 import com.roofstreet.android.veery.*;

public class MapsActivity extends AppCompatActivity implements
        OnMapReadyCallback,
        Veery.LocationUpdate {

    private final Veery veery = new Veery(this);
    private GoogleMap mMap = null;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        veery.serviceConnect();
        veery.setApiKeySecret("SuPeRScrET12345789GhJ");
        FirebaseApp.initializeApp(this);
        veery.setFirebaseToken(FirebaseInstanceId.getInstance().getToken());
  //
        veery.activate(Veery.FOREGROUND);
  // Requires Android Veery 1.3.2
  //veery.activate(Veery.FOREGROUND_GEOLOC );

        setContentView(R.layout.activity_maps);

        // Added by Google Maps
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        veery.serviceResume();
        veery.requestLocationUpdate(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        veery.servicePause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            veery.stopLocationUpdate();
            veery.serviceDisconnect();
        } catch (Throwable t) {
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;

        mMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
            @Override
            public void onMapLoaded() {
                mMap.setPadding(200, 100, 0, 200);

                // Force the callback as it could have been triggered before the Map were ready
                onLocationUpdate(veery.getCurrentLocation(), veery.getCurrentLocationAge());
            }
        });
    }

    @Override
    public void onLocationUpdate(Location loc, long age) {
        if (mMap != null){
            mMap.clear();
            if (loc != null) {

                // We will draw a circle where where the user is

                LatLng here = new LatLng(loc.getLatitude(), loc.getLongitude());

                CircleOptions circleOptions = new CircleOptions();

                circleOptions.center(here);

                // Radius is the accuracy of the Location

                circleOptions.radius(loc.getAccuracy());

                // We want the circle to be red or green depending on the source ou the Location
                int color;
                int scolor;
                if (loc.getProvider().startsWith("gps")) {
                    color = 0xF0FF0000; // RED
                    scolor = Color.RED;

                } else {
                    color = 0xF000FF00; // GREEN
                    scolor = Color.GREEN;
                }

                // Define the alpha depending on the accuracy
                int mincolor = 160;
                int maxdist = 1200;
                color ^= (Math.max(-(mincolor * (int) loc.getAccuracy() / maxdist) + mincolor, 0) << 24);

                circleOptions.fillColor(color);
                circleOptions.strokeColor(scolor);
                circleOptions.strokeWidth(1);

                mMap.addCircle(circleOptions);
            }
        }
    }
}

User interaction

none

See also

stopLocationUpdate()

Stop receiving events (callback) requested by requestLocationUpdate.

onLocationUpdate(Location loc, long age)

Callback triggered every time a new geolocation is known.