Get the User Position
Retrieve the estimated Woosmap Latitude/Longitude for the current user.Introduction
The recommendation.js
is a lightweight JavaScript library for quickly requesting the Woosmap Recommendation REST API. The library is designed to be small (less than 4Ko) and multi-browser (ie9 support).
This page shows you how to use Woosmap Recommendation Javascript API to provide a personalized and local experience to your visitors.
Adding the following code to your site’s templates is the easiest way to get started using recommendation.js
.
The getUserPosition(GetPositionOptions)
returns a successCallback
with the Woosmap estimated Latitude and Longitude for the current user.
Please, check the GetPositionOptions object Specification to see how to use this method in details.
Retrieve the user position
Implementation
<script src="https://sdk.woosmap.com/recommendation/recommendation.js"></script>
<script>
(publicKey => {
woosmapRecommendation.setProjectKey(publicKey);
woosmapRecommendation.getConsent((consent) => {
if (consent === true) {
woosmapRecommendation.getUserPosition({
successCallback(latlng) {
if ('latitude' in latlng) {
userPositionMap.src = `//maps.googleapis.com/maps/api/staticmap?markers=icon:https://images.woosmap.com/user-position.png|${latlng.latitude},${latlng.longitude}&zoom=14&size=600x400&maptype=roadmap&key=XXX`;
} else {
userPositionMap.src = "/assets/images/no_map.png";
}
}
});
} else if (consent === false) {
// invite the user to accept your privacy policy/cookies
// before optIn him to Woosmap recommendation
// using woosmapRecommendation.optIn();
}
});
})('WOOS-XXXX-YYYY');
</script>
The above code does four main things:
-
Synchronously downloads the
recommendation.js
JavaScript library fromhttps://sdk.woosmap.com/recommendation/recommendation.js
. -
Initializes the Recommendation process with
setProjectKey(publicKey)
for the project specified via the'WOOS-XXXX-YYYY'
parameter. This method GET or CREATE the Woosmap User Id Cookie. -
Get the current status of user consent for this project using
getConsent(callback(consent))
. Ifconsent === true
, you can proceed to retrieve the user estimated position. Iffalse
, you should optIn the user. -
Get the Woosmap estimated user position with
getUserPosition({successCallback})
. ThesuccessCallback(latlng)
method is called if the request returns without errors. But this does not mean Woosmap found the user position. You have to check forlatlng
Object is not empty. (if ('latitude' in latlng)
in this sample). -
Display a Google Static Map centered on the returned user position.