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 Distance API 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 instanciate 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 SerachAPI are strored in the SDK database, you can retrieve data from the SDK database like this :

POI[] poiList = WoosmapDb.getInstance(mContext, true).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:

WoosmapGeofencing.shared.setSearchAPIParameters(parameters: ["radius":"20000","stores_by_page":"2", "query":"type:\"click&collect\""])
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(), true));

// set latitude and longitude of the location, and the id of the location if you want to update a location in database of the SDK or you can set to 0 for the id location.
mPositionsManager.searchAPI( location.getLatitude(), location.getLongitude(), location.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 Distance API to retrieve distance and duration value

As for the Search API, request to the Distance API 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, instanciate Woosmap, set keys, specifies the mode of transport to use when calculating distance and set a listener to monitor result of the DistanceAPI request :

@Override
protected void onCreate(Bundle savedInstanceState) {
    // Set Keys
    WoosmapSettings.privateKeyWoosmapAPI = woosmapPrivateKey;
    
    //Specifies the mode of transport to use when calculating distance. Valid values are "driving", "cycling", "walking". (if not specified default is driving)
    WoosmapSettings.modeDistance = "driving";

    // Set the Distance API listener 
    this.woosmap.setDistanceAPIReadyListener(new WoosDistanceAPIReadyListener()); 
}

Distance API Delegate

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

public class WoosDistanceAPIReadyListener implements Woosmap.DistanceAPIReadyListener {
    public void DistanceAPIReadyCallback(DistanceAPI distanceAPIData) {
        // result of Distance API request
    }
}

Send request to the Distance API

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 API.

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

// set latitude and longitude of the origin, 
// set the latitude and longitude of the destinations,
// and the id of the location if you want to update a POI in database of the SDK or you can set to 0 for the id location.
List<Pair<Double, Double>> listDestinationPoint = new ArrayList<>();
listDestinationPoint.add(new Pair(place.getLatitude(), place.getLongitude()));
Double latOrigin = currentPosition.getLatitude();
Double lngOrigin = currentPosition.getLongitude();
mPositionsManager.distanceAPI(latOrigin,lngOrigin,listDestinationPoint,place.getLocationId());

More details about the Woosmap Distance API

Was this article helpful?
Have more questions? Submit a request