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

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 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). 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 provider 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()
        
        // [...]

        return true
}

Distance Delegate

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

public class DataDistance:DistanceAPIDelegate  {
    public init() {}
    
    public func distanceAPIError(error: String) {
        print(error)
    }

    public func distanceAPIResponse(distance: [Distance]) {
        print("distance = " + distance.debugDescription)
        
    }
    
}

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.

WoosmapGeofencing.shared.getLocationService().calculateDistance(
    // set latitude and longitude of the origin
    locationOrigin: location,
    
    // set the latitude and longitude of destinations,
    coordinatesDest: destinations,
    
    // [OPTIONAL] switch from a distance provider to another  
    distanceProvider: DistanceProvider.woosmapDistance,
    
    // [OPTIONAL] change the travel mode ("walking", "cycling" or "driving")
    distanceMode: DistanceMode.walking,
    
    // [OPTIONAL] change the distance units ("imperial" or "metric")
    distanceUnits: DistanceUnits.imperial,
    
    // [OPTIONAL] change the language for textual result (example: "en", "fr")
    distanceLanguage: "en"
)

More details about the Woosmap Distance API

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