-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgeolocation-example.js
58 lines (52 loc) · 1.54 KB
/
geolocation-example.js
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
$( document ).ready( function() {
/*
* Does the browser support geolocation?
* Wrap any geolocation link/feature in this function
*/
function supportGeolocation() {
if( "geolocation" in navigator ) {
console.log( 'Browser supports geoLocation ' );
return true;
} else {
console.log( 'Browser does no support geoLocation ' );
return false;
}
}
/*
* Returns on successfully getting location
*/
function navigatorSuccess( position ) {
loadWeather( position.coords.latitude + ',' + position.coords.longitude );
}
/*
* Returns on error getting location
* See all Error codes at: https://developer.mozilla.org/en-US/docs/Web/API/PositionError
*/
function navigatorFail( error ) {
if( error.code == 2 ) {
console.log( 'Browser failed to provide location.' );
} else {
//timeout or permission error
console.log( 'Error getting location.' );
}
}
/*
* Load weather based on location data provided
*/
function loadWeather( location ) {
$.simplerWeather( {
location: location,
apikey: 'YOUR_API_KEY',
success: function( weather ) {
geoLocationWeather = '<h2> <i class="icon ' + weather.icon +
'"></i> ' + Math.round( weather.temp ) + '°';
geoLocationWeather +=
'<p> Currently: </span>' + weather.currently + '</p>';
$( "#local_weather" ).html( geoLocationWeather );
},
error: function( error ) {
$( "#local_weather" ).html( '<p>' + error + '</p>' );
}
} );
}
} );