{"id":2850,"date":"2018-12-10T10:02:00","date_gmt":"2018-12-10T10:02:00","guid":{"rendered":"https:\/\/www.htmlgoodies.com\/uncategorized\/navigating-html5-geolocation\/"},"modified":"2021-04-27T23:11:23","modified_gmt":"2021-04-27T23:11:23","slug":"html-geolocation","status":"publish","type":"post","link":"https:\/\/www.htmlgoodies.com\/mobile\/html-geolocation\/","title":{"rendered":"HTML Geolocation: How to Find Someone’s Location"},"content":{"rendered":"
Since using GPS navigation is very common nowadays, it has completely changed the way people use maps. Everything is digitalized, and paper version of maps are now outdated. However, with the ability to know exactly where you are, you must agree that you allow the application to do so, because gathering your data without your permission endangers your privacy. That is why applications that use HTML Geolocation will always ask for your permission. So, the user will be prompted with a popup or dialog requesting the user’s permission to share the location information. The user can accept or deny the request. The Geolocation API of HTML5 helps in identifying the user’s location, which can be used to provide location-specific information or route navigation details to the user.<\/p>\n
There are many techniques used to identify the location of the user. A desktop browser generally uses WIFI- or IP-based positioning techniques, whereas a mobile browser uses cell triangulation, GPS, A-GPS, WIFI-based positioning techniques, etc. The Geolocation API uses any one of these techniques to identify the user’s location. At the base of every location-based application is positioning and Geolocation which is similar to the use of positioning systems but is more focused on determining a meaningful location (such as a street address) rather than just a set of geographic coordinates.<\/p>\n
In this article, you will learn the Geolocation capabilities of HTML5. The API provides a method to locate the user’s, more or less exact, position. This is useful in a number of ways that range from providing a user with location-specific information to providing route navigation. There is more than one way to figure out where you are — your IP address, your wireless network connection, which cell tower your phone is talking to, or dedicated GPS hardware that calculates latitude and longitude from information sent by satellites.<\/p>\n
In HTML5, we have a set of APIs to effectively allow the client-side device (i.e. your iPhone 3G+, Android 2.0+ phones, or even your conventional desktop browsers) to retrieve geographic positioning information with JavaScript. The Geolocation API is supported by the following browsers and smartphones. Minimum version requirements are mentioned as well.<\/p>\n
The HTML Geolocation API is used to get the geographical position of a user. Since this can compromise privacy, the position is not available unless the user approves it.<\/p>\n
To return the user’s position, the The example below returns the latitude and longitude of the user’s position:<\/p>\n After pressing the button, you should get you own coordinates:<\/p>\n <\/p>\n The second parameter of the The <\/p>\n If you’re building a cool web app or mobile app, you could use the The example below shows the getCurrentPosition()<\/code> method is used.<\/p>\n
<!DOCTYPE html>\n<html>\n<body>\n\n<button onclick=\"getLocation()\">Your coordinates are:<\/button>\n\n<p id=\"Test\"><\/p>\n\n<script>\nvar x = document.getElementById(\"Test\");\n\nfunction getLocation() {\n \/\/Check if Geolocation is supported \n if (navigator.geolocation) {\n\t \/\/If supported, run the getCurrentPosition() method\n\t \/\/If the getCurrentPosition() method is successful, it returns a coordinates object to the function specified in the parameter (showPosition)\n navigator.geolocation.getCurrentPosition(showPosition);\n } else { \n\t \/\/If not, display a message to the user\n x.innerHTML = \"Geolocation is not supported by this browser.\";\n }\n}\n\n\/\/The showPosition() function outputs the Latitude and Longitude\nfunction showPosition(position) {\n x.innerHTML = \"Latitude: \" + position.coords.latitude + \n \"<br>Longitude: \" + position.coords.longitude;\n}\n<\/script>\n\n<\/body>\n<\/html><\/pre>\n
Handling Errors<\/h2>\n
getCurrentPosition()<\/code> method is used to manage errors. Possible error codes are:<\/p>\n
\n
function showError(error) {\n switch(error.code) {\n case error.PERMISSION_DENIED:\n x.innerHTML = \"User denied the request for Geolocation.\"\n break;\n case error.POSITION_UNAVAILABLE:\n x.innerHTML = \"Location information is unavailable.\"\n break;\n case error.TIMEOUT:\n x.innerHTML = \"The request to get user location timed out.\"\n break;\n case error.UNKNOWN_ERROR:\n x.innerHTML = \"An unknown error occurred.\"\n break;\n }\n}<\/pre>\n
The getCurrentPosition() Method – Return Data<\/h2>\n
getCurrentPosition()<\/code> method returns an object on success. Notice that the latitude, longitude and accuracy properties are always returned. The other properties are returned if available:<\/p>\n
Periodic Updates of Geolocation Position<\/h2>\n
watchPosition();<\/code> method instead of
getCurrentPosition()<\/code>, as this retrieves periodic updates about the current geographic location of the device. To clear anything being watched, simply use the
clearWatch();<\/code> method when necessary:<\/p>\n
\n
watchPosition()<\/code> — Returns the current position of the user and continues to return updated position as the user moves (like the GPS in a car).<\/li>\n
clearWatch()<\/code> — Stops the
watchPosition()<\/code> method.<\/li>\n<\/ul>\n
watchPosition()<\/code> method:<\/p>\n
<!DOCTYPE html>\n<html>\n<body>\n\n<button onclick=\"getLocation()\">Your coordinates are:<\/button>\n\n<p id=\"Test\"><\/p>\n\n<script>\nvar x = document.getElementById(\"Test\");\n\nfunction getLocation() {\n \/\/Check if Geolocation is supported \n if (navigator.geolocation) {\n\t \/\/the watchposition() method show the position of the user and update it while is moving\n\t navigator.geolocation.watchPosition(showPosition);\n } else { \n\t \/\/If not, display a message to the user\n x.innerHTML = \"Geolocation is not supported by this browser.\";\n }\n}\n\/\/The showPosition() function outputs the Latitude and Longitude\nfunction showPosition(position) {\n x.innerHTML = \"Latitude: \" + position.coords.latitude + \n \"<br>Longitude: \" + position.coords.longitude;\n}\n<\/script>\n\n<\/body>\n<\/html><\/pre>\n
Display the Result in a Map<\/h2>\n