3. Google API - jmini1234/tripmate GitHub Wiki

1. Google Map API

1.1 Google Map ์ถœ๋ ฅ

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 : ์œ„์น˜๋ฅผ ์œ„๋„์™€ ๊ฒฝ๋„๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.

1.2 ํ˜„์žฌ ์œ„์น˜ ์ขŒํ‘œ ๋ฐ˜ํ™˜

  // 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 ์ฐธ์กฐ) ํ•จ์ˆ˜๋ฅผ ๋ถˆ๋Ÿฌ์™€์„œ ์‹ค์ œ ์ฃผ์†Œ ๊ฐ’๊นŒ์ง€ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

1.3 ์ขŒํ‘œ๋ฅผ ์ฃผ์†Œ๋กœ ๋ณ€๊ฒฝ (reverse Geocoding)

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๋ฅผ ํ‘œ์‹œํ•œ๋‹ค.


2. Google Map - Firebase ์—ฐ๋™

2.1 firebase ์—ฐ๋™

  <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๋ฅผ ํ†ตํ•ด์„œ ๋„ฃ์–ด์ค€๋‹ค.

2.2 Google Map ์—ฐ๋™

    <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๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

โš ๏ธ **GitHub.com Fallback** โš ๏ธ