JavaScript - The Complete Guide 2022

Section 24: Time to Practice! - Let's build a Webpage

olivia_yj 2022. 11. 7. 19:15

The goals

๐Ÿ’ช๐ŸปPractice What We've learned!

 

 

Google Maps SDK

<script
      src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCueuEMBmaAK6XUl6pfsL0J5NTF6HwpjtY"
      defer
    ></script>
export class Map {
  constructor(coords) {
    // this.coordinates = coords;
    this.render(coords);
  }

  render(coordinates) {
    if (!google) {
      alert('Could not load maps library - please try again later!');
      return;
    }

    const map = new google.maps.Map(document.getElementById('map'), {
      center:coordinates,
      zoom: 16
    });

    new google.maps.Marker({
      position: coordinates,
      map: map
    });
  }
}
import { Map } from './UI/Map';

class LoadedPlace {
  constructor(coordinates, address) {
    new Map(coordinates);
    const headerTitleEl = document.querySelector('header h1');
    headerTitleEl.textContent = address;
  }
}

const url = new URL(location.href);
const queryParams = url.searchParams;
const coords = {
  lat: parseFloat(queryParams.get('lat')),
  lng: +queryParams.get('lng')
};
const address = queryParams.get('address');
new LoadedPlace(coords, address);

์ดˆ๊ณผํ•˜์ง€ ์•Š์„ ๋„‰๋„‰ํ•œ ๋ฌด๋ฃŒ ๋“ฑ๊ธ‰์„ ๋ฐ›์•˜๋”๋ผ๋„ Google ์ง€๋„๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ถˆํ–‰ํžˆ๋„ ์‹ ์šฉ ์นด๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์‹ ์šฉ ์นด๋“œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ๋Œ€์•ˆ์œผ๋กœ OpenLayers๋ฅผ ์“ฐ๋Š” ๊ฒƒ๋„ ์ƒ๊ฐํ•ด ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค(์—ฌ๊ธฐ์—์„œ ์ง€๋„๋ฅผ ๋ Œ๋”๋งํ•˜๋Š” ๋ฐฉ๋ฒ•: https://openlayers.org/en/latest/doc/quickstart.html).

์ €ํฌ์˜ ๊ตฌ์ฒด์ ์ธ ์˜ˆ์‹œ์—์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ง€๋„๋ฅผ ๋ Œ๋”๋งํ•ฉ๋‹ˆ๋‹ค:

HTML ํŒŒ์ผ์— ๋‹ค์Œ์˜ ์ฝ”๋“œ๋ฅผ ํฌํ•จ์‹œ์ผœ์ฃผ์„ธ์š”.

<link rel="stylesheet" 
	href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/css/ol.css" 
    type="text/css">
<script 
	src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.1.1/build/ol.js">
</script>

Map.js์—์„œ ๋‹ค์Œ์˜ JS ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

document.getElementById('map').innerHTML = ''; // clear the <p> in the <div id="map">
 
const map = new ol.Map({
  target: 'map',
  layers: [
    new ol.layer.Tile({
      source: new ol.source.OSM()
    })
  ],
  view: new ol.View({
    center: ol.proj.fromLonLat([coordinates.lng, coordinates.lat]),
    zoom: 16
  })
});

OpenLayers ๋ฌธ์„œ๋ฅผ ๋‘˜๋Ÿฌ๋ณด๋ฉด์„œ ๋‹ค์–‘ํ•œ ํ•ญ๋ชฉ์„ ๋ Œ๋”๋งํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‹ค์Œ ๊ฐ•์˜์—์„œ๋Š” Google API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ขŒํ‘œ ์Œ์„ ์ฃผ์†Œ๋กœ ๋˜๋Š” ๊ทธ ๋ฐ˜๋Œ€๋กœ ๋ณ€ํ™˜ํ•ด๋ณผ ๊ฒ๋‹ˆ๋‹ค. ์ด๋ฅผ ์œ„ํ•ด Google API๋„ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ์‹ ์šฉ ์นด๋“œ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

์•ˆํƒ€๊น๊ฒŒ๋„ ์ €๋Š” ์‹ ์šฉ์นด๋“œ๊ฐ€ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋Œ€์•ˆ์„ ์•Œ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ๋กœ์„œ๋Š” ์ €ํฌ๊ฐ€ ์ถ”๊ฐ€ํ•  ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜์—์„œ ์ผ๋ถ€ ๋”๋ฏธ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒƒ์ด ํ•ด๊ฒฐ์ฑ…์ด ๋  ๊ฒ๋‹ˆ๋‹ค.

  export async function getAddressFromCoords(coords) {
    return '6th Avenue'; // return any dummy address you want
}
 
  export async function getCoordsFromAddress(address) {
    return {lat: 47.01, lng: 33.55}; // return any dummy coordinates you want
}

 

How to change user's input as location point

async findAddressHandler(event) {
    event.preventDefault();
    const address = event.target.querySelector('input').value;
    if (!address || address.trim().length === 0) {
      alert('Invalid address entered - please try again!');
      return;
    }
    const modal = new Modal(
      'loading-modal-content',
      'Loading location - please wait!'
    );
    modal.show();
    try {
      const coordinates = await getCoordsFromAddress(address);
      this.selectPlace(coordinates, address);
    } catch (err) {
      alert(err.message);
    }
    modal.hide();
  }
}

