04 geolocation

16
HTML5 GEOLOCATION Where am I?

description

 

Transcript of 04 geolocation

Page 1: 04 geolocation

HTML5 GEOLOCATION Where am I?

Page 2: 04 geolocation

Location-aware apps and websites are all the rage today

Page 3: 04 geolocation

Quiz: Geolocation must be done on the ____________

a. Server b. Browser

Page 4: 04 geolocation

Remember how a page is delivered

3. Browser renders the page

2. Server responds with that

page

1. Browser requests a page

Page 5: 04 geolocation

How does geolocation know where I am?

Page 6: 04 geolocation

How does geolocation know where I am?

Page 7: 04 geolocation

No, but seriously ... Google Location Services (via Skyhook Wireless) • mac_address is the mac address of the WiFi node. •  signal_strength is current signal strength measured in

dBm. •  age is the number of milliseconds since the WiFi node

was detected. • SSID is the name or ESSID of the WiFi node. GPS Cell towers

Page 8: 04 geolocation

Geolocation only works with the user's permission

Page 9: 04 geolocation

Geolocation requires these JavaScript steps 1.  Detect if geolocation is supported 2.  Request current position from the user 3.  If if fails, handle the error 4.  If it succeeds, do something with the coordinates

Page 10: 04 geolocation

Use the geolocation object to determine browser support if (typeof navigator.geolocation == 'undefined') {! alert("No geolocation");!}!else {! //Do geolocation stuff here!}!

Page 11: 04 geolocation

Request the current location from the browser • Uses the async pattern •  navigator.geolocation.getCurrentPosition(success,failure)

Page 12: 04 geolocation

If the request fails, handle the problem function failure(error) {! switch(error.code) {! case 1:! alert('Permission denied'); break;! case 2:! alert('Position unavailable'); break;! case 3:! alert('Timed out'); break;! case 4:! alert('Unknown error'); break;! }!}!

Page 13: 04 geolocation

If the request succeeds, grab the position from coords.latitude and coords.longitude function success (pos) {! var out = "lat: " + pos.coords.latitude + " lon: " + pos.coords.longitude; console.log(out); }!

Page 14: 04 geolocation

The precision is contained in coords.accuracy

•  position.coords.accuracy

Page 15: 04 geolocation

Hands on geolocation

Page 16: 04 geolocation

Conclusion • Geolocation can be determined with just a few lines of

JavaScript • We determine if it is supported by looking at

browser.geolocation • Get the location with geolocation.getCurrentPosition(a,b) •  Failure gives us one of four failure codes •  Location comes from position.coords.latitude and

position.coords.longitude