Posts Easy Native HTML5 Geolocation
Post
Cancel

Easy Native HTML5 Geolocation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
//https://gist.github.com/cceremuga/9001819
function successCallback(position) {
    //set latitude / longitude
    var nearLat = position.coords.latitude;
    var nearLong = position.coords.longitude;

    //do whatever you need with the lat / long here
    console.log('Latitude: ' + nearLat + ' Longitude: ' + nearLong);
}

function errorCallback(error) {
    var errorMessage = '';

    switch (error.code) {
        case error.PERMISSION_DENIED:
            errorMessage = "

We cannot find your location. Please make sure location services is enabled then reload the page and click allow when prompted.
"
            break;
        case error.POSITION_UNAVAILABLE:
            errorMessage = "

Location information is unavailable. Please try again later.
"
            break;
        case error.TIMEOUT:
            errorMessage = "

The request to get your location timed out. Please try again later.
"
            break;
        case error.UNKNOWN_ERROR:
            errorMessage = "

An unknown error occurred.
"
            break;
    }

    //do something besides logging this to inform your user what has gone wrong
    console.log(errorMessage);
}

//function to call initially when you want to query lat / long on the device, it calls successCallback on success, errorCallback on error
function getGeoDataFromDevice() {
    //if geo is available, get the current position if the maximum age of the last retrieved position is older than 10 seconds
    if (navigator.geolocation) {
        //high accuracy gets a precise location, but utilizes more battery on a device. We're not doing this excessively, so you should be good to go.
        //Maximum age is "how old can this data be, before we need something fresh" (10 seconds)
        //Timeout is how long should we try to get this information before giving up (20 seconds)
        navigator.geolocation.getCurrentPosition(successCallback, errorCallback, { enableHighAccuracy: true, maximumAge: 10000, timeout: 20000 });
    } else {
        //do something with this besides logging to inform the user their device is unsupported
        console.log('

Your device does not support location detection.
');
    }
}

origin - http://www.pipiscrew.com/?p=3449 easy-native-html5-geolocation

This post is licensed under CC BY 4.0 by the author.
Contents

Trending Tags