{"id":3225,"date":"2020-09-04T04:22:54","date_gmt":"2020-09-04T04:22:54","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=3225"},"modified":"2021-01-20T04:59:47","modified_gmt":"2021-01-20T04:59:47","slug":"user-location-detection-with-javascript","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/","title":{"rendered":"User Location Detection With Javascript"},"content":{"rendered":"<h2>Detect location(Coordinates\/Country) of your website visitors using Javascript<\/h2>\n<p>The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages depending upon different countries, or to display targeted information to visitors from different locations.<\/p>\n<p>We have two ways to do this:<\/p>\n<ol>\n<li>The Geolocation API <\/li>\n<li>IP address lookup<\/li>\n<\/ol>\n<h3>The Geolocation API<\/h3>\n<p>This is a new HTML5 feature. It allows a visitor to share their location with you by prompting if they\u2019d like to share their location with your site when you try to retrieve the location using this API. <\/p>\n<p>This is the pop-up you often see when you visit a website using Geolocation API.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/09\/google.png\" alt=\"google\"><\/p>\n<p>Here is the code for this:<\/p>\n<p><code><em>if (\u201cgeolocation\u201d in navigator) {<br \/>\n     \/\/ check if geolocation is enabled on current browser<br \/>\n     navigator.geolocation.getCurrentPosition(<br \/>\n          function success(position) {<br \/>\n     \t   console.log(\u2018latitude\u2019,position.coords.latitude,\u2019longitude\u2019,position.coords.longitude);<br \/>\n          },<br \/>\n         function error(error_message) {<br \/>\n             console.error(\u2018Error occured while retrieving location\u2019, error_message)<br \/>\n         }<br \/>\n     );<br \/>\n} else {<br \/>\n     \/\/ if geolocation is not supported<br \/>\n    console.log(\u2018geolocation is not enabled on this browser\u2019)<br \/>\n}<br \/>\n<\/em><\/code><\/p>\n<p><strong>Output:<\/strong>(run in my browser console)<br \/>\nlatitude 30.683182199999994 longitude 76.71986989999999<\/p>\n<p>There are some drawbacks to using this method:<\/p>\n<ul>\n<li>You can not get the location of the visitor doesn\u2019t allow you to do so.<\/li>\n<li>It only works on secure servers (https)<\/li>\n<li>It is not compatible with Internet Explorer 10 and below, nor with OperaMini.<\/li>\n<li>This gives you only the coordinates of their location not the actual location\/address in words.<\/li>\n<\/ul>\n<h3>IP Address Lookup<\/h3>\n<p>This is another way to get the location of your website visitors. We can use public IP lookup services\/APIs for this option(Paid as well as free).<br \/>\nWe can get data in various formats like JSON, XML, and CSV using IP Geolocation API, IPinfo, and GEOIP DB. In this example, we are using IP Geolocation API with the easiest library jQuery.<\/p>\n<p>Drawbacks of using this method:<\/p>\n<ul>\n<li>The coordinates are not always accurate but the country is always accurate.<\/li>\n<li>Some of these services require API Key which is paid.<\/li>\n<\/ul>\n<p>Here is the code:<\/p>\n<p><code><em>unction ipLookUp () {<br \/>\n          $.ajax(\u2018http:\/\/ip-api.com\/json\u2019).then(<br \/>\n                  function success(response) {<br \/>\n                       console.log(\u2018Location is \u2018, response);<br \/>\n                       console.log(\u2018Country is \u2018, response.country);<br \/>\n                  },<br \/>\n                 function fail(data, status) {<br \/>\n                       console.log(\u2018Request denied\u2019);<br \/>\n                  }<br \/>\n           );<br \/>\n}<br \/>\nipLookUp();<br \/>\n<\/em><\/code><\/p>\n<p><strong style=\"text-align:left;\">response will be this<\/strong><br \/>\n&#8220;status&#8221;:&#8221;success&#8221;,&#8221;country&#8221;:&#8221;India&#8221;,&#8221;countryCode&#8221;:&#8221;IN&#8221;,&#8221;region&#8221;:&#8221;PB&#8221;, &#8220;regionName&#8221;:&#8221;Punjab&#8221;,&#8221;city&#8221;:&#8221;Ludhiana&#8221;,&#8221;zip&#8221;:&#8221;141015&#8243;,&#8221;lat&#8221;:30.9146, &#8220;lon&#8221;:75.8543,&#8221;timezone&#8221;:&#8221;Asia\/Kolkata&#8221;, &#8220;isp&#8221;:&#8221;Reliance Jio Infocomm Limited&#8221;,&#8221;org&#8221;:&#8221;Rjil Internet&#8221;,&#8221;as&#8221;:&#8221;AS55836 Reliance Jio Infocomm Limited&#8221;,&#8221;query&#8221;:&#8221;157.39.110.109&#8243;}<\/p>\n<p>To convert actual geographic coordinates retrieved from above methods into a human-readable address is called <strong>Reverse Geocoding<\/strong>, that we will discuss in the future post.<\/p>\n<p><small><em>If you have skills in PHP programming and you want to enhance your career in this field, a PHP certification from StudySection can help you reach your desired goals. Both beginner level and expert level <a href=\"https:\/\/www.studysection.com\/cakephp-developer-foundation-diploma\">PHP Certification Exams<\/a> are offered by StudySection along with other programming certification exams. <\/em><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Detect location(Coordinates\/Country) of your website visitors using Javascript The location detection can be useful for you in a variety of<\/p>\n","protected":false},"author":1,"featured_media":3226,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[199,535],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>StudySection Blog - User Location Detection With Javascript<\/title>\n<meta name=\"description\" content=\"The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"StudySection Blog - User Location Detection With Javascript\" \/>\n<meta property=\"og:description\" content=\"The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-04T04:22:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-20T04:59:47+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/09\/location.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"User Location Detection With Javascript\",\"datePublished\":\"2020-09-04T04:22:54+00:00\",\"dateModified\":\"2021-01-20T04:59:47+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\"},\"wordCount\":401,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"JavaScript\",\"Location\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\",\"url\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\",\"name\":\"StudySection Blog - User Location Detection With Javascript\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2020-09-04T04:22:54+00:00\",\"dateModified\":\"2021-01-20T04:59:47+00:00\",\"description\":\"The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"User Location Detection With Javascript\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"StudySection Blog - User Location Detection With Javascript","description":"The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages.","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:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/","og_locale":"en_US","og_type":"article","og_title":"StudySection Blog - User Location Detection With Javascript","og_description":"The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages.","og_url":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2020-09-04T04:22:54+00:00","article_modified_time":"2021-01-20T04:59:47+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2020\/09\/location.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"User Location Detection With Javascript","datePublished":"2020-09-04T04:22:54+00:00","dateModified":"2021-01-20T04:59:47+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/"},"wordCount":401,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["JavaScript","Location"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/","url":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/","name":"StudySection Blog - User Location Detection With Javascript","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2020-09-04T04:22:54+00:00","dateModified":"2021-01-20T04:59:47+00:00","description":"The location detection can be useful for you in a variety of scenarios like to show your website\u2019s content in different languages.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/user-location-detection-with-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"User Location Detection With Javascript"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":230,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3225"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=3225"}],"version-history":[{"count":7,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3225\/revisions"}],"predecessor-version":[{"id":3859,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/3225\/revisions\/3859"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/3226"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=3225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=3225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=3225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}