Using the Geolocation HTML5 API
We can use various resources to get the user's position, so we are using the HTML5 API in this example. We will use an external JavaScript file to create a map with the exact location of the user:
- Create a file called
getCurrentPosition.js
and save it within thepublic/js
folder. - Place the following code in
getCurrentPosition.js
:function getCurrentPosition() { // Check boreswer/navigator support if (navigator.geolocation) { var options = { enableHighAccuracy : true, timeout : Infinity, maximumAge : 0 }; navigator.geolocation.watchPosition(getUserPosition, trackError, options); } else { alert('Ops; Geolocation is not supported'); } // Get user position and place ...
Get Node.js 6.x Blueprints now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.