We Make Awesome Sh

Discovering distance to a location using javascript

I saw @MrQwest tweet the other day that he was going to be creating something that showed the user something different if they were near a location. He asked Twitter if anyone had done something similar.

Last Christmas I was asked to make a web site for the launch of the latest Rolling Stones’ greatest hits album Grrr. For the launch, the Stones were having a pop up shop on Carnaby Street. Polydor, the label the band is signed to, wanted to be able to allow fans to listen to the album via their mobile phone if they were within a mile of the store while they were shopping. It would also allow fans to listen to the album easily and for free while in the pop up store before they decided to buy the album.

For this example I checked using the new(ish) HTML5 JavaScript geolocation API for the users location:

https://gist.github.com/sydlawrence/6708285

The location object
has longitude and latitude as attributes along with various others. Once I have got their location I then needed to discover their distance from a known location, using the longitude and latitude.

https://gist.github.com/6708282

So now I know their distance, I can then show them the relative information. Now there are a few things to bear in mind, location data can be spoofed. The browser could pretend it is somewhere else. The band and label were very aware with this for the project.

There we go, simples.

Recent Articles

comments powered by Disqus