$(function() {
mapboxgl.accessToken = 'pk.###';
var map = new mapboxgl.Map({
container: 'map-global',
style: '..'
});
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"title": "POI Title"
},
"geometry": {
"type": "Point",
"coordinates": [0, 0]
}
}
]
};
geojson.features.forEach(function(marker) {
// create a HTML element for each feature
var el = document.createElement('div');
el.className = 'marker';
new mapboxgl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.setPopup(new mapboxgl.Popup()
.setHTML(marker.properties.title))
.addTo(map);
});
});
设置标志和它工作正常。但我想用GeoJSON作为外部文件:
var geojson = 'file.geojson';
在这里,我有一个问题 - 它不工作:
TypeError: undefined is not an object (evaluating '"map.geojson".features.forEach')".
有没有办法使用外部GeoJSON文件,自定义HTML 标记?