如何在 JavaScript 中正确读取 geoJSON 文件?

问题描述 投票:0回答:1

我正在使用 Leaflet 和 OpenStreetMap 在我的页面上显示地图。我按照步骤在页面上创建了一个简单地图。一旦我能够生成地图,我就会按照创建 geojson 文件的步骤,转到 https://geojson.io/

我遇到的问题是当我调用文件时,出现以下错误:“未捕获错误:无效的 GeoJSON 对象。”。不知道我做错了什么。

以下是代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>Interactive Map</title>
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.7.1/leaflet.css" />
        <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
        <style>
            #map {
                height: 500px;
                width: 100%;
            }
            .leaflet-popup-content{ width: 330px !important; }
        </style>
    </head>
    <body>
        <div id="map"></div>
        <script>
            const myGeoJSONData = 'json/geomap.geojson';
            const mymap = L.map('map').setView([34.0000, -118.300], 13);
            L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
                attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors',
                maxZoom: 18
            }).addTo(mymap);
            const laCounty = L.marker([34.05951960593492, -118.25037820245461], 13).addTo(mymap);
            laCounty.bindPopup("<b>Location Name</b><br><b>Address:</b> <br /><b>Phone:</b>").openPopup();

            mymap.on('click', function(e) { 
                L.marker(e.latlng).addTo(mymap); 
            });

            var myGeoJSONLayer = L.geoJSON(myGeoJSONData, {
            style: function(feature) {
                return {color: feature.properties.color};
            },
            onEachFeature: function(feature, layer) {
                layer.bindPopup(feature.properties.name);
            }
            }).addTo(mymap);
        </script>
    </body>
</html>

以下是geojson:

 {
   "type":"FeatureCollection",
   "features":[
      {
         "type":"Feature",
         "geometry":{
            "type":"Point",
            "coordinates":[
               34.05951960593492,
               -118.25037820245461
            ]
         },
         "properties":{
            "name":"location name",
            "address":"address of location",
            "phone_number":"phone number"
         }
      }
   ]
}
javascript leaflet openstreetmap
1个回答
0
投票

myGeoJSONData
需要是一个 javascript 对象,而不是像当前那样指向 geojson 文件的文件位置的字符串。

在脚本中内联 geojson 数据。

const myGeoJSONData = {
   "type": "FeatureCollection",
   "features": [
      {
         "type": "Feature",
         "geometry": {
            "type": "Point",
            "coordinates": [
               34.05951960593492,
               -118.25037820245461
            ]
         },
         "properties": {
            "name": "location name",
            "address": "address of location",
            "phone_number": "phone number"
         }
      }
   ]
}
var myGeoJSONLayer = L.geoJSON(myGeoJSONData, {
   style: function (feature) {
      return { color: feature.properties.color };
   },
   onEachFeature: function (feature, layer) {
      layer.bindPopup(feature.properties.name);
   }
}).addTo(mymap);
© www.soinside.com 2019 - 2024. All rights reserved.