Then update the Code.gs file with:

function doGet() {  const html = HtmlService.createHtmlOutputFromFile(\\'Index\\')    .setTitle(\\'Map with Draggable Points\\')    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);  return html;}

Save, and then click Deploy, and publish as a web app. Then open the link for the new deployment and you should see Leaflet.js displaying a map on New York.

\\\"Building

Ok, that\\'s the regular map example using Leaflet. Now on to the CRS.Simple map type, which allows supplying a background image.

Update the HTML with this example from the Leaflet Tutorials.

  CRS Simple Example - Leaflet          

Here we are supplying an image of 1000 x 1000 pixels, then setting the center marker at 500, 500.

Click Save, then Deploy>Test Deployments, to see the new map type. You should now have a map with a background image and a marker plotted in the center.

\\\"Building

Initializing a Map with Data from Google Sheets

Next, we\\'ll use data from the sheet to populate a set of markers on the map.

First, add a function to the Code.gs file to get the marker locations:

function getPinData(){  const ss = SpreadsheetApp.getActiveSpreadsheet();  const sh = ss.getSheetByName(\\'map_pin\\');  const data = sh.getDataRange().getValues();  const json = arrayToJSON(data);  //Logger.log(json);  return json}function arrayToJSON(data=getPinData()){  const headers = data[0];  const rows = data.slice(1);  let jsonData = [];  for(row of rows){    const obj = {};    headers.forEach((h,i)=>obj[h] = row[i]);    jsonData.push(obj)  }  //Logger.log(jsonData)  return jsonData}

Here I\\'m returning the pins as JSON so they\\'re easier to work with in the HTML in the next section.

Now add a function to the HTML to loop over this JSON and create the map pins after the map has loaded.

// Add map pins from sheet data    google.script.run.withSuccessHandler(addMarkers).getPinData();    function addMarkers(mapPinData) {      mapPinData.forEach(pin => {        const marker = L.marker([pin.x, pin.y], {          draggable: true        }).addTo(map);        marker.bindPopup(`${pin.title}`).openPopup();        marker.on(\\'dragend\\', function(e) {          const latLng = e.target.getLatLng();          console.log(`Marker ${pin.title} moved to: ${latLng.lat}, ${latLng.lng}`);        });      });    }

Save, and then open the test deployment. You should now have markers generated from your sheet data!

\\\"Building

Each pin has a popup with the title from that row. The pins are draggable at this point, but we still need a function to save the new position.

Saving Marker Position When Dragged

To save the new position, we need two functions: one in the HTML to capture the event on the client side, and one to save the new position on the server side, in the Code.gs file.

Update the HTML with:

    function addMarkers(mapPinData) {      mapPinData.forEach(pin => {        const { id, title, x, y } = pin;        const marker = L.marker([x, y], {          draggable: true        }).addTo(map);        marker.bindPopup(`${title}`).openPopup();        marker.on(\\'dragend\\', function(e) {          const latLng = e.target.getLatLng();          console.log(`Marker ${title} moved to: ${latLng.lat}, ${latLng.lng}`);          saveMarkerPosition({ id, title, lat: latLng.lat, lng: latLng.lng });        });      });    }    function saveMarkerPosition({ id, title, lat, lng }) {      google.script.run.saveMarkerPosition({ id, title, lat, lng });    }

And then add a function to the Code.gs file to save the location:

function saveMarkerPosition({ id, lat, lng }) {  const ss = SpreadsheetApp.getActiveSpreadsheet();  const sh = ss.getSheetByName(\\'map_pin\\');  const data = sh.getDataRange().getValues();  for (let i = 1; i < data.length; i  ) {    if (data[i][0] === id) {  // ID column (index 0)      sh.getRange(i   1, 3).setValue(lat);  // latitude column      sh.getRange(i   1, 4).setValue(lng);  // longitude column      break;    }  }}

Save, and refresh the test deployment. You should now see the sheet update when a marker is dragged!

\\\"Building

Adding New Points

We can now move the existing points, but what about adding new ones? Again, we\\'ll need two functions, one in the HTML, and one in the Code.gs file.

First, add a function to the HTML to open a prompt when the user clicks an empty spot on the map, and pass the value to a server function.

    // Function to add a new pin    map.on(\\'click\\', function(e) {      const latLng = e.latlng;      const title = prompt(\\'Enter a title for the new pin:\\');      if (title) {        google.script.run.withSuccessHandler(function(id) {          addNewMarker({ id, title, lat: latLng.lat, lng: latLng.lng });        }).addNewPin({ title, lat: latLng.lat, lng: latLng.lng });      }    });    function addNewMarker({ id, title, lat, lng }) {      const marker = L.marker([lat, lng], {        draggable: true      }).addTo(map);      marker.bindPopup(`${title}`).openPopup();      marker.on(\\'dragend\\', function(e) {        const latLng = e.target.getLatLng();        saveMarkerPosition({ id, title, lat: latLng.lat, lng: latLng.lng });      });    }

Then add the function to the Code.gs to save the new row.

function addNewPin({ title, lat, lng }) {  const ss = SpreadsheetApp.getActiveSpreadsheet();  const sh = ss.getSheetByName(\\'map_pin\\');  // Check if there are any rows present, if not initialize ID  const lastRow = sh.getLastRow();  let newId = 1;  if (lastRow > 0) {    const lastId = sh.getRange(lastRow, 1).getValue();    newId = lastId   1;  }  sh.appendRow([newId, title, lat, lng]);  return newId;}

Save once more and refresh the test deployment. Now when you click an empty spot, you can enter a title and save a new marker!

\\\"Building

Deleting A Marker

Lastly, we should add a way to delete markers, giving us a full CRUD app in map view.

Update the add marker function to give the popup a delete button:

      const popupContent = `${title}
`; marker.bindPopup(popupContent).openPopup();

And then add a function for deleting from the client side:

// Function to delete a marker  function deleteMarker(id) {    const confirmed = confirm(\\'Are you sure you want to delete this marker?\\');    if (confirmed) {      google.script.run.withSuccessHandler(() => {        // Refresh the markers after deletion        google.script.run.withSuccessHandler(addMarkers).getPinData();      }).deleteMarker(id);    }  }

Then add the matching function to the Code.gs file:

function deleteMarker(id) {  const ss = SpreadsheetApp.getActiveSpreadsheet();  const sh = ss.getSheetByName(\\'map_pin\\');  const data = sh.getDataRange().getValues();  for (let i = 1; i < data.length; i  ) {    if (data[i][0] === id) {  // ID column (index 0)      sh.deleteRow(i   1);  // Delete the row      break;    }  }}

What\\'s Next?

There\\'s a ton more you could do from here, like adding other data points to each marker, dynamic background images, or other click and drag interactions. You could even make a game! Got an idea for a use case? Drop a comment below!

","image":"http://www.luping.net/uploads/20241011/1728615609670894b9b23dd.png","datePublished":"2024-11-07T01:20:46+08:00","dateModified":"2024-11-07T01:20:46+08:00","author":{"@type":"Person","name":"luping.net","url":"https://www.luping.net/articlelist/0_1.html"}}
」工欲善其事,必先利其器。「—孔子《論語.錄靈公》

使用 Google Apps 腳本和 Leaflet.js 建立互動式 XY 圖像圖

發佈於2024-11-07
瀏覽:901

Google Maps has a ton of features for plotting points on a map, but what if you want to plot points on an image? These XY Image Plot maps are commonly used for floor maps, job site inspections, and even games.

In this guide, I'll show you how to create an interactive map with draggable points using Leaflet.js and Google Apps Script. We'll cover everything from setting up the map to integrating data from Google Sheets, and deploying it as a web app.

This guide will cover:

  • Setting up Leaflet.js in a Google Apps Script HTML Service

  • Displaying Markers using data from Google Sheets

  • Updating Sheets row when a Marker is moved

  • Creating new Markers from the map and saving to Sheets

  • Deleting a marker from the web app

Setting up Leaflet.js in a Google Apps Script HTML Service

Leaflet.js is one of the most popular open-source mapping libraries. It's light-weight, easy to use, and had great documentation. They support a ton of different map types, including "CRS.Simple", or Coordinate Reference System, which allows you to supply a background image.

Google Sheets Set Up

Start out by creating a sheet named map_pin with the following structure:

id title x y
1 test1 10 30
2 test2 50 80

Then open Apps Script from the Extensions menu.

Creating HTML File

First, we'll start with the basic example from the Leaflet docs, just to get the library working. You can see the full example in their quick start guide, here.

Add a new HTML File named Index, and set the content to:



  Quick Start - Leaflet

Then update the Code.gs file with:

function doGet() {
  const html = HtmlService.createHtmlOutputFromFile('Index')
    .setTitle('Map with Draggable Points')
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL);
  return html;
}

Save, and then click Deploy, and publish as a web app. Then open the link for the new deployment and you should see Leaflet.js displaying a map on New York.

Building an Interactive XY Image Plot with Google Apps Script and Leaflet.js

Ok, that's the regular map example using Leaflet. Now on to the CRS.Simple map type, which allows supplying a background image.

Update the HTML with this example from the Leaflet Tutorials.



  CRS Simple Example - Leaflet

Here we are supplying an image of 1000 x 1000 pixels, then setting the center marker at 500, 500.

Click Save, then Deploy>Test Deployments, to see the new map type. You should now have a map with a background image and a marker plotted in the center.

Building an Interactive XY Image Plot with Google Apps Script and Leaflet.js

Initializing a Map with Data from Google Sheets

Next, we'll use data from the sheet to populate a set of markers on the map.

First, add a function to the Code.gs file to get the marker locations:

function getPinData(){
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName('map_pin');
  const data = sh.getDataRange().getValues();
  const json = arrayToJSON(data);
  //Logger.log(json);
  return json
}

function arrayToJSON(data=getPinData()){
  const headers = data[0];
  const rows = data.slice(1);
  let jsonData = [];
  for(row of rows){
    const obj = {};
    headers.forEach((h,i)=>obj[h] = row[i]);
    jsonData.push(obj)
  }
  //Logger.log(jsonData)
  return jsonData
}

Here I'm returning the pins as JSON so they're easier to work with in the HTML in the next section.

Now add a function to the HTML to loop over this JSON and create the map pins after the map has loaded.

// Add map pins from sheet data
    google.script.run.withSuccessHandler(addMarkers).getPinData();

    function addMarkers(mapPinData) {
      mapPinData.forEach(pin => {
        const marker = L.marker([pin.x, pin.y], {
          draggable: true
        }).addTo(map);

        marker.bindPopup(`${pin.title}`).openPopup();

        marker.on('dragend', function(e) {
          const latLng = e.target.getLatLng();
          console.log(`Marker ${pin.title} moved to: ${latLng.lat}, ${latLng.lng}`);
        });
      });
    }

Save, and then open the test deployment. You should now have markers generated from your sheet data!

Building an Interactive XY Image Plot with Google Apps Script and Leaflet.js

Each pin has a popup with the title from that row. The pins are draggable at this point, but we still need a function to save the new position.

Saving Marker Position When Dragged

To save the new position, we need two functions: one in the HTML to capture the event on the client side, and one to save the new position on the server side, in the Code.gs file.

Update the HTML with:

    function addMarkers(mapPinData) {
      mapPinData.forEach(pin => {
        const { id, title, x, y } = pin;
        const marker = L.marker([x, y], {
          draggable: true
        }).addTo(map);

        marker.bindPopup(`${title}`).openPopup();

        marker.on('dragend', function(e) {
          const latLng = e.target.getLatLng();
          console.log(`Marker ${title} moved to: ${latLng.lat}, ${latLng.lng}`);
          saveMarkerPosition({ id, title, lat: latLng.lat, lng: latLng.lng });
        });
      });
    }

    function saveMarkerPosition({ id, title, lat, lng }) {
      google.script.run.saveMarkerPosition({ id, title, lat, lng });
    }

And then add a function to the Code.gs file to save the location:

function saveMarkerPosition({ id, lat, lng }) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName('map_pin');
  const data = sh.getDataRange().getValues();

  for (let i = 1; i 



Save, and refresh the test deployment. You should now see the sheet update when a marker is dragged!

Building an Interactive XY Image Plot with Google Apps Script and Leaflet.js

Adding New Points

We can now move the existing points, but what about adding new ones? Again, we'll need two functions, one in the HTML, and one in the Code.gs file.

First, add a function to the HTML to open a prompt when the user clicks an empty spot on the map, and pass the value to a server function.

    // Function to add a new pin
    map.on('click', function(e) {
      const latLng = e.latlng;
      const title = prompt('Enter a title for the new pin:');
      if (title) {
        google.script.run.withSuccessHandler(function(id) {
          addNewMarker({ id, title, lat: latLng.lat, lng: latLng.lng });
        }).addNewPin({ title, lat: latLng.lat, lng: latLng.lng });
      }
    });

    function addNewMarker({ id, title, lat, lng }) {
      const marker = L.marker([lat, lng], {
        draggable: true
      }).addTo(map);

      marker.bindPopup(`${title}`).openPopup();

      marker.on('dragend', function(e) {
        const latLng = e.target.getLatLng();
        saveMarkerPosition({ id, title, lat: latLng.lat, lng: latLng.lng });
      });
    }

Then add the function to the Code.gs to save the new row.

function addNewPin({ title, lat, lng }) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName('map_pin');

  // Check if there are any rows present, if not initialize ID
  const lastRow = sh.getLastRow();
  let newId = 1;

  if (lastRow > 0) {
    const lastId = sh.getRange(lastRow, 1).getValue();
    newId = lastId   1;
  }

  sh.appendRow([newId, title, lat, lng]);

  return newId;
}

Save once more and refresh the test deployment. Now when you click an empty spot, you can enter a title and save a new marker!

Building an Interactive XY Image Plot with Google Apps Script and Leaflet.js

Deleting A Marker

Lastly, we should add a way to delete markers, giving us a full CRUD app in map view.

Update the add marker function to give the popup a delete button:

      const popupContent = `${title}
`; marker.bindPopup(popupContent).openPopup();

And then add a function for deleting from the client side:

// Function to delete a marker
  function deleteMarker(id) {
    const confirmed = confirm('Are you sure you want to delete this marker?');
    if (confirmed) {
      google.script.run.withSuccessHandler(() => {
        // Refresh the markers after deletion
        google.script.run.withSuccessHandler(addMarkers).getPinData();
      }).deleteMarker(id);
    }
  }

Then add the matching function to the Code.gs file:

function deleteMarker(id) {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sh = ss.getSheetByName('map_pin');
  const data = sh.getDataRange().getValues();

  for (let i = 1; i 



What's Next?

There's a ton more you could do from here, like adding other data points to each marker, dynamic background images, or other click and drag interactions. You could even make a game! Got an idea for a use case? Drop a comment below!

版本聲明 本文轉載於:https://dev.to/greenflux/building-an-interactive-xy-image-plot-with-google-apps-script-and-leafletjs-2ooe?1如有侵犯,請聯絡study_golang@163 .com刪除
最新教學 更多>
  • 如何將 Firebase 與 Laravel 集成
    如何將 Firebase 與 Laravel 集成
    Laravel and Firebase are two powerful tools that can significantly enhance the development of modern web applications. Laravel, a popular PHP framewor...
    程式設計 發佈於2024-11-07
  • Expo with Redux Toolkit、檔案系統與 Redux Persist:綜合指南
    Expo with Redux Toolkit、檔案系統與 Redux Persist:綜合指南
    Redux Toolkit 是一個流行的函式庫,它透過提供一組實用程式和約定來簡化 Redux 開發。它包括一個減速器和動作創建模式,可以簡化編寫 Redux 邏輯的過程。將 Redux Persist 與 Redux Toolkit 結合可以顯著提高 React Native 應用程式中狀態管理的...
    程式設計 發佈於2024-11-07
  • 如何處理非巢狀 Lambda 閉包中的變數作用域問題?
    如何處理非巢狀 Lambda 閉包中的變數作用域問題?
    Python Lambda 閉包作用域問題將變數封裝在閉包中以將其從函數簽章中刪除是一種常用於高效代碼結構的技術。但是,在非嵌套 lambda 的情況下,閉包保留變數的最終值,從而在嘗試基於迭代變數存取特定值時導致問題。 考慮提供的程式碼片段:names = ['a', 'b', 'c'] def ...
    程式設計 發佈於2024-11-07
  • 如何使用現代 CSS 將按鈕無縫整合到輸入欄位中?
    如何使用現代 CSS 將按鈕無縫整合到輸入欄位中?
    如何使用現代CSS 將按鈕整合到輸入中問題:創建視覺元素其中按鈕無縫整合在輸入欄位中,允許正常的使用者互動、保留文字可見性並保持可存取性和螢幕閱讀器相容性。 解決方案:Flexbox 和表單邊框最佳方法是使用彈性盒佈局以及包含元素(表單)上的邊框:定位:設定具有水平行佈局的彈性盒,允許輸入擴展以填充...
    程式設計 發佈於2024-11-07
  • 核心開發中的 C++:綜合指南
    核心開發中的 C++:綜合指南
    介紹 由於直接硬體存取和最小的運行時開銷,核心開發傳統上是 C 的領域。然而,由於其物件導向的特性,C 在核心程式設計中找到了自己的位置,這可以帶來更乾淨、更易於維護的程式碼。本指南將逐步介紹如何使用 C 進行核心開發,重點是設定環境、建置專案以及使用 C 功能編寫核心程式碼,同時...
    程式設計 發佈於2024-11-07
  • 在 React 專案中實作 CSS 模組
    在 React 專案中實作 CSS 模組
    React 中的 CSS 模組是一種透過自動產生唯一類別名稱來確定 CSS 範圍的方法。這可以防止大型應用程式中的類別名稱衝突並允許模組化樣式。以下是如何在 React 專案中使用 CSS 模組: 1. 設定 預設情況下,React 支援 CSS 模組。您只需使用擴展名 .modul...
    程式設計 發佈於2024-11-07
  • 有哪些資源可用於實現彗星模式?
    有哪些資源可用於實現彗星模式?
    Comet:伺服器推送模式伺服器推送是一種在伺服器和Web 用戶端之間實現雙向通訊的技術,已經獲得了顯著的成果最近的興趣。 Comet 設計模式是作為在 JavaScript 應用程式中實現伺服器推送的一種有前途的方法而出現。本問題探討了 Comet 模式的 jQuery 實作和通用資源的可用性。 ...
    程式設計 發佈於2024-11-07
  • 探索心理健康門診計畫的類型
    探索心理健康門診計畫的類型
    門診心理健康治療方法是一種不強調在醫療機構過夜的方案。這種療法主要在醫生辦公室、醫院或診所提供,在那裡人們可以進行定期治療,以進行高度結構化的定期治療。 在 COVID-19 大流行期間,全球約有 2.75 億名吸毒者。比前幾十年高出近 22%。吸毒成癮的增加導致全美吸毒過量人數創下歷史新高。好消...
    程式設計 發佈於2024-11-07
  • 如何在 C++ Builder 中初始化 OpenGL 幀:逐步指南
    如何在 C++ Builder 中初始化 OpenGL 幀:逐步指南
    如何在C Builder 中初始化OpenGL 幀在C Builder 中的窗體內初始化OpenGL 幀可能是一項具有挑戰性的任務。在嘗試調整現有 OpenGL 程式碼(例如問題中提供的範例)時,您可能會遇到困難。 要正確建立和渲染OpenGL 幀,請依照下列步驟操作:使用TForm::Handle...
    程式設計 發佈於2024-11-07
  • 利用這些罕見的 HTML 屬性來增強您的 Web 開發技能
    利用這些罕見的 HTML 屬性來增強您的 Web 開發技能
    Introduction HTML attributes are most often referred to as the overlooked heroes of web development, playing a crucial role in shaping the st...
    程式設計 發佈於2024-11-07
  • 如何在 Python 中將字串轉換為二進位:ASCII 與 Unicode?
    如何在 Python 中將字串轉換為二進位:ASCII 與 Unicode?
    在Python中將字串轉換為二進位在Python中,您可能會遇到需要將字串表示為二進位數字序列的情況。這對於多種原因都很有用,例如資料加密或二進位檔案操作。 使用 bin() 函數將字串轉換為二進位的最簡單方法就是使用bin()函數。該函數接受一個字串作為輸入,並將其二進位表示形式傳回為字串。例如:...
    程式設計 發佈於2024-11-07
  • 為什麼從 Java 中的匿名內部類別存取外部實例變數需要是 Final?
    為什麼從 Java 中的匿名內部類別存取外部實例變數需要是 Final?
    Java內部類別:為什麼必須使用「最終」外部實例變數在Java中定義匿名內部類別時,您可能會遇到將外部實例變數標記為“final”的要求。本文探討了這個約束背後的原因。 如同提供的程式碼中所提到的,實例變數 jtfContent 必須宣告為 Final 才能在內部類別中存取。這項要求源自於 Java...
    程式設計 發佈於2024-11-07
  • 理解 Python 中的關鍵字參數
    理解 Python 中的關鍵字參數
    When you're programming in Python, knowing how to pass arguments to functions is key for writing clear, flexible, and easy-to-maintain code. One powe...
    程式設計 發佈於2024-11-07
  • 如何防止列印時DIV跨頁分割?
    如何防止列印時DIV跨頁分割?
    列印問題:防止 DIV 跨頁分叉遇到動態 DIV 在頁面之間切成兩半的列印困境?當嘗試列印具有大量可變高度 DIV 元素的冗長文件時,就會出現此問題。 CSS 救援解決方案為了解決此問題,CSS 屬性打破了 -裡面來拯救。透過指定值避免,您可以確保渲染引擎防止 DIV 中途分割。這是程式碼片段:@m...
    程式設計 發佈於2024-11-07
  • Python 是強類型語言嗎?
    Python 是強類型語言嗎?
    Python 是強型別語嗎? Python 中的強型別概念造成了一些混亂,因為該語言允許變數改變執行期間的類型。然而,Python 確實是強型別的,儘管是動態的。 Python 中的強型別強型別可確保值維持其宣告的型別,除非明確轉換。在Python中,這意味著變數沒有固定的類型,而是它們所保存的值有...
    程式設計 發佈於2024-11-07

免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。

Copyright© 2022 湘ICP备2022001581号-3