Geolocation API

Lidia Wilczyńska

What I am doing here?

Presentation milestones

Geolocation: theory

Geo + Location

IsGeolocationPartOfHTML5.com

Question: Is geolocation part of HTML5? Why are you talking about it?
Answer: Geolocation support is being added to browsers right now, along with support for new HTML5 features. Strictly speaking, geolocation is being standardized by the Geolocation Working Group, which is separate from the HTML5 Working Group. But I’m going to talk about geolocation in this book anyway, because it’s part of the evolution of the web that’s happening now.

Browser support

Firefox IE Chrome Safari Opera iPhone Android BlackBerry
Dive into HTML5 3.5+ 9.0+ 5.0+ 5.0+ 10.6+ ?? 2.0+ ??
HTML5 Solutions 3.5+ 9.0+ 5.0+ 5.0+ 10.63+ 3.2+ 2.1+ OS 6.0

How does it work?

Geolocation: practise

Checking geolocation enabled?

Common API

getCurrentPosition(successPos, errorPos, [, additionalParams])

{
  coords: {
    latitude: [latitude], // degrees
    longitude: [longitude], // degrees
    accuracy: [accuracy], // meters
    altitude: [altitude], // meters
    altitudeAccuracy: [altitudeAccuracy], // meters
    speed: [speed], // meters/second
    heading: [heading],
        // angle (clockwise degrees from north)
  },
  timestamp: [timestamp] // miliseconds
}
            

getCurrentPosition(successPos, errorPos, [, additionalParams])

{
  enableHighAccuracy: [bool], 
  maximumAge: [milliseconds],
  timeout: [miliseconds]
}
            

getCurrentPosition(successPos, errorPos, [, additionalParams])

{
  code: [code], //number 
  message: [message] //string
}
            

  Error codes

How to use it?

watchPosition (successPos, errorPos, [, additionalParams])

var watchedUser = navigator.geolocation.watchPosition (
        successPos,
        errorPos,
        additionalParams
    );
//to clear
navigator.geolocation.clearWatch(watchedUser);
            

People done that

Geolocation in use

You could do that too!

distance on sphere

You could do that too! - from math to ours

function euclidDistance(x1, y1, x2, y2) {
    var r = 6371,
        i;
        
    for(i in arguments) {
        if(arguments.hasOwnProperty(i)) {
            arguments[i] = Math.PI * arguments[i] / 180;
        }
    }

    return r * Math.sqrt(
        2 - 2 * (
        Math.cos(x1)*Math.cos(x2)*Math.cos(y1-y2) +
            (Math.sin(x1) * Math.sin(x2))
        )
    );
}
            

You could do that too! - simplier

function estDistance(x1, y1, y2, x2) {
    var fc = 40000 / 360, //km per degree
        xDif = x2 - x1,
        yDif = y2 - y1;

    return Math.sqrt(
        xDif * xDif * fc * fc +
        yDIf * yDif * fc * fc
    );
}
            

User's point of view

Why geolocation is good for you

I have PMS and GPS, which means I'm a bitch who will find you!

Questions?

Bibliography

Thank you!

Go vote!