{"id":367,"date":"2009-05-01T12:55:59","date_gmt":"2009-05-01T20:55:59","guid":{"rendered":"http:\/\/blog.mozilla.org\/webdev\/?p=367"},"modified":"2017-03-08T14:50:07","modified_gmt":"2017-03-08T22:50:07","slug":"geolocation-in-the-browser","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/","title":{"rendered":"Geolocation in the Browser"},"content":{"rendered":"<p>Firefox 3.5 makes it super simple to discover the location of a user on your website\u00a0 You can read more about it from <a href=\"http:\/\/dougt.wordpress.com\/2009\/04\/30\/geolocation-in-firefox-35-and-fennec\/\">Doug Turner<\/a> or the <a href=\"http:\/\/www.mozilla.com\/firefox\/geolocation\/\">official Mozilla page<\/a>, but today I want to look at <a href=\"https:\/\/developer.mozilla.org\/En\/Using_geolocation\">how to use<\/a> the new <abbr title=\"Awesome Programming Interface\">API<\/abbr>.<\/p>\n<p>The following image is a screenshot of geolocation plus Google maps.  If you&#8217;re running Firefox 3.5, we&#8217;ll replace that with a real map showing your current position.<\/p>\n<p><div id=\"attachment_385\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are.jpg\"><img aria-describedby=\"caption-attachment-385\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg\" alt=\"Geolocation services finding me at the Mozilla office.\" title=\"geolocation screenshot\" width=\"300\" height=\"191\" class=\"size-medium wp-image-385\" srcset=\"https:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg 300w, https:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are.jpg 513w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-385\" class=\"wp-caption-text\">Geolocation finding me at the Mozilla office.<\/p><\/div><br \/>\n<script type=\"text\/javascript\" src=\"http:\/\/maps.google.com\/maps?file=api&#038;v=2&#038;sensor=true&#038;key=ABQIAAAAdKqUl5P5ngUsPujIZwvdLxSceV2u1YX1A4SJMX68dXj2a2_D8BSebGgC9lpsF2DiYm8ICbuEBsBiJw\"><\/script><br \/>\n<!-- Sorry about this nastiness, see http:\/\/people.mozilla.com\/~jbalogh\/geo.html for a readable example --><br \/>\n<script type=\"text\/javascript\"> function geo() { if (navigator.geolocation){ if (location.pathname.indexOf('geolocation-in-the-browser') != -1) { navigator.geolocation.getCurrentPosition(mapper); } } } function mapper(position) { if (GBrowserIsCompatible()) { var map = document.getElementById('attachment_385'); map.style.height = '300px'; map.style.width = '450px'; var gmap = new GMap2(map); var point = new GLatLng(position.coords.latitude, position.coords.longitude); gmap.setCenter(point, 15); gmap.addOverlay(new GMarker(point)); gmap.setUIToDefault(); } } document.addEventListener('DOMContentLoaded', geo, false); <\/script><\/p>\n<p>First, we should check that the browser supports the <a href=\"http:\/\/dev.w3.org\/geo\/api\/spec-source.html\">Geolocation API<\/a>:<\/p>\n<pre><code>if (!navigator.geolocation) { alert('Get a better browser'); }<\/code><\/pre>\n<p>Now that we&#8217;ve cleared out the riff-raff we can dive in to the new feature:<\/p>\n<p><a href=\"https:\/\/developer.mozilla.org\/En\/Using_geolocation#section_2\"><\/p>\n<pre><code>navigator.geolocation.getCurrentPosition()<\/code><\/pre>\n<p><\/a><\/p>\n<p>I love the simplicity of this API; that&#8217;s all you need to know to get the user&#8217;s position!  To do something useful, we&#8217;ll need a function to handle a successful positioning call:<\/p>\n<pre><code>navigator.geolocation.getCurrentPosition(function(position) {\r\n    alert(position.coords.latitude + \", \" + position.coords.longitude);\r\n});<\/code><\/pre>\n<p>Since it takes a few seconds to gather location data and send it up to the geolocation services, we must use an asynchronous callback instead of a return value.<\/p>\n<p>The <a href=\"https:\/\/developer.mozilla.org\/En\/Using_geolocation#section_4\">Position object<\/a> has two fields: <code>timestamp<\/code> and <code>coords<\/code>.  <a href=\"https:\/\/developer.mozilla.org\/En\/NsIDOMGeoPositionCoords\"><code>position.coords<\/code><\/a> holds all the position and velocity information, with the two most interesting (on my stationary desktop) being <code>latitude<\/code> and <code>longitude<\/code>.  The motion information will be a lot more fun on mobile devices, especially in conjunction with <a href=\"https:\/\/developer.mozilla.org\/En\/Using_geolocation#section_3\">navigator.geolocation.watchPosition<\/a>.<\/p>\n<p>This blog goes over the most basic usage of the geolocation API, but I have a slightly more involved\u00a0example available at <a href=\"http:\/\/people.mozilla.com\/~jbalogh\/geo.html\">http:\/\/people.mozilla.com\/~jbalogh\/geo.html<\/a>.  If you <a href=\"http:\/\/gist.github.com\/105157\">view the source<\/a> you can check out <code>geolocation<\/code>, <code>-moz-border-radius<\/code>, and <code>-moz-box-shadow<\/code> in action.<\/p>\n<p>NOTE: due to a <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=490740\">bug<\/a> in the current Firefox 3.5b4, the geolocation services may fail on repeated calls.  The bug has been fixed, but we&#8217;re stuck with it in the current release.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Firefox 3.5 makes it super simple to discover the location of a user on your website\u00a0 You can read more about it from Doug Turner or the official Mozilla page, but today I want to look at how to use &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/\">Continue reading<\/a><\/p>\n","protected":false},"author":1438,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[288],"tags":[],"coauthors":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Geolocation in the Browser - Mozilla Web Development<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"mozilla\" \/>\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\":\"WebPage\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/\",\"url\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/\",\"name\":\"Geolocation in the Browser - Mozilla Web Development\",\"isPartOf\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#primaryimage\"},\"thumbnailUrl\":\"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg\",\"datePublished\":\"2009-05-01T20:55:59+00:00\",\"dateModified\":\"2017-03-08T22:50:07+00:00\",\"author\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9\"},\"breadcrumb\":{\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#primaryimage\",\"url\":\"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg\",\"contentUrl\":\"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blog.mozilla.org\/webdev\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Geolocation in the Browser\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#website\",\"url\":\"https:\/\/blog.mozilla.org\/webdev\/\",\"name\":\"Mozilla Web Development\",\"description\":\"For make benefit of glorious tubes\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blog.mozilla.org\/webdev\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9\",\"name\":\"mozilla\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/image\/e77ee64829d0c3831212656324f746d1\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g\",\"caption\":\"mozilla\"},\"url\":\"https:\/\/blog.mozilla.org\/webdev\/author\/mozilla-2\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Geolocation in the Browser - Mozilla Web Development","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:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/","twitter_misc":{"Written by":"mozilla","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/","url":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/","name":"Geolocation in the Browser - Mozilla Web Development","isPartOf":{"@id":"https:\/\/blog.mozilla.org\/webdev\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#primaryimage"},"image":{"@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#primaryimage"},"thumbnailUrl":"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg","datePublished":"2009-05-01T20:55:59+00:00","dateModified":"2017-03-08T22:50:07+00:00","author":{"@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9"},"breadcrumb":{"@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#primaryimage","url":"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg","contentUrl":"http:\/\/blog.mozilla.org\/webdev\/files\/2009\/05\/do-you-know-where-your-browsers-are-300x191.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/blog.mozilla.org\/webdev\/2009\/05\/01\/geolocation-in-the-browser\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blog.mozilla.org\/webdev\/"},{"@type":"ListItem","position":2,"name":"Geolocation in the Browser"}]},{"@type":"WebSite","@id":"https:\/\/blog.mozilla.org\/webdev\/#website","url":"https:\/\/blog.mozilla.org\/webdev\/","name":"Mozilla Web Development","description":"For make benefit of glorious tubes","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blog.mozilla.org\/webdev\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/70ae25c16f09d053c6d8b5eac29dbda9","name":"mozilla","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blog.mozilla.org\/webdev\/#\/schema\/person\/image\/e77ee64829d0c3831212656324f746d1","url":"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/75d2017e019c87560fe5d148a64659dc?s=96&d=mm&r=g","caption":"mozilla"},"url":"https:\/\/blog.mozilla.org\/webdev\/author\/mozilla-2\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts\/367"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/users\/1438"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/comments?post=367"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/posts\/367\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/media?parent=367"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/categories?post=367"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/tags?post=367"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/webdev\/wp-json\/wp\/v2\/coauthors?post=367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}