Loading a local geojson file in Mapbox GL JS

huangapple go评论47阅读模式
英文:

Loading a local geojson file in Mapbox GL JS

问题

我尝试在Mapbox GL JS中加载本地保存的geojson文件,但是在浏览器中加载时它并没有显示出来。当我将一个实时URL替换为"data"时,它可以工作,但是当我引用本地文件时,它不起作用。我已经检查过本地文件确实有效且存在。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>使用GeoJSON源将多边形添加到地图</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
    <link href="https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.css" rel="stylesheet">
    <script src="https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.js"></script>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>
    <div id="map"></div>

    <script>
        mapboxgl.accessToken = '你的访问令牌';
        var map = new mapboxgl.Map({
            container: 'map',
            style: 'mapbox://styles/mapbox/dark-v11',
            zoom: 10,
            center: [-73.98, 40.76],
        });

        map.on('load', () => {

            var dataGJ = 'data/borough-boundaries.geojson';

            map.addSource('borough-boundaries', {
                type: 'geojson',
                data: dataGJ
            })

            map.addLayer({
                'id': 'borough-boundaries-fill',
                'type': 'fill',
                'source': 'borough-boundaries',
                'paint': {
                    'fill-color': 'green',
                }
            })
        });
    </script>
</body>
</html>

我尝试将文件保存在本地并在代码中引用它,但它仍然无法在浏览器中加载。

英文:

I am trying to load a locally-saved geojson file in Mapbox GL JS, however it does not show up when loaded in the browser. It works when I swap out a live URL for the 'data', however when I reference the local file, it does not work. I have checked that the local file is indeed valid and actually there.

&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot;&gt;
&lt;title&gt;Add a polygon to a map using a GeoJSON source&lt;/title&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;initial-scale=1,maximum-scale=1,user-scalable=no&quot;&gt;
&lt;link href=&quot;https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;script src=&quot;https://api.mapbox.com/mapbox-gl-js/v2.13.0/mapbox-gl.js&quot;&gt;&lt;/script&gt;
&lt;style&gt;
body {
margin: 0;
padding: 0;
}
#map {
position: absolute;
top: 0;
bottom: 0;
width: 100%;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div id=&quot;map&quot;&gt;&lt;/div&gt;
&lt;script&gt;
mapboxgl.accessToken = &#39;pk.eyJ1IjoiYWRhbWJydWVja25lciIsImEiOiJjbGVoNTYwaTUwcm8zM3htejV0MHpxY2RiIn0.XMLudwO9aIRQquhkKGEnVg&#39;;
var map = new mapboxgl.Map({
container: &#39;map&#39;,
// Choose from Mapbox&#39;s core styles, or make your own style with Mapbox Studio
style: &#39;mapbox://styles/mapbox/dark-v11&#39;,
zoom: 10,
center: [-73.98, 40.76],
//projection: &#39;globe&#39;
});
map.on(&#39;load&#39;, () =&gt; {
var dataGJ = &#39;data/borough-boundaries.geojson&#39;;
// add a source layer for the borough boundaries
map.addSource(&#39;borough-boundaries&#39;, {
type: &#39;geojson&#39;,
data: dataGJ
//data: &#39;https://data.cityofnewyork.us/api/geospatial/tqmj-j8zm?method=export&amp;format=GeoJSON&#39;
})
map.addLayer({
&#39;id&#39;: &#39;borough-boundaries-fill&#39;,
&#39;type&#39;: &#39;fill&#39;,
&#39;source&#39;: &#39;borough-boundaries&#39;,
&#39;paint&#39;: {
&#39;fill-color&#39;: &#39;green&#39;,
}
})
});
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;`

I tried saving the file locally and referencing it in the code, but still it does not actually load up in the browser

答案1

得分: 0

在Mapbox GL样式中,数据URL需要是绝对路径:http://localhost/data/borough-boundaries.geojson或类似的。

英文:

In Mapbox GL styles, data URLs need to be absolute: http://localhost/data/borough-boundaries.geojson or similar.

答案2

得分: 0

如果@Steve的解决方案没有奏效,请尝试参考我以前编写的这个示例。它会呈现来自本地路径的数据(相对于index.html的数据路径)。我没有使用map.addSource(),而是像下面这样在map.addLayer()中指定了数据源。

map.on('load', () => {
    map.addLayer({
        id: 'restaurant',
        type: 'circle',
        source: {
            type: 'geojson',
            data: 'data/chainness_point_2021.geojson' 
        }
    });
});
英文:

If @Steve's solution did not work, try referencing this example I wrote in the past. It renders data from a local path (relative path to the data from index.html). I did not use map.addSource() and I just specified the source in map.addLayer() like below.

map.on(&#39;load&#39;, () =&gt; {
map.addLayer({
id: &#39;restaurant&#39;,
type: &#39;circle&#39;,
source: {
type: &#39;geojson&#39;,
data: &#39;data/chainness_point_2021.geojson&#39; 
}
});

huangapple
  • 本文由 发表于 2023年2月24日 07:28:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551318.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定