Information Technology Reference
In-Depth Information
Listing 9-1.
Simple geolocation support check
<html>
<head>
<title>Android Web Application Development-Geolocation example 1</title>
</head>
<body>
<script type="text/javascript">
if (navigator.geolocation) {
alert("Congratulations, your browser supports geolocation");
} else {
alert('Sorry, your browser does not support geolocation');
}
</script>
</body>
</html>
You can find this code in the file
ch09-example01.html
. Simply place this under the
control of the web server of your choice, and then point your browser (desktop or
mobile) to it to see the results.
NOTE:
This example, and the others in this chapter, typically should be run from a web server,
rather than opened as a file straight from a desktop or mobile browser. This is because most
contemporary browsers prevent any
file://
based URL from accessing the geolocation API.
Put simply, any attempt to access a
navigator.geolocation
object, its methods, or data
members will silently fail. This is due principally to security concerns, which could fill a chapter
on their own. Choose a web server with which you are comfortable, such as Apache or Nginx,
and you won't see any issues.
Figures 9-2 and 9-3 show the results you should see when running our first example in
a desktop browser and mobile browser, respectively.