Javascript

[Javascript] 지도 API 사용하기

유호야 2021. 8. 12. 10:05
반응형

간단한 웹사이트를 만들고 있는데

지도 기능을 넣고 싶어서 찾아보았다.

복잡해보이지만 생각보다 간단한 것!

 

Adding a Google Map with a Marker to Your Website

 

<!DOCTYPE html>
<html>
  <head>
    <title>Add Map</title>

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script src="./index.js"></script>
  <style>
      #map {
        height: 400px;
        /* The height is 400 pixels */
        width: 100%;
        /* The width is the width of the web page */
    }
  </style>
  </head>
  <body>
    <h3>My Google Maps Demo</h3>
    <!--The div element for the map -->
    <div id="map"></div>

    <script>
        // Initialize and add the map
        function initMap() {
          // The location of Uluru
          const uluru = { lat: -25.344, lng: 131.036 };
          // The map, centered at Uluru
          const map = new google.maps.Map(document.getElementById("map"), {
            zoom: 4,
            center: uluru,
          });
          // The marker, positioned at Uluru
          const marker = new google.maps.Marker({
            position: uluru,
            map: map,
          });
        }
      </script>
    <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
    <script
      src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_CODE&callback=initMap&libraries=&v=weekly"
      async
    ></script>
  </body>
</html>
반응형