我有一个使用 Mapbox js 在地图上添加标记的 rails 应用程序。它与 Mapbox JS 完美配合。但是,我无法弄清楚如何让它与 Mapbox GL JS 一起使用。这是我的控制器索引操作:
def index
@locations = Location.all
@geojson = Array.new
@locations.each do |location|
@geojson << {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [location.longitude, location.latitude]
},
properties: {
title: location.name,
description: [location.elevation, location.locatable_type ],
:'marker-color' => "#3ca0d3",
:'marker-symbol' => "rocket",
:'marker-size' => 'medium'
}
}
end
respond_to do |format|
format.html
format.json { render json: @geojson }
end
end
在我看来,我使用此脚本通过 Mapbox JS 获取地图上的点:
L.mapbox.accessToken = 'pk.mylongaccesskeygoeshere';
var map = L.mapbox.map('map', 'mapbox.streets')
.setView([13.770391, -88.866245], 8);
map.scrollWheelZoom.disable();
var featureLayer = L.mapbox.featureLayer()
.loadURL('/maps/index.json')
.addTo(map);
我的问题是:如何使用 Mapbox GL JS 完成此任务?这是我用来在页面上获取地图的代码。
mapboxgl.accessToken = 'pk.mylongaccesskeygoeshere';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/outdoors-v9', //stylesheet location
center: [-88.866245, 13.770391], // starting position
zoom: 7.2 // starting zoom
});
我已经尝试了很多不同的方法来传递我的“数据”的 url,就像它在 Mapbox GL JS 文档中所说的那样。例如,我试过
mapboxgl.accessToken = 'pk.mylongaccesskeygoeshere';
var map = new mapboxgl.Map({
container: 'map', // container id
style: 'mapbox://styles/mapbox/outdoors-v9', //stylesheet location
center: [-88.866245, 13.770391], // starting position
zoom: 7.2 // starting zoom
});
var url = '/maps/index.json';
var source = new mapboxgl.GeoJSONSource({
data: url
});
map.on('load', function () {
map.addSource('location', source);
map.addLayer({
"id": "location",
"type": "symbol",
"source": "location",
"layout": {
"icon-image": "rocket-15",
}
});
});
我在控制台中收到此错误:
map.js:929 Error: Input data is not a valid GeoJSON
object.util.extend.onError @ map.js:929
Evented.fire @ evented.js:90
util.extend._forwardSourceEvent @ map.js:943
Evented.fire @ evented.js:90
util.inherit._forwardSourceEvent @ style.js:680
Evented.fire @ evented.js:90
(anonymous function) @ geojson_source.js:162
Actor.receive @ actor.js:31
当我转到 maps/index.json 时,页面上 json 的输出如下所示:
[
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-89.12312324,
13.686886
]
},
"properties": {
"title": "Random Location",
"description": [
"700.0",
"Individual"
],
"marker-color": "#3ca0d3",
"marker-symbol": "rocket",
"marker-size": "medium"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-88.231293812398,
13.087893
]
},
"properties": {
"title": "Some Place",
"description": [
"50.0",
"Organization"
],
"marker-color": "#3ca0d3",
"marker-symbol": "rocket",
"marker-size": "medium"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-89.224564,
13.700985
]
},
"properties": {
"title": "San Salvador",
"description": [
"550.0",
"Individual"
],
"marker-color": "#3ca0d3",
"marker-symbol": "rocket",
"marker-size": "medium"
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
-89.0,
13.0
]
},
"properties": {
"title": "Set Location Test",
"description": [
"100.0",
"Individual"
],
"marker-color": "#3ca0d3",
"marker-symbol": "rocket",
"marker-size": "medium"
}
}
]