【问题标题】:Draw markers on leaflet map from geojson data从 geojson 数据在传单地图上绘制标记
【发布时间】:2017-09-25 09:29:56
【问题描述】:

如何将geoJson数据(超过2000个坐标)导入传单地图? 这是geo json的简短示例

{
   "type": "FeatureCollection",
   "features": [
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ 44.8242557024,20.4048512901 ]
    },
    "properties": {
    }
  },
  {
    "type": "Feature",
    "geometry": {
       "type": "Point",
       "coordinates":  [ 44.8242557024,20.4048512901 ]
    },
    "properties": {
    }
  },...]

我试过的代码:

<!doctype html>
<html>
<head>
  <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
  <!--[if lte IE 8]>
     <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.ie.css" />
  <![endif]-->
  <style type="text/css">
    body {
      padding: 0;
      margin: 0;
    }

    html, body, #cmap {
      height: 100%;
    }

  </style>
  <script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
  <script src="http://code.jquery.com/jquery-2.1.0.min.js"></script>
</head>
<body>
  <div id="cmap"></div>
  <script>
  var cupcakeTiles = L.tileLayer('https://api.mapbox.com/v4/mapbox.emerald/page.html?access_token=cj5h2mqa63sc92srx3bu62e2j', {
    maxZoom: 18
  });

  $.getJSON("convertcsv.geojson", function(data) {
    var geojson = L.geoJson(data, {
      onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.name);cj5h2mqa63sc92srx3bu62e2j
      }
    });

var map = L.map('map', {
    center: [44, 20],
    zoom: 7
});

L.tileLayer('https://{s}.tiles.mapbox.com/v3/{id}/{z}/{x}/{y}.png', {       
    id: 'examples.map-20v6611k'
}).addTo(map);

new L.GeoJSON(meta1nJson).addTo(map);
  });
  </script>
</body>
</html>

但是什么也没发生,它只是一个灰色的背景。我不确定错误在哪里(可能不止一个),但导入geojson数据和地图令牌可能有错误。 我是这方面的初学者。预先感谢。

【问题讨论】:

    标签: javascript json leaflet geojson


    【解决方案1】:

    您的代码中似乎有很多问题。首先,您的 html 中不存在 id 为“map”的元素,因此无法放置地图图层。您必须在下面的代码中添加 'cmap' 作为 id。

    var map = L.map('cmap', {
       center: [44, 20],
       zoom: 7
    });
    

    你的代码中似乎没有定义 meta1nJson,所以下面的代码不起作用。

    new L.GeoJSON(meta1nJson).addTo(map);
    

    cupcakeTiles 似乎已定义但从未添加到地图中。您在下面的代码中还有一个杂散字符串应该被删除。

     $.getJSON("convertcsv.geojson", function(data) {
    var geojson = L.geoJson(data, {
      onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.name); //cj5h2mqa63sc92srx3bu62e2j
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2014-03-07
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-02
      • 2014-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多