Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $(document).ready(function(){
- function getPosition() {
- if (navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(getCity);
- } else {
- alert("Geolocation is not supported by this browser.");
- }
- }
- function getCity(position) {
- var url = "https://maps.googleapis.com/maps/api/geocode/json? latlng=" + position.coords.latitude + "," + position.coords.longitude;
- var city,
- country;
- $.getJSON(url, function(response) {
- city = response.results[0].address_components[2].short_name;
- country = response.results[0].address_components[5].short_name;
- $('.yourLocationGoesHere').attr('value', city + ", " + country);
- });
- // Get weather by your location
- $.getJSON("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&APPID=7f5806c8f3fd28b03e2d6580a50732d6", function (data) {
- var celsius = Math.round(data.main.temp - 273.15);
- var fahrenheit = Math.round(9/5 * (data.main.temp - 273) + 32);
- var iconCode = data.weather[0].icon;
- var iconUrl = "http://openweathermap.org/img/w/" + iconCode + ".png";
- $(".icon").html("<img src='" + iconUrl + "'>");
- });
- $(':radio').change(function(){
- // "this" will be the checked radio element
- if (this.id === 'celsius'){
- $(".showDegree").html(celsius + "°C");
- }else{
- $(".showDegree").html(fahrenheit + "°F");
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement