{"id":59,"date":"2026-01-14T08:21:06","date_gmt":"2026-01-14T08:21:06","guid":{"rendered":"http:\/\/99webtools.com\/get-user-geolocation-using-javascript-2\/"},"modified":"2026-01-14T08:21:06","modified_gmt":"2026-01-14T08:21:06","slug":"get-user-geolocation-using-javascript-2","status":"publish","type":"page","link":"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html","title":{"rendered":"Get user geolocation using javascript"},"content":{"rendered":"<div class=\"col-md-8\">\n<article id=\"post-20\" class=\"post-20 post type-post status-publish format-standard hentry category-javascript\">\n<header class=\"entry-header\">\n<h1 class=\"entry-title\">Get user geolocation using javascript<\/h1>\n<div class=\"entry-meta\">\n\t\t\t<span class=\"date\"><span class=\"glyphicon glyphicon-time\">&nbsp;<\/span> <a href=\"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html\" title=\"Permalink to Get user geolocation using javascript\" rel=\"bookmark\"><time class=\"entry-date\" datetime=\"2013-02-28T18:00:30+00:00\">February 28, 2013<\/time><\/a><\/span><span class=\"categories-links\"><span class=\"glyphicon glyphicon-folder-open\">&nbsp;<\/span> <a href=\"https:\/\/99webtools.com\/blog\/category\/javascript.html\" rel=\"category tag\">Javascript<\/a><\/span><span class=\"author vcard\"><span class=\"glyphicon glyphicon-user\">&nbsp;<\/span> <a class=\"url fn n\" href=\"https:\/\/99webtools.com\/blog\/author\/sunny.html\" title=\"View all posts by Sunny\" rel=\"author\">Sunny<\/a><\/span>\t\t\t\t\t\t<\/div>\n<\/header>\n<p><!-- .entry-header --><\/p>\n<div class=\"entry-content\">\n<p class=\"bms8vtyd8szzajbcw\">W3C has introduced geolocation API in html5. We can use this API to get geographic location of client side device using javascript. Location of device determined by various sources like GPS, wifi network, Bluetooth mac address, GSM\/CDMA cell IDs etc. In this article i will explain how to use this API.<\/p>\n<h3>List of Geolocation API capable browsers<\/h3>\n<ul>\n<li>Internet Explorer 9+<\/li>\n<li>Firefox 3.5+<\/li>\n<li>Chrome<\/li>\n<li>Safari 5+<\/li>\n<li>Opera 10.6+<\/li>\n<\/ul>\n<p class=\"bms8vtyd8szzajbcw\"><span id=\"more-20\">&nbsp;<\/span><\/p>\n<h2>Code to get Geolocation<\/h2>\n<pre class=\"brush: js\">window.onload=function(){\nif(navigator.geolocation)\n{\nnavigator.geolocation.getCurrentPosition(showPosition);\n}\nelse\n{\nalert(\"Geolocation is not supported by this browser.\");\n}\n}\nfunction showPosition(pos){\nalert(\"Latitude: \"+pos.coords.latitude+\"nLongitude: \"+pos.coords.longitude);\n}<\/pre>\n<p class=\"bms8vtyd8szzajbcw\">The <strong>getCurrentPosition()<\/strong> Method Returns Following Data<\/p>\n<table>\n<thead>\n<tr>\n<th>Property<\/th>\n<th>Description<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>coords.latitude<\/td>\n<td>The latitude as a decimal number<\/td>\n<\/tr>\n<tr>\n<td>coords.longitude<\/td>\n<td>The longitude as a decimal number<\/td>\n<\/tr>\n<tr>\n<td>coords.accuracy<\/td>\n<td>The accuracy of position<\/td>\n<\/tr>\n<tr>\n<td>coords.altitude<\/td>\n<td>The altitude in meters above the mean sea level<\/td>\n<\/tr>\n<tr>\n<td>coords.altitudeAccuracy<\/td>\n<td>The altitude accuracy of position<\/td>\n<\/tr>\n<tr>\n<td>coords.heading<\/td>\n<td>The heading as degrees clockwise from North<\/td>\n<\/tr>\n<tr>\n<td>coords.speed<\/td>\n<td>The speed in meters per second<\/td>\n<\/tr>\n<tr>\n<td>timestamp<\/td>\n<td>The date\/time of the response<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p class=\"bms8vtyd8szzajbcw\">&nbsp;<\/p>\n<h2>Error handling<\/h2>\n<p class=\"bms8vtyd8szzajbcw\">The second parameter of the getCurrentPosition() method is used to handle errors. It specifies a function to run if it fails to get the user\u2019s location<\/p>\n<pre class=\"brush: js\">window.onload=function(){\nif(navigator.geolocation)\n{\nnavigator.geolocation.getCurrentPosition(showPosition,showError);\n}\nelse\n{\nalert(\"Geolocation is not supported by this browser.\");\n}\n}\nfunction showPosition(pos){\nalert(\"Latitude: \"+pos.coords.latitude+\"nLongitude: \"+pos.coords.longitude);\n}\nfunction showError(error)\n  {\n  switch(error.code)\n    {\n    case error.PERMISSION_DENIED:\n      alert(\"User denied the request for Geolocation.\")\n      break;\n    case error.POSITION_UNAVAILABLE:\n      alert(\"Location information is unavailable.\")\n      break;\n    case error.TIMEOUT:\n      alert(\"The request to get user location timed out.\")\n      break;\n    case error.UNKNOWN_ERROR:\n      alert(\"An unknown error occurred.\")\n      break;\n    }\n  }\n<\/pre>\n<h2>.watchPosition() Method<\/h2>\n<p class=\"bms8vtyd8szzajbcw\">This method Returns the current position of the user and continues to return updated position as the user moves.<\/p>\n<pre class=\"brush: js\">window.onload=function(){\nif(navigator.geolocation)\n{\nnavigator.geolocation.watchPosition(showPosition);\n}\nelse\n{\nalert(\"Geolocation is not supported by this browser.\");\n}\n}\nfunction showPosition(pos){\nalert(\"Latitude: \"+pos.coords.latitude+\"nLongitude: \"+pos.coords.longitude);\n}<\/pre>\n<h2>.clearWatch() Method<\/h2>\n<p class=\"bms8vtyd8szzajbcw\">This method clears even created by .watchPosition() Method<\/p>\n<pre class=\"brush: js\">var watchID;\nwindow.onload=function(){\nif(navigator.geolocation)\n{\nwatchID=navigator.geolocation.watchPosition(showPosition);\n}\nelse\n{\nalert(\"Geolocation is not supported by this browser.\");\n}\n}\nfunction showPosition(pos){\nalert(\"Latitude: \"+pos.coords.latitude+\"nLongitude: \"+pos.coords.longitude);\n}\n\/\/call this function to stop location updates\nfunction stopWatch(){\n   navigator.geolocation.clearWatch(watchID);\n}\n<\/pre>\n<div class=\"wp_rp_wrap  wp_rp_plain\" id=\"wp_rp_first\">\n<div class=\"wp_rp_content\">\n<h3 class=\"related_post_title\">More from 99webtools<\/h3>\n<ul class=\"related_post wp_rp\">\n<li data-position=\"0\" data-poid=\"in-184\" data-post-type=\"none\"><a href=\"https:\/\/99webtools.com\/blog\/get-profile-picture-from-facebook-google-twitter-and-gravatar.html\" class=\"wp_rp_title\">Get profile picture from facebook, google, twitter and gravatar<\/a><\/li>\n<li data-position=\"1\" data-poid=\"in-124\" data-post-type=\"none\"><a href=\"https:\/\/99webtools.com\/blog\/trick-to-get-indexed-by-google-fast.html\" class=\"wp_rp_title\">Trick to get indexed by Google fast<\/a><\/li>\n<li data-position=\"2\" data-poid=\"in-80\" data-post-type=\"none\"><a href=\"https:\/\/99webtools.com\/blog\/get-weather-information-using-php.html\" class=\"wp_rp_title\">Get weather information using php<\/a><\/li>\n<li data-position=\"3\" data-poid=\"in-51\" data-post-type=\"none\"><a href=\"https:\/\/99webtools.com\/blog\/get-address-from-latitudelongitude-in-php.html\" class=\"wp_rp_title\">Get address from Latitude\/Longitude in php<\/a><\/li>\n<li data-position=\"4\" data-poid=\"in-5\" data-post-type=\"none\"><a href=\"https:\/\/99webtools.com\/blog\/php-script-to-get-pagerank.html\" class=\"wp_rp_title\">PHP script to get pagerank<\/a><\/li>\n<\/ul>\n<\/div>\n<\/div><\/div>\n<p><!-- .entry-content --><\/p>\n<footer>\n<div class=\"comments-link\">\n\t\t\t\t\t<a href=\"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html#respond\"><span class=\"leave-reply\">Leave a reply<\/span><\/a>\t\t\t\t<\/div>\n<p><!-- .comments-link --><br \/>\n\t\t\t\t\t\t\t\t<\/footer>\n<p><!-- .entry-meta --><br \/>\n\t<\/article>\n<p><!-- #post --><br \/>\n<br class=\"clearfix\"><\/p>\n<div class=\"well well-sm text-center\" id=\"subscribe\">\n<h3>Liked It? Get Free updates in your Email<\/h3>\n<form class=\"form\" role=\"form\" action=\"https:\/\/feedburner.google.com\/fb\/a\/mailverify\" method=\"post\" target=\"popupwindow\" onsubmit=\"window.open('https:\/\/feedburner.google.com\/fb\/a\/mailverify?uri=99webtools', 'popupwindow', 'scrollbars=yes,width=550,height=520');return true\">\n<div class=\"row\">\n<div class=\"col-md-offset-3 col-md-6\">\n<div class=\"input-group\"><input type=\"text\" name=\"email\" class=\"form-control\" placeholder=\"Enter Email\"><span class=\"input-group-btn\"><button type=\"submit\" value=\"Subscribe\" class=\"btn btn-warning\">Subscribe <i class=\"fa fa-rss\"><\/i><\/button><\/span><\/div>\n<\/div>\n<p><input type=\"hidden\" value=\"99webtools\" name=\"uri\"\/><input type=\"hidden\" name=\"loc\" value=\"en_US\"\/><\/div>\n<p class=\"bms8vtyd8szzajbcw\">Delivered by <img decoding=\"async\" alt=\"feedburner\" src=\"https:\/\/99webtools.com\/files\/img\/feed.png\" border=\"0\"><\/p>\n<\/form>\n<\/div>\n<nav class=\"navigation\">\n<ul class=\"pager\">\n<li class=\"previous\"><a href=\"https:\/\/99webtools.com\/blog\/php-script-to-get-alexa-rank.html\" rel=\"prev\"><span class=\"glyphicon glyphicon-arrow-left\">&nbsp;<\/span> PHP Script to get Alexa Rank<\/a><\/li>\n<li class=\"next\"><a href=\"https:\/\/99webtools.com\/blog\/extract-website-data-using-php.html\" rel=\"next\">Extract website data using php <span class=\"glyphicon glyphicon-arrow-right\">&nbsp;<\/span><\/a><\/li>\n<\/ul>\n<\/nav>\n<p><!-- .nav-single --><\/p>\n<div id=\"comments\" class=\"comments-area\">\n<div id=\"respond\" class=\"comment-respond\">\n<h3 id=\"reply-title\" class=\"comment-reply-title\">Leave a Reply <small><a rel=\"nofollow\" id=\"cancel-comment-reply-link\" href=\"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html#respond\" style=\"display:none;\">Cancel Reply<\/a><\/small><\/h3>\n<form action=\"https:\/\/99webtools.com\" method=\"post\" id=\"commentform\" class=\"comment-form\" novalidate>\n<p class=\"comment-notes\"><span id=\"email-notes\">Your email address will not be published.<\/span> Required fields are marked <span class=\"required\">*<\/span><\/p>\n<div class=\"comment-form-comment row\">\n<div class=\"col-md-10\"><label for=\"comment\" class=\"sr-only\">Comment<\/label><textarea id=\"comment\" name=\"comment\" rows=\"8\" aria-required=\"true\" placeholder=\"Comment...\"><\/textarea><\/div>\n<\/div>\n<div class=\"comment-form-author row\">\n<div class=\"col-md-6\"><label for=\"author\" class=\"sr-only\">Name<\/label> <\/p>\n<div class=\"input-group\"><span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-user\">&nbsp;<\/span><br \/>\n<\/span><input id=\"author\" class=\"form-control\" name=\"author\" type=\"text\" value=\"\" size=\"30\" aria-required=\"true\" placeholder=\"Name\"><\/div>\n<\/div>\n<\/div>\n<div class=\"comment-form-email row\">\n<div class=\"col-md-6\"><label for=\"email\" class=\"sr-only\">Email<\/label> <\/p>\n<div class=\"input-group\"><span class=\"input-group-addon\"><span class=\"glyphicon glyphicon-envelope\">&nbsp;<\/span><br \/>\n<\/span><input id=\"email\" class=\"form-control\" name=\"email\" type=\"email\" value=\"\" size=\"30\" aria-required=\"true\" placeholder=\"Email\"><\/div>\n<\/div>\n<\/div>\n<p class=\"comment-form-cookies-consent\"><input id=\"wp-comment-cookies-consent\" name=\"wp-comment-cookies-consent\" type=\"checkbox\" value=\"yes\"\/> <label for=\"wp-comment-cookies-consent\">Save my name, email, and website in this browser for the next time I comment.<\/label><\/p>\n<p class=\"form-submit\"><input name=\"submit\" type=\"submit\" id=\"submit\" class=\"submit\" value=\"Post Comment\"\/> <input type=\"hidden\" name=\"comment_post_ID\" value=\"20\" id=\"comment_post_ID\"\/><br \/>\n<input type=\"hidden\" name=\"comment_parent\" id=\"comment_parent\" value=\"0\"\/>\n<\/p>\n<p style=\"display: none;\"><input type=\"hidden\" id=\"akismet_comment_nonce\" name=\"akismet_comment_nonce\" value=\"e14f17e4b8\"\/><\/p>\n<p style=\"display: none;\"><input type=\"hidden\" id=\"ak_js\" name=\"ak_js\" value=\"38\"\/><\/p>\n<\/form><\/div>\n<p><!-- #respond -->\n<\/div>\n<p><!-- #comments .comments-area -->\n\t\t<\/div>\n","protected":false,"raw":""},"excerpt":{"rendered":"<p>Get user geolocation using javascript &nbsp; February 28, 2013&nbsp; [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"template_3.php","meta":{"_acf_changed":false,"inline_featured_image":false,"footnotes":""},"class_list":["post-59","page","type-page","status-publish","hentry"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Get user geolocation using javascript - Web Tools<\/title>\n<meta name=\"description\" content=\"W3C has introduced geolocation API in html5. We can use this API to get geographic location of client side device using javascript. Location of device\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Get user geolocation using javascript - Web Tools\" \/>\n<meta property=\"og:description\" content=\"W3C has introduced geolocation API in html5. We can use this API to get geographic location of client side device using javascript. Location of device\" \/>\n<meta property=\"og:url\" content=\"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html\" \/>\n<meta property=\"og:site_name\" content=\"Web tools | SEO tools | Web development articles\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Ber\u00e4knad l\u00e4stid\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minuter\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Get user geolocation using javascript - Web Tools","description":"W3C has introduced geolocation API in html5. We can use this API to get geographic location of client side device using javascript. Location of device","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html","og_type":"article","og_title":"Get user geolocation using javascript - Web Tools","og_description":"W3C has introduced geolocation API in html5. We can use this API to get geographic location of client side device using javascript. Location of device","og_url":"https:\/\/99webtools.com\/blog\/get-user-geolocation-using-javascript.html","og_site_name":"Web tools | SEO tools | Web development articles","twitter_card":"summary_large_image","twitter_misc":{"Ber\u00e4knad l\u00e4stid":"2 minuter"}},"_links":{"self":[{"href":"https:\/\/99webtools.com\/wp-json\/wp\/v2\/pages\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/99webtools.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/99webtools.com\/wp-json\/wp\/v2\/types\/page"}],"replies":[{"embeddable":true,"href":"https:\/\/99webtools.com\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":0,"href":"https:\/\/99webtools.com\/wp-json\/wp\/v2\/pages\/59\/revisions"}],"wp:attachment":[{"href":"https:\/\/99webtools.com\/wp-json\/wp\/v2\/media?parent=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}