Find nearest POIs

Find nearest POIs using iOS 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.

In your AppDelegate, set keys, and set a delegate to monitor result of the Search API request :

let dataPOI = DataPOI()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        // Set Woosmap API Private key
        WoosmapGeofencing.shared.setWoosmapAPIKey(key: WoosmapKey)

         // Set delegate of protocol POI 
        WoosmapGeofencing.shared.getLocationService().searchAPIDataDelegate = DataPOI()

        return true
}

Search API Delegate

Get the result of the Search API request in your class delagate DataPOI :

public class DataPOI:SearchAPIDelegate  {
    public init() {}
    
    public func searchAPIResponse(poi: POI) {
        NotificationCenter.default.post(name: .newPOISaved, object: self, userInfo: ["POI": poi])
    }
    
    public func searchAPIError(error: String) {
        
    }
    
    public func readPOI()-> [POI] {
        return POIs.getAll()
    }
    
    func getPOIbyLocationID(locationId: String)-> POI? {
        return POIs.getPOIbyLocationID(locationId: locationId)
    }
    
    public func erasePOI() {
        POIs.deleteAll()
    }
    
}

More details about the Woosmap Search API

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\""])

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.

let location = CLLocation(latitude: latitude, longitude: longitude)
WoosmapGeofencing.shared.getLocationService().searchAPIRequest(location: location)

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:

WoosmapGeofencing.shared.setPoiRadius(radius: 200.0)

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

WoosmapGeofencing.shared.setPoiRadius(radius: "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). To retrieve distance and duration values between origin(s) and destination(s), you must create a class delegate to retrieve the those data when the method distanceAPIRequestis called.

In your AppDelegate, set keys, and set a delegate to monitor result of the Distance API request :

let dataDistance = DataDistance()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

         // Set delegate of protocol Distance
        WoosmapGeofencing.shared.getLocationService().distanceAPIDataDelegate = DataDistance()
        
        //Specifies the mode of transport to use when calculating distance. Valid values are "driving", "cycling", "walking". (if not specified default is driving)
        WoosmapGeofencing.shared.setDistanceAPIMode(mode: drivingModeDistance)

        return true
}

Distance API Delegate

Get the result of the Distance API request in your class delegate DataDistance :

public class DataDistance:DistanceAPIDelegate  {
    public init() {}
    
    public func distanceAPIResponseData(distanceAPIData: DistanceAPIData, locationId: String) {
        if (distanceAPIData.status == "OK") {
            if (distanceAPIData.rows?.first?.elements?.first?.status == "OK") {
                let distance = distanceAPIData.rows?.first?.elements?.first?.distance?.value!
                let duration = distanceAPIData.rows?.first?.elements?.first?.duration?.text!
                if(distance != nil && duration != nil) {
                    print(distance ?? 0)
                    print(duration ?? 0)
                }
            }
        }
    }
    
    public func distanceAPIError(error: String) {
        print(error)
    }
    
}

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.

let location = CLLocation(latitude: latitude, longitude: longitude)
let latDest = poi!.latitude
let lngDest = poi!.longitude
WoosmapGeofencing.shared.getLocationService().distanceAPIRequest(locationOrigin: location,coordinatesDest: [(latDest, lngDest)])

More details about the Woosmap Distance API

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