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