HTML5 Geolocation | Kebab Finder

By | March 25, 2011

The HTML5 Geo location API hasn’t really got anything to do with the HTML5 spec. In fact it was created by the W3C but seems to have been lumped in with the spec in everyone’s mind. And so the only way you’ll ever find this post is if I title it HTML5 Geo location.

So what is all this geo location stuff?

It’s magic!!? Basically the geolocation api gives us a way to locate the exact position of the visitor to our site. It uses either GPS or wifi. If it’s the latter it uses something called wifi triangulation to pin point where the user is (I have no idea what this means but I thought I would include it anyway in case you do!)

Finding my own way

Over the past week or so I’ve been thinking “Well it’s ok all this stuff but how could I use it?”. Then in a flash of inspiration it came to me. “A Kebab Finder” (I did have help from Dave at My Photoshop Textures but I’m going to take all the credit, cheers Dave). So the idea is that you come out of a nightclub/pub (a little worse for wear if you’re anything like me) and maybe you’re in a different town, maybe on a stag do and all you want is one of a few things. 1) To get home 2) to get something to eat 3) To go to the Casino. This is where Kebab Finder comes in. You open up the page and not only does it find where you are (using the geo location) but it also tells you the nearest service of your choice. Sound good? No? Well tough that’s as good as it gets for me. Now to pull this off we need to link into the Google AJAX Local Search API and I’m not going to show you how that works here, I’m just going to concentrate on location stuff.

So where am I?

Before we do anything we need to do some feature detection to make sure the browser supports the functionality that we need. In the past it was common to test for browsers not features so you would test for Firefox, IE or Chrome etc but this becomes a nightmare because there are different versions of browsers supporting different features. So now we test for features, if it doesn’t support it then we know not to do anything.

function OnLoad() {
				if (navigator.geolocation) {
						navigator.geolocation.getCurrentPosition(handle_geo); 
						$("#queryInput").change(doSearch);
					} else {
						var map = document.getElementById("map");
						map.innerHTML = "Geolocation is not available... have you considered a forward-thinking browser?";
					}	
			};

So there are a couple of things going on here. The function is called on load and as you can see it says if navigator geolocation is is available do one thing else do something er…. else. So what are we doing if we have the feature available is use the getCurrentPosition function and past it another function called handle_geo. The getCurrentPosition is a catch all function that basically does all the hard work for you and gets the visitors current location. We pass it the other function because we want to use the co-ordinates to position the map. The next function here is for the menu that just says when you change the selection in the menu run the doSearch function, we’ll look at that in a minute. After the else statement we just put a message for anyone using an old browser to say it doesn’t support what we’re trying to do.

function handle_geo(position){ 
				// Initialize the map with default UI.
					gMap = new google.maps.Map(document.getElementById("map"), {
					center: new google.maps.LatLng(position.coords.latitude, position.coords.longitude),
					zoom: 15,
					mapTypeId: 'roadmap'
				});
      // Create one InfoWindow to open when a marker is clicked.
       gInfoWindow = new google.maps.InfoWindow;
      	google.maps.event.addListener(gInfoWindow, 'closeclick', function() {
        unselectMarkers();
      });			
 
      // Initialize the local searcher
      gLocalSearch = new GlocalSearch();
      gLocalSearch.setSearchCompleteCallback(null, OnLocalSearch);
			
      var myLatlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      var marker = new google.maps.Marker({
      position: myLatlng, 
      map: gMap, 
      title:"You are here!"
 			 });
			doSearch();
    }

The handle_geo function takes one parameter, that is position. This holds the position of the current visitor. So all we need to do is feed this position into the google map api to get the centre for the map. We also set the zoom setting for the map and the map type. We also create the info windows the local search callback function and the marker (with the same co-ordinates and the center of the map).

function doSearch() {
      var query = document.getElementById("queryInput").value;
      gLocalSearch.setCenterPoint(gMap.getCenter());
      gLocalSearch.execute(query);
    }

So the final function I’m going to look at is the doSearch function. This is how we pass the different values from the menu to the search. We get the query from the element with the id of queryInput. Then we set the centre point from where we want to do the local search, which is the centre of the map then we execute the search using the value from the menu.
As I said I’m not going into detail about the Google api I just thought it would give more relevance to the tutorial if you could see a working example.

In Conclusion

The geolocation api is very easy to use in it’s basic form. Here we have only used one of the 3 the available functions. We will look at the others in later tutorials. I have included two demo pages one with the simple marker of the location of the visitor and the second the fully functioning Kebab finder.
DEMO1 Simple Geolocation | DEMO2 Kebab Finder

11 thoughts on “HTML5 Geolocation | Kebab Finder

  1. Pingback: Anonymous

  2. baunel

    Hi,
    I like your website. it help me lot because I m young developer.
    your demo kebab finder is very nice.
    I would like make it.
    is it possible to have this demo’s script doawnload?
    thanks et good work

    Reply
    1. Matt Walker Post author

      Sorry I usually put the code up to download. I will do that tonight.

      Reply
  3. baunel

    hi
    thanks you advance, I wait it.

    I begin with HTML5, what trips can you give me between HTML5+CSS3 and IE6,IE7,IE8?
    have you already tried CSS3PIE?

    Reply
    1. Matt Walker Post author

      You can grab the code from the demo but yeah I’ll upload the source code to make it easier.

      Reply
  4. mohd tariq khan

    i want to geolocation script in which locator pin adds and it move place to place when i move , and get latitude and longitude of this place.
    please give responce

    Reply
  5. Brett

    Great tutorial.
    Is there a way to force the browser to not ask for permission when finding the location like your kebab shop finder?
    Thanks 🙂

    Reply
    1. Matt Walker Post author

      Not as far as I know, which is a shame. We have found this a blocker at work from using HTML Geolocation.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *