Geolocation

an HTML5 API

Getting your position could involve

wi-fi, 4G triangulation and GPS

1. https web server
2. JavaScript

(3. Google API-key)

navigator.geolocation.getCurrentPosition(yes, no);

function yes(geoObj) {
    console.log(geoObj.coords.latitude);
    console.log(geoObj.coords.longitude);
}

function no(errorObj) {
    alert(errorObj.message);
}

/*
############################################################
Coordinates object (coords) 
############################################################

coords.latitude:           the position's latitude in decimal degrees.

coords.longitude:          the position's longitude in decimal degrees.

coords.altitude:           the position's altitude in metres, relative to sea level. null if the implementation cannot provide the data.

coords.accuracy:           the accuracy of the latitude and longitude properties, expressed in meters.

coords.altitudeAccuracy:   the accuracy of the altitude expressed in meters. can be null.

coords.heading:            the direction in which the device is traveling. This value, specified in degrees, indicates how far off from heading true north the device is. 0 degrees represents true north, and the direction is determined clockwise (which means that east is 90 degrees and west is 270 degrees). If speed is 0, heading is NaN. If the device is unable to provide heading information, this value is null.

coords.speed:              the velocity of the device in meters per second. This value can be null.
*/

Basic usage of the geolocation API

var ID = navigator.geolocation.watchPosition(yes, no);

function yes(geoObj) {
    console.log(geoObj.coords.latitude);
    console.log(geoObj.coords.longitude);
}

function no(errorObj) {
    alert(errorObj.message);
}


navigator.geolocation.clearWatch(ID);

Constantly getting the position:

demo of the geolocation API

So,

what do we do with those numbers?

Google Maps API

Create a basic map

Tons of map examples and complete documentation:

https://developers.google.com/maps/documentation/javascript/

geolocation

By Johan Kohlin

geolocation

  • 728