Skip to content

Instantly share code, notes, and snippets.

@im-noob
Created April 1, 2021 12:05
Show Gist options
  • Save im-noob/c15e057c1c11d0fbb5cf299e16f4d0ec to your computer and use it in GitHub Desktop.
Save im-noob/c15e057c1c11d0fbb5cf299e16f4d0ec to your computer and use it in GitHub Desktop.
Weather Java Script By Lat Long
<script>
$(function () {
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(fetchPosition);
} else {
$('.wether-section').hide();
console.log("Geolocation is not supported by this browser.");
}
}
navigator.geolocation.watchPosition(
function (position) {
console.log("Location Accessed");
},
function (error) {
if (error.code === error.PERMISSION_DENIED) {
$('.wether-section').hide();
console.log("Geolocation has been blocked by this browser.");
}
}
);
function position() {
console.log('position');
}
function fetchPosition(position) {
let lat = position.coords.latitude;
let long = position.coords.longitude;
updateWeather(lat, long)
}
function updateWeather(lat, long) {
$.ajax({
method: 'GET',
url: "http://api.weatherapi.com/v1/current.json",
data: {
key: "{{env('WEATHER_API_KEY')}}",
q: lat + ',' + long,
},
success: function (response) {
let location_name = response.location.name;
let current_temp_c = response.current.temp_c;
let current_condition_text = response.current.condition.text;
let current_condition_code = response.current.condition.code;
let current_condition_icon = response.current.condition.icon;
$('.wether-section').html(
location_name + ' · ' +
current_temp_c + '°C ' +
'<img style="width: 40px" src="' + current_condition_icon + '">'
);
}
});
}
getLocation();
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment