Display and access user's OkHi locations on your website.
The OkHi JavaScript library provides you with a quick and easy way to start collecting high accuracy location information from your users. Its extremely light-weight (~5kb), which eliminates any load performance concerns.
Video demo
New user checkout, delivery address collection
Return user checkout, zero click pre-selected delivery address
Prerequisites
api-key: You can obtain an api key by signing up here
Onward to the code 🚀
Include the OkHi JavaScript library to your HTML document. Make sure to place the script tag above your own JavaScript file.
The OkHi Location Card displays the most recently used location by the user. It also allows them to create or select another location via the Location Manager. To get started, create a div on your HTML Document where you'd want to display the location card and use the OkHi library as shown below. In the example the user's phone number is obtained from the data attribute in the HTML document, but feel free to customise this to however that suits you.
var okhi =newOkHi({ apiKey:'my_api_key' });var element =document.getElementById('lets-okhi');var user = { firstName:element.getAttribute('data-firstname'), lastName:element.getAttribute('data-lastname'), phone:element.getAttribute('data-phone'),};varhandleOnSuccess=function(data) {// handle your success here with the data you get back};varhandleOnError=function (error) {// handle errors here e.g fallback to your own way of collecting a location information };var locationCard =newokhi.LocationCard({ element: element, user: user, onSuccess: handleOnSuccess, onError: handleOnError,});
#lets-okhi {width:100%;height:300px;}
*IMPORTANT* The onSuccess callback may be called multiple times as the user changes or manages their location.
Launching the OkHi Location Manager
The OkHi Location Manager enables the user to create or select an already existing location. It can be launched from a simple button click or any other triggers you wish to have. In the example below the user information is obtained from the data attribute in the HTML document, but feel free to customise this to however that suits you.
var button =document.getElementById('lets-okhi');varhandleOnSuccess=function (data) {// handle your success here with the data you get back};varhandleOnError=function (error) {// handle errors here e.g fallback to your own way of collecting a location information };varhandleButtonClick=function () {var okhi =newOkHi({ apiKey:'my_api_key' });var user = { phone:this.dataset.phone, firstName:this.dataset.firstname, lastName:this.dataset.lastname, };var locationManager =newokhi.LocationManager({ user: user, onSuccess: handleOnSuccess, onError: handleOnError, });locationManager.launch();};button.addEventListener('click', handleButtonClick);
Want more customization options? We've got you covered! Check out the API Reference for styling configuration and more