3. Google API - jmini1234/tripmate GitHub Wiki
var map, infowindow, pos, geocoder;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 6
});
infowindow = new google.maps.InfoWindow;
geocoder = new google.maps.Geocoder;
map ๋ณ์๋ html tag ์ค id ๊ฐ์ด map์ธ ํ๋ฉด์ ๋ฐ์์ ์ ์ธํ๋ค.
google map api๋ฅผ ํตํด infowindow์ geocoder ๋ณ์๋ฅผ ์ ์ธํ๋ค.
- infowindow : ํ์ฌ ์ ๋ณด์ ๊ด๋ จ๋ window ํ๋ฉด์ด๋ค.
- geocoder : ์์น๋ฅผ ์๋์ ๊ฒฝ๋๋ก ๋ํ๋ธ๋ค.
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infowindow.setPosition(pos);
infowindow.setContent('Location found.');
infowindow.open(map);
map.setCenter(pos);
geocodeLatLng(geocoder, map, infowindow);
}, function() {
handleLocationError(true, infowindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infowindow, map.getCenter());
}
}
initMap() : ๋ด ์์น๋ฅผ ๋ฐํํ๋ Map์ ๋ถ๋ฌ์จ๋ค.
navigator.geolocation.getCurrentPosition ๋ฉ์๋๋ก position ๋ณ์๋ฅผ ํตํด pos ์ ํ์ฌ ์๋์ ๊ฒฝ๋ ๊ฐ์ ์ ์ฅํ๋ค.
infowindow.setPosition(pos) ๋ก ํ์ฌ ์์น์ ๊ด๋ จ๋ ์ง๋๋ฅผ ํ๋ฉด์ ๋ณด์ฌ์ฃผ๊ณ map์ open ํด์ ํ๋ฉด์ ๋์ด๋ค.
์ด ๊ณณ์์ geocodeLatLng(geocoder, map, infowindow) (1.3 ์ฐธ์กฐ) ํจ์๋ฅผ ๋ถ๋ฌ์์ ์ค์ ์ฃผ์ ๊ฐ๊น์ง ๋ฐํํ๋ค.
var address ;
function geocodeLatLng(geocoder, map, infowindow) {
geocoder.geocode({'location': pos}, function(results, status) {
if (status === 'OK') {
if (results[0]) {
map.setZoom(11);
var marker = new google.maps.Marker({
position: pos,
map: map
});
infowindow.setContent(results[0].formatted_address);
address=results[0].formatted_address;
infowindow.open(map, marker);
var lat = document.getElementById("location");
lat.innerHTML = address;
} else {
window.alert('No results found');
}
} else {
window.alert('Geocoder failed due to: ' + status);
}
});
}
ํ์ฌ ์์น๋ก ๋ฐ์์จ ์๋ ๊ฒฝ๋ ๊ฐ์ ์ฃผ์ ๊ฐ์ผ๋ก ๋ณ๊ฒฝํ๋ geocodeLatLng ํจ์
Map์ pos(ํ์ฌ ์์น)๋ก Zoom์ํ๊ณ marker ํ์ํด ๊ทธ ๊ณณ์ setContent๋ก ์ฃผ์๋ฅผ ํ๋ฉด์ ์ถ๋ ฅํ๋ค.
pos์ ์ ์ฅ๋์ด ์๋ ์๋ ๊ฒฝ๋ ๊ฐ์ result[0]์ ๋ฃ๊ณ formatted_address๋ฅผ ์ฌ์ฉํด์ adress ๋ณ์์ ์ค์ ์ฃผ์๋ฅผ ์ ์ฅํ๋ค.
id๊ฐ location์ธ ํ๋ฉด์ ์ฐพ์ lat์ผ๋ก ์ ์ํ ๋ค์ ๊ทธ ํ๋ฉด์ ํ์ฌ ์ฃผ์๋ฅผ ๋ํ๋ด๊ธฐ์ํด innerHTML ์ ์ฌ์ฉํด์ address๋ฅผ ํ์ํ๋ค.
<script src="/__/firebase/5.5.8/firebase-app.js"></script>
<script src="/__/firebase/5.5.8/firebase-auth.js"></script>
<script src="/__/firebase/5.5.8/firebase-database.js"></script>
<script src="/__/firebase/5.5.8/firebase-storage.js"></script>
<script src="/__/firebase/5.5.8/firebase-messaging.js"></script>
<script src="/__/firebase/init.js"></script>
<script src="https://cdn.firebase.com/js/client/2.3.2/firebase.js"></script>
์ฌ์ฉํ๋ ํ๋ก์ ํธ์ database๊ฐ ์ ์ฅ๋ firebase ์ ์ฐ๊ฒฐํ๊ธฐ ์ํด firebase์ source๋ฅผ script๋ฅผ ํตํด์ ๋ฃ์ด์ค๋ค.
<script src="scripts/map.js"></script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=[Google_API_KEY]&callback=initMap">
</script>
firebase-Google Map ์ฐ๋์ ์ํด firebase.js๋ฅผ ์ฐธ์กฐํ๋ script์ google api key๋ฅผ ๋ฃ์ script๋ฅผ ์ถ๊ฐํ๋ค.