{"id":1369,"date":"2016-05-13T08:19:59","date_gmt":"2016-05-13T08:19:59","guid":{"rendered":"http:\/\/www.capitalnumbers.com\/blog\/?p=1369"},"modified":"2025-08-11T11:46:03","modified_gmt":"2025-08-11T11:46:03","slug":"how-to-detect-users-location-and-generate-coordinates-using-html5-geolocation","status":"publish","type":"post","link":"https:\/\/www.capitalnumbers.com\/blog\/how-to-detect-users-location-and-generate-coordinates-using-html5-geolocation\/","title":{"rendered":"How to Detect User\u2019s Location and Generate Coordinates Using HTML5 Geolocation"},"content":{"rendered":"<p><!--<a href=\"http:\/\/www.capitalnumbers.com\/blog\/wp-content\/uploads\/2016\/05\/HTML5-Geolocation.jpg\"><img class=\"aligncenter size-full wp-image-1370\" src=\"http:\/\/www.capitalnumbers.com\/blog\/wp-content\/uploads\/2016\/05\/HTML5-Geolocation.jpg\" alt=\"HTML5 Geolocation\" width=\"1000\" height=\"600\" srcset=\"https:\/\/www.capitalnumbers.com\/blog\/wp-content\/uploads\/2016\/05\/HTML5-Geolocation.jpg 1000w, https:\/\/www.capitalnumbers.com\/blog\/wp-content\/uploads\/2016\/05\/HTML5-Geolocation-300x180.jpg 300w, https:\/\/www.capitalnumbers.com\/blog\/wp-content\/uploads\/2016\/05\/HTML5-Geolocation-768x461.jpg 768w\" sizes=\"(max-width: 1000px) 100vw, 1000px\" \/><\/a>--><\/p>\n<p>There are instances when you\u2019d like web users to provide their locations to applications that they are using. This is because of privacy reasons. For instance, you may want to seek the user\u2019s permission to report his\/her location information to an application that you\u2019re using.<\/p>\n<p>The Geolocation feature in HTML5 is an API that can help you get user\u2019s location data, but only if with their permissions. In this blog post, we explore this HTML5 API feature. Let\u2019s dive in.<\/p>\n<p><strong>Checking browser compatibility for HTML5 Geolocation API<\/strong><\/p>\n<p>Of course, the first thing that you should do even before you think of implementing this feature on your website is checking whether your current browser is compatible with Geolocation feature. Here\u2019s the code that can help you check for compatibility:<\/p>\n<pre>if (navigator.geolocation) {\n}\nelse {\nalert (\u201cThe Geolocation API is not compatible with your browser. Update your browser\u201d);\n}<\/pre>\n<p><!-- This site is converting visitors into subscribers and customers with OptinMonster - http:\/\/optinmonster.com --><\/p>\n<div id=\"om-b7tfondceubrbvku-holder\">\u00a0If your browser isn\u2019t compatible, then download and install the latest version that supports HTML5. Most modern and latest browsers should support the Geolocation feature.<\/div>\n<p><strong>How can you get the current location of the user?<\/strong><\/p>\n<p>You\u2019ll use the \u201cgetCurrentPosition\u201d function of the \u201cnavigator.geolocation\u201d object. The getCurrentPosition function accepts three arguments as its parameters. These are the Success callback, Error callback and position options. If the location data of the user has been fetched successfully, then the success callback is invoked with the obtained position object as the input parameter.<\/p>\n<p>Otherwise, the error callback function is invoked with the error object as the input parameter. Here\u2019s the code that can get the current location of user:<\/p>\n<pre>if (navigator.geolocation) {\nnavigator.geolocation.getCurrentPosition(showPosition, showError);\n}\nelse {\nalert (\u201cThe Geolocation API is not compatible with your browser. Update your browser\u201d);\n}<\/pre>\n<p>Let\u2019s dive deeper and learn more about the three functions<\/p>\n<p><strong>#1: Success Callback function<\/strong><\/p>\n<p>This function will only be invoked when the web user accepts to share his\/her current location information. In this case, the location information will be available as the position object where the function will be called using the position object as the input parameter. The position object will indicate the timestamp which denotes the time at which the location data was retrieved and coordinates of the object.<\/p>\n<p>The coords object has the following characteristics:<\/p>\n<p>\u25cf \u00a0Latitude and longitude. These are the geographical coordinates expressed in decimal degrees.<br \/>\n\u25cf \u00a0Accuracy. It specifies the accuracy levels of the latitude and longitude coordinates in meters.<br \/>\n\u25cf \u00a0Altitude. It\u2019s the height of the current user\u2019s position above the sea level expressed in meters.<br \/>\n\u25cf \u00a0AltitudeAccuracy. It specifies the how far off the altitude position could be from the actual attitude value<br \/>\n\u25cf \u00a0Heading. It indicates the 360-degree heading information.<br \/>\n\u25cf \u00a0Speed. It provides the relative speed of the web user in meters per second.<\/p>\n<p><strong>#2: Error callback function<\/strong><\/p>\n<p>The Error callback function is an optional callback function which takes the Position Error object as its input argument. It is invoked under the following conditions:<\/p>\n<p>\u25cf \u00a0When an unknown error has occurred<br \/>\n\u25cf \u00a0When the request has timed out<br \/>\n\u25cf \u00a0When the user has denied sharing his\/her current location information.<br \/>\n\u25cf \u00a0The location information isn\u2019t available.<\/p>\n<p><strong>#3: Position Options<\/strong><\/p>\n<p>It indicates the options that are available for use while retrieving the web user\u2019s location. These options are:<\/p>\n<p>\u25cf \u00a0EnableHighAccuracy. It\u2019s a Boolean. If its value is true, then user agent will try to provide the most accurate \u00a0 \u00a0 position. Otherwise, if its value is false, then a less accurate position is obtained.<br \/>\n\u25cf \u00a0Timeout. It indicates the maximum time which a user agent can take to respond to the location data request.<br \/>\n\u25cf \u00a0MaximumAge. It specifies how long the user can keep using the cached location data before getting the new location information.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are instances when you\u2019d like web users to provide their locations to applications that they are using. This is because of privacy reasons. For instance, you may want to seek the user\u2019s permission to report his\/her location information to an application that you\u2019re using. The Geolocation feature in HTML5 is an API that can &#8230;<\/p>\n","protected":false},"author":12,"featured_media":1370,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false},"categories":[723,743],"tags":[789,843,850,1002,1054,1133],"acf":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts\/1369"}],"collection":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/comments?post=1369"}],"version-history":[{"count":3,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts\/1369\/revisions"}],"predecessor-version":[{"id":16349,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/posts\/1369\/revisions\/16349"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/media\/1370"}],"wp:attachment":[{"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/media?parent=1369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/categories?post=1369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.capitalnumbers.com\/blog\/wp-json\/wp\/v2\/tags?post=1369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}