Finding Yourself: From Geolocation

Finding Yourself: From Geolocation

Geolocation is a technique for discovering where people are, based on their computer’s location. Of course, “computer” really can mean smart phone, tablet, or other portable device as well as a desktop computer. Geolocation determines a person’s whereabouts by looking at their computer’s IP address, MAC address, Wi-Fi hotspot location, or even GPS coordinates if available. Although not strictly part of HTML5 the specification, Geolocation is often associated with HTML5 because it’s coming on the scene at the same time. Unlike W e b Storage, Geolocation was never part of the HTML5 specification. Like W e b Storage, it’s a very useful technology that is already implemented in Firefox, Safari, and Chrome. Let’s see how we can use it.

Locating Awesomeness for Finding Yourself: From Geolocation

We have been asked to create a contact page for the AwesomeCo website, and the CIO has asked whether we could show people’s location on a map along with the various AwesomeCo support centers. He’d love to see a prototype, so we’ll get one up and running quickly. We’ll use Google’s Static Map API for this because it doesn’t require an API key and we’re just going to generate a very simple map. AwesomeCo service centers are located in Portland, Oregon; Chicago, Illinois; and Providence, Rhode Island. Google’s Static Map API makes it really easy to plot these points on a map. All we have to do is construct an img tag and pass the addresses in the URL, like this to Finding Yourself: From Geolocation:

<img id= "map" alt="Map of AwesomeCo Service Center locations"
src="http://maps.google.com/maps/api/staticmap?
&amp;size=900x300
&amp;sensor=false
&amp;maptype=roadmap
&amp;markers=color:green|label:A|1+Davol+square,+Providence,+RI+02906-3810
&amp;markers=color:green|label:B|22+Southwest+3rd+Avenue,Portland,+OR
&amp;markers=color:green|label:C|77+West+Wacker+Drive+Chicago+IL">

We define the size of the image, and then we tell the Maps API that we did not use any sensor device, such as a GPS or client-side geolocation with the information we’re passing to this map. Then we define each marker on the map by giving it a label and the address. We could use a comma-separated pair of coordinates for these markers if we had them, but this is easier for our demonstration.
How to Be Found to Finding Yourself: From Geolocation.  We need to plot our visitor’s current location on this map, and we’ll do that by providing another marker on the map by using our latitude and longitude for a new marker. W e can ask the browser to grab our visitor’s latitude and longitude, like this:

navigator.geolocation.getCurrentPosition(function(position) {
showLocation(position.coords.latitude, position.coords.longitude);
});

This method prompts the user to provide us with their coordinates. If the visitor allows us to use their location information, we call the showLocation( ) method. The showLocation( ) method takes the latitude and longitude and reconstructs the image, replacing the existing image source with the new one. Here’s how we implement that method for Finding Yourself: From Geolocation:

 var showLocation = function(lat, lng){
 var fragment = "&markers=color:red|color:red|label:Y|" + lat + "," + lng;
 var image = $( "#map" );
 var source = image.attr( "src" ) + fragment;
 source = source.replace( "sensor=false" , "sensor=true" );
 image.attr("src" , source);
 };

Rather than duplicate the entire image source code, we’ll append our location’s latitude and longitude to the existing image’s source. Before we assign the modified image source back to the document, we need to change the sensor parameter from false to true. We’ll do that on line 5 with the replace( ) method. When we bring it up in our browser, we’ll see our location, marked with a “Y” among the other locations. To see an example, take a look next page to Finding Yourself: From Geolocation.

Finding Yourself: Geolocation

Finding Yourself: From Geolocation

Geolocation is a technique for discovering where people are, based on their computer’s location. Of course, “computer” really can

Falling Back to Finding Yourself: From Geolocation

As it stands, visitors to the page will still see the map with the locations of the AwesomeCo support centers, but we will get a JavaScript error if we try to load our page. W e need to detect support for geolocation before we attempt to get the visitor’s location, like this to Finding Yourself: From Geolocation:

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
showLocation(position.coords.latitude, position.coords.longitude);
});
}else{
};

Google’s Ajax API 11 does location lookup, so it’s a great fallback solution. You will need to obtain an API key to use this on your site when you go live, but you don’t need one to try this locally.
Our fallback looks like this here to Finding Yourself: From Geolocation:

var key = "your_key" ;
 var script = "http://www.google.com/jsapi?key=" + key;
 $.getScript(script, function(){
 if ((typeof google == 'object') &&
 google.loader && google.loader.ClientLocation) {
 showLocation(google.loader.ClientLocation.latitude,
 google.loader.ClientLocation.longitude);
 }else{
 var message = $( "<p>Couldn't find your address.</p>" );
 message.insertAfter("#map" );
 };
 });

We’re using jQuery’s getScript( ) method to load the Google Ajax API. We then use Google’s ClientLocation( ) method on line 5 to get a visitor’s location and invoke our showLocation( ) method to plot the location on our map. Unfortunately, Google can’t geolocate every IP address out there, so we may still not be able to plot the user on our map; therefore, we account for that by placing a message underneath our image. Our fallback solution isn’t foolproof, but it does give us a greater chance of locating our visitor.
Without a reliable method of getting coordinates from the client, we’ll just need to provide a way for the user to provide us with an address, but that’s an exercise I’ll leave up to you.
The Future to Finding Yourself: From Geolocation
The techniques we talked about in this chapter, although not all part of HTML5 proper, represent the future of web development. We’ll be pushing many more things to the client side. Better history management will make Ajax and client-side applications much more intuitive. Web Sockets can replace periodic polling of remote services for the display of real-time data. Cross-document Messaging lets us merge web applications that usually would never be able to interact, and Geolocation will eventually let us build better location-aware web applications, which Finding Yourself: From Geolocation, become more and more relevant every day with the growing mobile computing market. Explore these APIs and keep an eye on their adoption. You may soon find these to be invaluable tools in your web development toolbox for Finding Yourself: From Geolocation.

andcombd