Featured image of post OpenStreetMap-Leaflet-網頁地圖視覺化

OpenStreetMap-Leaflet-網頁地圖視覺化

簡易製作網頁地圖並顯示定位

最後修改時間:

製作網頁地圖

跟著網路上大大的教學文章,成功實作網頁上放入地圖!

參考資料

https://leafletjs.com

OSM + Leaflet 學習筆記 系列文

Demo:

https://github.com/wastu01/leaflet

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Original tutorial & code : 
// https://superplug.in/supermarquee/demo/7
// https://www.letswrite.tw/leaflet-osm-basic/

document.addEventListener('DOMContentLoaded', () => {  



  let center = [24.7824571,120.9957021];
  // 跟使用者要位置
  function successGPS(position) {
      const lat = position.coords.latitude;
      const lng = position.coords.longitude;
      center = [lat, lng];
      // 接著寫確認了座標後要執行的事
      triggerLeaflet()
    };
    function errorGPS() {
      window.alert('無法判斷您的所在位置,無法使用此功能。預設地點將為 NCHC');
      // 接著寫使用者「封鎖」位置資訊請求後要執行的事
      triggerLeaflet()
    }
  
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(successGPS, errorGPS);
      console.log(successGPS);

    } else {
      window.alert('您的裝置不具備GPS,無法使用此功能');
      // 接著寫使用者裝置不支援位置資訊時要執行的事
      triggerLeaflet();
  
  
    }
    function triggerLeaflet() {
  
      let zoom = 16; // 0 - 18
      let center = [24.7825236,120.9957928]; // 中心點座標
      let map = L.map('map').setView(center,zoom);
    
        L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
            maxZoom: 18,
            attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
        }).addTo(map);
  
      const marker = L.marker(center, {
          title: 'aaaa', //  <a>  title 一樣
          opacity: 1.0
        }).addTo(map);
  
        marker.bindPopup('<b>Hello Guys !</b><br>I am in NCHC. <a target="_blank" title="另開視窗" href="https://www.google.com/maps/dir//No.%207,%20R&D%206th%20Rd,%20East%20District,%20Hsinchu%20City,%20300">click me to here</a>').openPopup();

        marker.bindTooltip("my tooltip text", {
          direction: 'bottom', // rightlefttopbottomcenterdefault: auto
          sticky: true, // true 跟著滑鼠移動。default: false
          permanent: false, // 是滑鼠移過才出現,還是一直出現
          opacity: 1.0
        }).openTooltip();
  
        const popup = L.popup();
  
        function onMapClick(e) {
          let lat = e.latlng.lat; // 緯度
          let lng = e.latlng.lng; // 經度
          popup
            .setLatLng(e.latlng)
            .setContent(`緯度:${lat}<br/>經度:${lng}`)
            .openOn(map);
        }
        map.on('click', onMapClick);
  
    }
    const mySuperMarquee = new SuperMarquee( document.getElementById( "supermarquee" ),{"content" : "國家科學及技術委員會財團法人國家實驗研究院國家高速網路與計算中心"});
    document.querySelector( ".demo-button-container" ).addEventListener( "click", e => {
        const { target } = e;
        if ( target.classList.contains( "btn-primary"  ) ) {
            const oldBtn = document.querySelector( ".btn-light" );
            oldBtn.classList.remove( "btn-light" );
            oldBtn.classList.add( "btn-primary" );
            target.classList.remove( "btn-primary" );
            target.classList.add( "btn-light" );
            mySuperMarquee.setScrollSpeed( target.innerHTML.toLowerCase() );
        }
    });
  

})

Photo by Rick

comments powered by Disqus
使用 Hugo 建立
主題 StackJimmy 設計
已產出:2865字,共 12篇文章