Get User Location Securely in JavaScript without Privacy-Hassle

updated on 03 December 2020
Get user location without privacy hassle

Written by Kimmo Ihanus

To detect your website or webapp users' location, you have options to either use browser's Geolocation API or the client's IP-address. Whether your goal is to eg. personalize content, target advertisement, or create redirects, you need to send your users' latitude and longitude data, or IP-address to a third-party geocoding service. These services extract the readable place names from the data and return it to your client.

The process is straightforward, but it comes with privacy drawbacks.

Hyper-secure location detection with pointNG:

To save time and avoid privacy-headaches, follow these steps to identify your user's GPS-based location without sending their coordinates away from the browser.

1. Install pointNG location detection javascript plugin on each page on your website you wish to use your users' location: <script src="https://cdn.pointng.io/pointng-min.js"></script> 2. Paste this code below to initialize and configure pointNG: const pointng = new PointNG({ level: 'city', api_key: 'YOUR-API-KEY-HERE'}); Use the 'level' parameter to choose the level you wish to identify your users' location. Options are 'continent', 'country', 'state' and 'city'. If a user declines your website to use her GPS-location, pointNG uses the device time zone as the default fallback to estimate the location.

3. Sign up here to whitelist your domain, and grab your free API-key. Paste the key to the initialization script's 'api_key' parameter.

Do your magic!

That's it! Now you are ready to start building location-based web services securely without the fear of data misuses and privacy headaches.

Check these examples for what types of personalization you can make based on a user's location.

Read more