从 Google Sheet 中的目的地向 Leaflet 地图添加标记和折线

Add markers and polylines to a Leaflet map from destinations in a Google Sheet

我正在尝试创建一个 Leaflet 地图,它会自动在 Google sheet.

的目的地之间添加标记和多段线

我已经设法从链接到我的 Google Sheet 的其他示例中设置了一张地图。你在这里看到这个例子:

<!DOCTYPE html>
<html>

  <head>
    <script src='//cdn.jsdelivr.net/npm/tabletop@1.5.2/src/tabletop.min.js'></script>
    <link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" />
    <script src="//unpkg.com/leaflet@1.3.4/dist/leaflet.js"></script>
    <style>
      #map-div {
        position: absolute;
        height: 100%;
        width: 100%;
      }

    </style>
  </head>

  <body>
    <div id="map-div"></div>
  </body>

</html>
var map = L.map('map-div').setView([60.1682653, 24.9422078], 5);
var basemap = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: 'Basemap (c) <a href="http://openstreetmap.org">OpenStreetMap</a>',
  minZoom: 5,
  maxZoom: 100
});
basemap.addTo(map);

function addPoints(data, tabletop) {
  for (var row in data) {
    var marker = L.marker([
      data[row].Latitude,
      data[row].Longitude
    ]).addTo(map);
    marker.bindPopup('<strong>' + data[row].Info + '</strong>');
  }
}

function init() {
  Tabletop.init({
    key: 'https://docs.google.com/spreadsheets/d/1Rs6xPlJ8pU4UFfmokjATaf4dArMWxQxZcyS-xRIIFuY/edit?usp=sharing',
    callback: addPoints,
    simpleSheet: true
  })
}
init()

https://jsfiddle.net/Enounce/kwvn5e6z/12/

但不幸的是,我没有让地图做我想做的事情的技能:

我不确定如果目标位于外部源中,这是否完全可行。感谢您的帮助!

在标记之间画线:

您需要创建一个 L.Polyline 并向其中添加经纬度:

function addPoints(data, tabletop) {
    var line = L.polyline([]).addTo(map);
    for (var row in data) {
    var marker = L.marker([
      data[row].Latitude,
      data[row].Longitude
    ]).addTo(map);
    line.addLatLng(marker.getLatLng());
    marker.bindPopup('<strong>' + data[row].Info + '</strong>');
  }
}

缩放以适合所有标记:

将标记添加到 L.FeatureGroup(),然后您可以使用 map.fitBounds(fg.getBounds());

使地图边界适合组边界
var fg = L.featureGroup().addTo(map);

function addPoints(data, tabletop) {
    var line = L.polyline([]).addTo(map);
    for (var row in data) {
    var marker = L.marker([
      data[row].Latitude,
      data[row].Longitude
    ]).addTo(fg);
    line.addLatLng(marker.getLatLng());
    marker.bindPopup('<strong>' + data[row].Info + '</strong>');
  }
  
  map.fitBounds(fg.getBounds());
}

但是 您需要从 TileLayer 中删除 minZoom: 5

目的地

将目的地存储在一个数组中,然后在循环中创建一个 html 元素并添加一个点击侦听器:

var destinationHTML = document.getElementById("destinations-body");

var fg = L.featureGroup().addTo(map);
var destinations = [];
function addPoints(data, tabletop) {
    var line = L.polyline([]).addTo(map);
    for (var row in data) {
    var marker = L.marker([
      data[row].Latitude,
      data[row].Longitude
    ]).addTo(fg);
    line.addLatLng(marker.getLatLng());
    marker.bindPopup('<strong>' + data[row].Info + '</strong>');
    destinations.push({
        marker,
      data: data[row],
      id: row
    });
    
    
  destinationHTML.innerHTML += "<div class='destination-elm' onclick='destinationClick(\""+row+"\")'><span>"+data[row].Info+"</span></div>"
  }
  
  map.fitBounds(fg.getBounds());
}

function destinationClick(id){
    console.log(id)
  destinations.forEach(function(obj){
    if(obj.id == id){
        map.panTo(obj.marker.getLatLng());
    }
  })
}

示例:https://jsfiddle.net/falkedesign/k3b4nups/