Petar Strinic

and all those sorts of things…

While playing around with a weather widget, the other day, I though to myself why not use HTML5’s geolocation capabilities to show the user the weather for their current location automatically. This turns out to be exceedingly easy, if you’re willing to live with a couple of small assumptions, such as it’s safe to use a regular expression to parse the zip code out of Google Maps API’s formatted_address. Although they do offer a more specific “postal code” field as part of the address object, this is just a little quicker to get to, if you’re willing to assume a format like “, IL 60601”.

In easy steps…

1. Get the user’s latitude and longitude, we can do something like this:

2. Get the address information from Google maps and do the regex hack to get the zip out of the formatted_address.

Shady demo: http://petarstrinic.com/examples/whatIsMyZip.html

Share

April 7th, 2012

Posted In: Misc

Tags: , , ,

8 Comments

  • Anonymous says:

    this… this is good

  • Bruce says:

    Thanks, but it does not work on Safari on the mac. I get the error. Works fine on Chrome and Firefox. Is there a way to make this work for Safari?

  • admin says:

    It works on Mac+Safari for me. Screenshot: http://grab.by/iTHU

    Can you tell me what sort of an error you are seeing? Anything in the console log?

  • Glenn says:

    Works great in FF and Chrome and IE – does not work on mac safari for me either.

    Any ideas?

    Thanks for posting

  • admin says:

    It works in Safari on my MacBook, but it did prompt me to allow it to use my location twice. Do you have “Limit website access to location services” in the Privacy tab of settings set to “Deny without prompting”, by chance?

  • Edison Leon says:

    I got the following after allowing geolocation in the console (safary on Mac and windows, latest)

    [Log] err (whatIsMyZip.html, line 30)
    [Log] err 10 seconds expired (whatIsMyZip.html, line 30)
    [Log] err (whatIsMyZip.html, line 30)
    [Log] err 10 seconds expired (whatIsMyZip.html, line 30)
    [Log] err (whatIsMyZip.html, line 30)
    [Log] err 10 seconds expired (whatIsMyZip.html, line 30)

  • Li Boud says:

    navigator.getlocation.getCurrentPosition Should be navigator.geolocation.getCurrentPosition

  • J says:

    As other forums have noted on the topic, you MUST put the results on a map, or you’re violating the Google API Terms of Service.

Leave a Reply

Your email address will not be published.