Get User Position
Retrieve the estimated Woosmap Latitude/Longitude for the current userThis API has been deprecated and will be terminated on the 31/03/2021.
Need to automate user geolocation? Check out the Geolocation API instead.
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 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.