Clipboard API

class PlaceFinder {
  constructor() {
    const addressForm = document.querySelector('form');
    const locateUserBtn = document.getElementById('locate-btn');
    this.shareBtn = document.getElementById('share-btn');

    locateUserBtn.addEventListener('click', this.locateUserHandler.bind(this));
    this.shareBtn.addEventListener('click', this.sharePlaceHandler);
    addressForm.addEventListener('submit', this.findAddressHandler.bind(this));
  }

  sharePlaceHandler() {
    const sharedLinkInputElement = document.getElementById('share-link');
    if (!navigator.clipboard) {
      sharedLinkInputElement.select();
      return;
    }

    navigator.clipboard.writeText(sharedLinkInputElement.value)
      .then(() => {
        alert('Copied into clipboard!');
      })
      .catch(err => {
        console.log(err);
        sharedLinkInputElement.select();
      });
  }

 

URL.searchParams

The searchParams readonly property of the URL interface returns a URLSearchParams object allowing access to the GET decoded query arguments contained in the URL.

const queryParams = url.searchParams;


Stored as key-value paris in constance queryParams.

 

 

 

 

 

 

 

 

Sources

https://developers.google.com/maps

 

Google Maps Platform  |  Google Developers

Google Maps Platform ์„ค๋ช…

developers.google.com

https://developers.google.com/maps/documentation/javascript/geolocation

 

์œ„์น˜์ •๋ณด: ์ง€๋„์— ์‚ฌ์šฉ์ž ๋˜๋Š” ๊ธฐ๊ธฐ ์œ„์น˜ ํ‘œ์‹œ  |  Maps JavaScript API  |  Google Developers

์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Switch to English ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ ์œ„์น˜์ •๋ณด: ์ง€๋„์— ์‚ฌ์šฉ์ž ๋˜๋Š” ๊ธฐ๊ธฐ ์œ„์น˜ ํ‘œ์‹œ ์ปฌ๋ ‰์…˜์„ ์‚ฌ์šฉํ•ด ์ •๋ฆฌํ•˜๊ธฐ ๋‚ด ํ™˜๊ฒฝ์„ค์ •์„ ๊ธฐ์ค€์œผ๋กœ ์ฝ˜ํ…์ธ ๋ฅผ ์ €

developers.google.com

https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API

 

Clipboard API - Web APIs | MDN

The Clipboard API provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard.

developer.mozilla.org

https://developer.mozilla.org/en-US/docs/Web/API/URL/searchParams

 

URL.searchParams - Web APIs | MDN

The searchParams readonly property of the URL interface returns a URLSearchParams object allowing access to the GET decoded query arguments contained in the URL.

developer.mozilla.org

https://ko.javascript.info/url

 

URL objects

 

ko.javascript.info