Information Technology Reference
In-Depth Information
There are two things to note about our first piece of code that tests for geolocation
support. First, it really is that simple to determine a given browser's support (although
see the section "What Could Possibly Go Wrong?" later in this chapter for some more
nuanced analysis of when a device with support for geolocation does not actually
cooperate). The second is that using the JavaScript alert function is annoying most of
the time in desktop applications, and it gets even more annoying on a small-screen
mobile device. The experienced general web developers among you will know this, while
for the novices, this is a good time to consider pushing your working code into a
function that you can easily reuse without annoying your good users. Listing 9-2
refactors our detection example to create a separate geoSupport() function that we can
re-use as we see fit.
Listing 9-2. Simple geolocation support check revised
<html>
<head>
<title>Android Web Application Development-Geolocation example 2</title>
<script type="text/javascript">
function supportsGeo () {
var geosupport = "Sorry, your browser does not support geolocation";
if (navigator.geolocation) {
geosupport = "Congratulations, your browser supports geolocation";
}
return geosupport;
}
</script>
</head>
<body>
<script type="text/javascript">
document.write(supportsGeo());
</script>
</body>
</html>
You could, of course, take this further and push this function into a separately loaded
JavaScript source file. You're free to do that and adapt the later code in this chapter, if
you wish.
Exploring Our Sample Application
It's time to introduce our sample application. Unless you've been living under a rock,
you're likely aware of the range of popular "check in" style applications that have
flourished along with the explosion of mobile devices. Whether it's Foursquare, Latitude,
TripIt, or other similar apps, these all allow you to announce your position to the wide
world—presumably so your fans and the paparazzi can track your famous movements.
Many of these applications are implemented as native applications for a given device,
but there's nothing that stops us from developing the same style of application as a web
application available within the browser of any device.
Our application is fully self-contained in ch09-example03.html , which you can run from
your chosen web browser. Let's take a look from the users' perspective first, to see
 
Search WWH ::




Custom Search