-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeo.js
More file actions
73 lines (64 loc) · 2.72 KB
/
geo.js
File metadata and controls
73 lines (64 loc) · 2.72 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
/** NOTE: uses jQuery for quick & easy DOM manipulation **/
function getLocation(){
var msg;
/**
first, test for feature support
**/
if('geolocation' in navigator){
// geolocation is supported :)
requestLocation();
}else{
// no geolocation :(
msg = "Sorry, looks like your browser doesn't support geolocation";
outputResult(msg); // output error message
$('.pure-button').removeClass('pure-button-primary').addClass('pure-button-success'); // change button style
}
/***
requestLocation() returns a message, either the users coordinates, or an error message
**/
function requestLocation(){
/**
getCurrentPosition() below accepts 3 arguments:
a success callback (required), an error callback (optional), and a set of options (optional)
**/
var options = {
// enableHighAccuracy = should the device take extra time or power to return a really accurate result, or should it give you the quick (but less accurate) answer?
enableHighAccuracy: false,
// timeout = how long does the device have, in milliseconds to return a result?
timeout: 5000,
// maximumAge = maximum age for a possible previously-cached position. 0 = must return the current position, not a prior cached position
maximumAge: 0
};
// call getCurrentPosition()
navigator.geolocation.getCurrentPosition(success, error, options);
// upon success, do this
function success(pos){
// get longitude and latitude from the position object passed in
var lng = pos.coords.longitude;
var lat = pos.coords.latitude;
// and presto, we have the device's location!
msg = 'You appear to be at longitude: ' + lng + ' and latitude: ' + lat + '<img src="https://maps.googleapis.com/maps/api/staticmap?zoom=15&size=300x300&maptype=roadmap&markers=color:red%7Clabel:A%7C' + lat + ',' + lng+ '&sensor=false">';
outputResult(msg); // output message
$('.pure-button').removeClass('pure-button-primary').addClass('pure-button-success'); // change button style
}
// upon error, do this
function error(err){
// return the error message
msg = 'Error: ' + err + ' :(';
outputResult(msg); // output button
$('.pure-button').removeClass('pure-button-primary').addClass('pure-button-error'); // change button style
}
} // end requestLocation();
/***
outputResult() inserts msg into the DOM
**/
function outputResult(msg){
$('.result').addClass('result').html(msg);
}
} // end getLocation()
// attach getLocation() to button click
$('.pure-button').on('click', function(){
// show spinner while getlocation() does its thing
$('.result').html('<i class="fa fa-spinner fa-spin"></i>');
getLocation();
});