1
Geolocation
Jay Urbain, PhD
Credits:
- http://www.w3schools.com
- Jpassion.com
- Dr. Mark Hornick
Geolocation Jay Urbain, PhD Credits: http://www.w3schools.com - - PowerPoint PPT Presentation
Geolocation Jay Urbain, PhD Credits: http://www.w3schools.com Jpassion.com Dr. Mark Hornick 1 What is Geolocation? Built-in browser functionality that lets you make a web application location-aware Geographic
1
Built-in browser functionality that lets you make a web
Geographic latitude/longitude (north-south/east-west) Supported by all modern browsers Internet Explorer 9+, Firefox, Chrome, Safari and Opera
2
Show user’s position on a map Indicate proximity to hospitals, coffee shops, public
Provide directions to a known point of interest Alert users to friends in the area Notify users of events, traffic, local weather… Provide auto-assist for zip code, area code, shipping
3
This security mechanism is built-in and cannot be disabled (part of HTML5 specification).
Collected info is sent to the default location service provider (Google Location Services) to get an estimate
4
Universally available on any computer Often resolves to your ISP’s office location, so
Limits what you can do with the information Local weather forecast Default language to present to user
5
Google (and others) have an extensive database of Wi-Fi
If multiple Wi-Fi hotspots are available, triangulation based
Relies on accuracy of the database.
6
Cell towers report their geographical location to the
When multiple cell towers are available, triangulation
Fairly accurate (~100ft) Works indoors and outdoors In (rural) areas with few cell towers, accuracy is poor
7
Device hosting the browser contains hardware that
Can be highly accurate (~3ft) Available everywhere Works outdoors only – view of sky required Accuracy decreases in areas with tall buildings,
8
You don’t – the browser will decide internally how it is
However, you can specify how accurate you want the
The browser may communicate with your device to
9
10
if( navigator.geolocation ) {// check for geolocation support navigator.geolocation.getCurrentPosition( displayLoc ); } else { // no geolocation object in BOM navigator alert(“No geolocation support!”); } ... // This function is called when the location is found function displayLoc( position ) { var lat = position.coords.latitude; var long = position.coords.longitude; // TODO: display the position somewhere on the page }
11
!" #$
12
getCurrentPosition takes 1, 2, or 3 arguments:
getCurrentPosition( successHandler, /* optional */ errorHandler, /* optional */ positionOptions); // This function is called when an error occurs: function errorHandler( error ) { // error.code is a value from 0 – 3: // 0: unknown error // 1: Permission denied by user // 2: position not available // 3: request timed out // if the error code is 0 or 2, error.message may // contain a more specific reason for the failure }
13
if( navigator.geolocation ) {// check for geolocation support wID = navigator.geolocation.watchPosition( displayLoc ); } else { // no geolocation object in BOM navigator alert(“No geolocation support!”); } // Call this function to cancel the watch function stopWatch() { if( wID ) { // make sure wID is not null navigator.geolocation.clearWatch( wID ); wID = null; } }
14
getCurrentPosition( successHandler, /* optional */ errorHandler, /* optional */ positionOptions); // The positionOptions argument is a map: var positionOptions { enableHighAccuracy: true, // favor accuracy over speed timeout: Infinity, // default; other values in ms maximumAge: 0 // default; other values in ms }
15
<script src="http://maps.google.com/maps/api/js?sensor=true"> </script> function displayMap(position) { // display a Google Map var googlePosition = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); var mapOptions = { zoom: 10, // range 0 to 21 center: googlePosition, // location of center of map mapTypeId: google.maps.MapTypeId.ROADMAP // ROADMAP, SATELLITE, or HYBRID }; var mapDiv = $("#map").get(0); // get the DOM <div> element var map = new google.maps.Map(mapDiv, mapOptions); }
16
!" #$ $
function addMarker(map, position, title, content) { var markerOptions = { position: position, // position of the push-pin map: map,// the map to put the pin into title: title, // title of the pin clickable: true // enable info window pop-up }; var marker = new google.maps.Marker(markerOptions); }
17
function addMarker(map, position, title, content) { … var marker = new google.maps.Marker(markerOptions); var infoWindowOptions = { content: content, // description in pop-up position: position // where to put it }; var infoWindow = new google.maps.InfoWindow(infoWindowOptions); google.maps.event.addListener(marker, "click", function() { infoWindow.open(map); }); }
18