iT邦幫忙

2024 iThome 鐵人賽

DAY 2
0
JavaScript

PM說: RD大大,這個功能要怎麼寫啊?系列 第 2

PM說: 要怎麼寫一個免費的網頁地圖?

  • 分享至 

  • xImage
  •  

前言

主菜會需要

  1. 廚具
  2. 食材

同理要寫一個線上地圖有2個不可或缺的東西
地圖套件 & 圖資

以下我會使用來製作

  • leaflet 免費開源的地圖套件
  • OSM 免費的地圖資訊

程式碼

<!DOCTYPE html>
<html lang="zh-TW">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>leaflet + OSM 製作免費線上地圖</title>
    <link
      rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css"
    />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.js"></script>
    <style>
      body {
        background-color: black;
        padding: 0;
        margin: 0;
      }
      .wrapper {
        width: 99dvw;
        height: 99dvh;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      #map {
        height: 100%;
        width: 100%;
      }
    </style>
  </head>
  <body>
    <!-- 給地圖一個高 -->
    <div class="wrapper">
      <div id="map"></div>
    </div>
    <script>
      const myMap = {
        map: null,
        init() {
          this.map = L.map("map").setView([25.033, 121.5654], 14);
          //圖層
          L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
            attribution: "© OpenStreetMap contributors",
          }).addTo(this.map);
        },
        main() {
          // 點
          const taipei101 = L.latLng(25.0339, 121.5646);
          const daAnPark = L.latLng(25.0327467, 121.5343);

          const lineConfig = {
            color: "red",
            weight: 3,
          };
          // 生成一條線
          const polyline = L.polyline([taipei101, daAnPark], lineConfig)
            .addTo(this.map)
            .bindPopup("大安森林公園~台北101");

          // 生成地標
          L.marker(taipei101).addTo(this.map).bindPopup("台北101");
          L.marker(daAnPark).addTo(this.map).bindPopup("大安森林公園");
        },
      };

      myMap.init();
      myMap.main();
    </script>
  </body>
</html>

語法

  1. L.map()
//https://leafletjs.com/reference.html#map
//綁定地圖並生成
L.map("map")
  1. L.tileLayer()
//https://leafletjs.com/reference.html#tilelayer

//第一個參數為圖資網址  s: subdomains; z: zoom; x,y: 經緯度 
//第二個參數是設定檔
//給予圖層(圖資)
          L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
            attribution: "© OpenStreetMap contributors",
          }).addTo(this.map);
  1. L.latLng()
//https://leafletjs.com/reference.html#latlngbounds
//透過經緯度生成一個點(還沒加到地圖上~)
const taipei101 = L.latLng(25.0339, 121.5646);
  1. L.polyline()
//https://leafletjs.com/reference.html#polyline
//產生一條線
//第一個參數是兩個點組成的[]
//第二個參數是線的設定檔
          const lineConfig = {
            color: "red",
            weight: 3,
          };
          const polyline = L.polyline([taipei101, daAnPark], lineConfig)
  1. L.marker()
//https://leafletjs.com/reference.html#marker
//透過點產生一個地標
L.marker(taipei101)
  1. .addTo()
//將生成的物件掛到地圖上顯示
//生成的地標 & 線 都具有的方法
//return this
  1. .bindPopup()
//https://leafletjs.com/reference.html#layer-bindpopup
//在地圖上綁定對話框

結語

今天我們學會了

  1. 生成免費地圖
  2. 生成地標
  3. 生成線
  4. 生成對話框

下一篇來做出一個公車路線圖(地標&線)~/images/emoticon/emoticon42.gif


上一篇
PM說: 網頁要怎麼客製化右鍵選單(menu)?
下一篇
PM說: 要怎麼寫出公車路線圖(網頁地圖)?
系列文
PM說: RD大大,這個功能要怎麼寫啊?20
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言