Geolocation funktioniert 🙂
// ABFRAGE VON GEOLOCATION
if (navigator.geolocation) {
jQuery(‘div#weather’).html(‘
Geolocation funktioniert 🙂
‘);
function position(currentPosition) {
var coords = currentPosition.coords;
var lat = coords.latitude;
var lng = coords.longitude;
weatherApi(lat, lng);
};
} else {
jQuery(‘div#weather’).html(‘
Geolocation funktioniert nicht 🙁
‘);
var lat = ‘52.52’;
var lng = ‘13.39’;
weatherApi(lat, lng);
}
navigator.geolocation.getCurrentPosition(position);
// FUNCTION
function weatherApi (newPositionLat = null , newPositionLng = null ) {
// AUSLESEN VON LAT UND LNG
if ( newPositionLat == null && newPositionLng == null ) {
var newPositionLat = ‘52.5200066’;
var newPositionLng = ‘13.404954’;
}
// API URL
var apiUrl = ‘https://api.openweathermap.org/data/2.5/weather?lat=’ + newPositionLat + ‘&lon=’ + newPositionLng + ‘&units=metric&appID=d658a7b5c33b3fe424f9dfc8f4413aa0’;
// WETTER API
jQuery.ajax ({
url: apiUrl,
type: ‘GET’,
dataType: ‘jsonp’,
success: function(data) {
// KOORDINATEN
var coordLat = data.coord.lat;
var coordLng = data.coord.lon;
// WETTER
var weatherId = data.weather[0].id;
var weatherMain = data.weather[0].main;
var weatherDesc = data.weather[0].description;
var weatherIcon = ‘‘;
var weatherBg = data.weather[0].icon;
// EXAKT
if ( weatherBg == 800 ) {
// klares wetter
jQuery(‘div#weatherBg’).css(‘background-color’, ‘yellow’);
} else if ( weatherBg == 951 ) {
// ruhe
jQuery(‘div#weatherBg’).css(‘background-color’, ‘blue’);
} else if ( weatherBg == 952 ) {
// leichte-briese
jQuery(‘div#weatherBg’).css(‘background-color’, ‘AliceBlue ‘);
} else if ( weatherBg == 954 ) {
// maessige-briese
jQuery(‘div#weatherBg’).css(‘background-color’, ‘AntiqueWhite ‘);
} else if ( weatherBg == 955 ) {
// frische-briese
jQuery(‘div#weatherBg’).css(‘background-color’, ‘Aqua’);
} else if ( weatherBg == 956 ) {
// starke-briese
jQuery(‘div#weatherBg’).css(‘background-color’, ‘Aquamarine ‘);
} else if ( weatherBg == 957 ) {
// starker-wind
jQuery(‘div#weatherBg’).css(‘background-color’, ‘Azure’);
} else if ( weatherBg == 958 ) {
// sturm
jQuery(‘div#weatherBg’).css(‘background-color’, ‘BlueViolet’);
} else if ( weatherBg == 959 ) {
// starker-sturm
jQuery(‘div#weatherBg’).css(‘background-color’, ‘Brown’);
} else if ( weatherBg == 959 ) {
// starker-sturm
jQuery(‘div#weatherBg’).css(‘background-color’, ‘BurlyWood’);
} else if ( weatherBg == 960 ) {
// sturm
jQuery(‘div#weatherBg’).css(‘background-color’, ‘CadetBlue’);
} else if ( weatherBg == 961 ) {
// heftiger-sturm
jQuery(‘div#weatherBg’).css(‘background-color’, ‘Chartreuse’);
} else if ( weatherBg == 962 ) {
// hurrikan
jQuery(‘div#weatherBg’).css(‘background-color’, ‘Coral’);
}
// RANGE
else if ( weatherBg >= 900 && weatherBg <= 950 ) { // extrem jQuery('div#weatherBg').css('background-color', 'DarkBlue'); } else if ( weatherBg >= 801 && weatherBg <= 899 ) { // wolkig jQuery('div#weatherBg').css('background-color', 'DarkGrey'); } else if ( weatherBg >= 701 && weatherBg <= 799 ) { // atmosphäre jQuery('div#weatherBg').css('background-color', 'DarkTurquoise'); } else if ( weatherBg >= 600 && weatherBg <= 700 ) { // schnee jQuery('div#weatherBg').css('background-color', 'GhostWhite'); } else if ( weatherBg >= 500 && weatherBg <= 599 ) { // regen jQuery('div#weatherBg').css('background-color', 'LightSteelBlue'); } else if ( weatherBg >= 300 && weatherBg <= 499 ) { // nieselregen jQuery('div#weatherBg').css('background-color', 'LightSkyBlue'); } else if ( weatherBg >= 200 && weatherBg <= 299 ) { // gewitter jQuery('div#weatherBg').css('background-color', 'MidnightBlue'); } // BASE var baseData = data.base; // TEMP var mainTemp = data.main.temp; var mainPressure = data.main.pressure; var mainHumidity = data.main.humidity; var mainTempMin = data.main.temp_min; var mainTempMax = data.main.temp_max; // VISIBILITY var visibility = data.visibility; // WIND var windSpeed = data.wind.speed; var windDeg = data.wind.deg; // CLOUDS var clouds = data.clouds.all; // DT var dt = data.dt; // SYS var sysType = data.sys.type; var sysId = data.sys.id; var sysMessage = data.sys.message; var sysCountry = data.sys.country; var sysSunrise = data.sys.sunrise; var sysSunset = data.sys.sunset; // ID var id = data.id; // NAME var name = data.name; // COD var cod = data.cod; var coor = '
Koordinaten
- Lat: ‘ + coordLat + ‘
- Lng: ‘ + coordLng + ‘
‘;
var weather = ‘
Wetter
- ID: ‘ + weatherId + ‘
- Main: ‘ + weatherMain + ‘
- Desc: ‘ + weatherDesc + ‘
- icon: ‘ + weatherIcon + ‘
‘;
var base = ‘
Base
- Base: ‘ + baseData + ‘
‘;
var temperatur = ‘
Temperatur
- Main Temp: ‘ + mainTemp + ‘
- Druck: ‘ + mainPressure + ‘
- Feuchtigkeit: ‘ + mainHumidity + ‘
- min. Temp.: ‘ + mainTempMin + ‘
- Max. Temp.: ‘ + mainTempMax + ‘
‘;
var visibility = ‘
Sichtweite
- Sichtweite: ‘ + visibility + ‘
‘;
var wind = ‘
Wind
- Wind Speed: ‘ + windSpeed + ‘
- Wind Deg: ‘ + windDeg + ‘
‘;
var clouds = ‘
Wolken
- Wolken: ‘ + clouds + ‘
‘;
var dt = ‘
DT
- Dt: ‘ + dt + ‘
‘;
var system = ‘
Base
- Typ: ‘ + sysType + ‘
- ID: ‘ + sysId + ‘
- Message: ‘ + sysMessage + ‘
- Land: ‘ + sysCountry + ‘
- Sunrise: ‘ + sysSunrise + ‘
- Sunset: ‘ + sysSunset + ‘
‘;
var id = ‘
ID
- ID: ‘ + id + ‘
‘;
var name = ‘
Name
- Name: ‘ + name + ‘
‘;
var cod = ‘
Cod
- Cod: ‘ + cod + ‘
‘;
// OUTPUT
jQuery(‘div#weather’).html( coor + weather + base + temperatur + visibility + wind + clouds + dt + system + id + name + cod );
console.log(data);
}
});
}