CHECK POINT ๐Ÿ‘โ€๐Ÿ—จ

  • navigator.geolocation.getCurrentPosition
  • JSON.stringify

PROCESS

  1. init()
  2. localstorage check
    2-1. null
    askForCoords()
    2-2. not null
    getWeather()
  3. getCurrentPosition(succes, error)
    3-1. succes
    handleGeoSucces() -> saveCoords()
    3-2. error
    handleGeoError()
const API_KEY = "24cc11af809bd59bd471045ca27f0625";
const COORDS = 'coords';

function saveCoords(coordsObj){
    localStorage.setItem(COORDS, JSON.stringify(coordsObj));
}

function handleGeoSucces(position){
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;
    const coordsObj = {
        latitude,
        longitude
    };
    saveCoords(coordsObj);
}

function handleGeoError(position){
    console.log('nonono');
}

function askForCoords(){
    navigator.geolocation.getCurrentPosition(handleGeoSucces, handleGeoError);
}

function loadCoords(){
    const loadedCoords = localStorage.getItem(COORDS);
    if (loadedCoords === null){
        // ํ˜ธ์ถœ
        askForCoords();
    }else{
        // getWeather
    }
}

function init(){
    loadCoords();
}

init();