Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Using Firefox’s Geolocation API

8 Responses »
Mozilla Firefox Geolocation

One interesting aspect of web development is Geolocation; where is your user viewing your website from? You can base your language locale on that data or show certain products in your store based on the user's location. Let's examine how you can use Geolocation within Firefox to get location details down to the street!

Detecting Browser Geolocation Capabilities

if(navigator.geolocation) {
	//w00t!
}
else {
	alert('No soup for you!  Your browser does not support this feature');
}

They key to detecting Geolocation within your browser is the navigator.geolocation object.

Querying For Geolocation Information

if(navigator.geolocation) {
	navigator.geolocation.getCurrentPosition(function(position) {
		alert('Your lat-long is: ' + position.coords.latitude + ' / ' + position.coords.longitude);
		alert('You live in ' + position.address.city + ', ' + position.address.state)
	});
}

The navigator.geolocation.getCurrentPosition method is the driving force behind retrieving location details. Once you call this method (providing it a function which will execute if your request is successful), the browser will ask the user if they will allow you to retrieve their location information:

Mozilla Firefox Geolocation Permissions

When the user allows the website to retrieve their location information, Firefox fetches the information, providing you a position object with two very useful attributes: position.coords and position.address. position.coords gives you latitude and longitude while position.address gives you streetNumber, street, premises, city, county, region, country, countryCode, and postalCode. Firefox may even take a picture of your user through his/her window soon. (Editor's note: I completely made that up)

Of course most of this functionality is only available within Firefox. Safari will retrieve the user's latitude and longitude but that's the extent of WebKit's support. I look forward to bigger and better things from the W3C's Geolocation API in the future!

Discussion

  1. June 28, 2010 @ 11:10 am

    It will be cool when this becomes a viable option on sites (when more browsers support it)

  2. mn
    June 28, 2010 @ 2:19 pm

    @Dan: I’m going to write a book about the history of the WWW and that’s going to be the title.

  3. makepanic
    June 28, 2010 @ 2:41 pm

    Latitude and Longitude work fine on Opera 10.60.
    Kinda scary how easy it is to get your location…

  4. June 28, 2010 @ 3:07 pm

    Wow, this creeps me out David! You always seem to amaze me… :-O
    A feature like this could be very handy for all those annoyed people who don’t like filling in address forms. Does this always work, even with firewalls/DNS/hotspots/ripping internet from your neighbours?

  5. adrian
    June 29, 2010 @ 6:42 am

    Hi…I implemented W3C Geo location within MooGeo and you can view in action some demos: http://thinkphp.ro/apps/js-hacks/MooGeo/

  6. lancia
    June 29, 2010 @ 6:48 am

    Hi…I implemented W3C Geo location within MooGeo and you can view in action some demos: http://is.gd/d8Kgm

  7. chris willis
    June 29, 2010 @ 11:55 am

    Lat and Long seems to work in Chrome 6 for me.

  8. christian baldinato
    July 1, 2010 @ 4:08 am

    It’s really interesting and cool but it depends a lot on your ISP, for example, I live in Vicenza, Italy, our company uses Fastweb as ISP, and geolocation responds that I stay in Modena, a city in an other region!!!!

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!