Find nearest POIs

Find nearest POIs using Android SDK

  1. Use Search API to find the Nearest POIs
  2. Use Search API to monitored region around POIs
  3. Use a Distance provider to retrieve distance and duration value

Use Search API to find the Nearest POIs

After receiving the user location, the SDK automatically triggers a request to the Search API to get the nearest POIs (to disable this automatic trigger see the Overload tracking properties documentation) .

You need a Private API key to request the Woosmap Search API.

To obtain on demand the closest POI from a location, you must instantiate the class PositionsManager call the method searchAPI and get the result on the callback SearchAPIReadyCallback or get the POI in the database inside the SDK.

In your activity, set keys and set a listener to monitor result of the SearchAPI request :

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Set Keys
    WoosmapSettings.privateKeyWoosmapAPI = woosmapPrivateKey;
    
    // Set the Search API listener 
    this.woosmap.setSearchAPIReadyListener(new WoosSearchAPIReadyListener());
}

Woosmap Search API Ready Listener

Create a listener connected to the interface Woosmap.SearchAPIReadyListener and set a callback to retrieve POI data.

public class WoosSearchAPIReadyListener implements Woosmap.SearchAPIReadyListener {
    public void SearchAPIReadyCallback(POI poi) {
        // result of Search API request
    }
}

Retrieve POI data

All result of request SearchAPI are strored in the SDK database, you can retrieve data from the SDK database like this :

POI[] poiList = WoosmapDb.getInstance(getApplicationContext()).getPOIsDAO().getAllPOIs();

Important : all retrieve from the database must be launch in a asynchronous task.

Request Parameters

Set parameter to narrow your results or filters it with the query parameter. More details in the Woosmap Search API documentation:

WoosmapSettings.searchAPIParameters.put("radius","5000");
WoosmapSettings.searchAPIParameters.put("stores_by_page","2");
WoosmapSettings.searchAPIParameters.put("query","type:\"click&collect\"");

Note that you can create a maximum of 5 POI from each SearchAPI’s response. So the parameter “stores_by_page” can’t exceed 5.

Force request to the Search API

If you need to trigger search requests (to retrieve POIs) by your own, you can perform on demand requests to the Search API.

PositionsManager mPositionsManager = new PositionsManager(getContext(), WoosmapDb.getInstance(getContext()));

// set latitude and longitude of the POI, and the id of the location if you want to update a place in database of the SDK (default to 0).
mPositionsManager.searchAPI(place.getLatitude(), place.getLongitude(), place.getLocationId());

Use Search API to monitored region around POIs

Regions creation is enabled on the nearest result of the Search API request. More details in the Monitor POIs documentation

Radius of POI

When you create a Geofence around a POI, manually define the radius value:

WoosmapSettings.poiRadius = 500;

or choose the user_properties subfield that corresponds to radius value of the Geofence:

WoosmapSettings.poiRadiusNameFromResponse = "radiusPOI";

Use a Distance provider to retrieve distance and duration value

As for the Search API, request to the Distance provider can be done on demand (e.g. if not enabled with each location collection or if needed on dedicated process of your own).

In your activity, instantiate Woosmap, define a Distance provider, set keys, specifies the mode of transport to use when calculating distance and set a listener to monitor result of the Distance request:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Set Keys
    WoosmapSettings.privateKeyWoosmapAPI = woosmapPrivateKey;
    
    // Set the Distance API listener 
    this.woosmap.setDistanceReadyListener(new WoosDistanceReadyListener());
    
    // [...]
}

Distance Delegate

Create a listener connected to the interface Woosmap.DistanceReadyListener and set a callback to retrieve distance and duration values.

public class WoosDistanceReadyListener implements Woosmap.DistanceReadyListener {
    public void DistanceReadyCallback(Distance[] distances) {
        // result of Distance request
    }
}

Send request to retrieve distance and duration data

As for the Search API, if you need to trigger distance requests (to calculate distance and duration) by your own, you can perform on demand requests to the Distance provider. Parameters are optional, if not defined, tracking profile values are used as default value.

PositionsManager mPositionsManager = new PositionsManager(getContext(), WoosmapDb.getInstance(getContext()));

// set latitude and longitude of the origin
Double latOrigin = currentPosition.getLatitude();
Double lngOrigin = currentPosition.getLongitude();

// set the latitude and longitude of destinations,
List<Pair<Double, Double>> listDestinationPoint = new ArrayList<>();
listDestinationPoint.add(new Pair(place.getLatitude(), place.getLongitude()));

Map<String, String> param = new HashMap<String, String>();

// [OPTIONAL] switch from a distance provider to another  
param.put( "distanceProvider","woosmapDistance" );

// [OPTIONAL] change the travel mode ("walking", "cycling" or "driving")
param.put( "distanceMode","walking" );

// [OPTIONAL] change the distance units ("imperial" or "metric")
param.put( "distanceUnits","imperial" );

// [OPTIONAL] change the language for textual result (example: "en", "fr")
param.put( "distanceLanguage","en" );

mPositionsManager.calculateDistance(latOrigin, lngOrigin, listDestinationPoint, param, place.getLocationId());
Was this article helpful?
Have more questions? Submit a request