Get the User Position

Retrieve the estimated Woosmap Latitude/Longitude for the current user.
  1. Introduction
  2. Retrieve the user position
  3. Implementation

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

loading

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:

  1. Synchronously downloads the recommendation.js JavaScript library from https://sdk.woosmap.com/recommendation/recommendation.js.

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

  3. Get the current status of user consent for this project using getConsent(callback(consent)). If consent === true, you can proceed to retrieve the user estimated position. If false, you should optIn the user.

  4. Get the Woosmap estimated user position with getUserPosition({successCallback}). The successCallback(latlng) method is called if the request returns without errors. But this does not mean Woosmap found the user position. You have to check for latlng Object is not empty. (if ('latitude' in latlng) in this sample).

  5. Display a Google Static Map centered on the returned user position.