我在View上使用了mapbox 并需要从JSON添加多个标记
这是我的JSON
[
{
"name":"Thistle London Heathrow Termin",
"address1":"Bath Road",
"rating":3.0,
"lng":-0.4836,
"lat":51.4805
},
{
"name":"Ibis London Heathrow Airport",
"address1":"112 Bath Road Hayes",
"rating":3.0,
"lng":-0.430683,
"lat":51.48079
},
{
"name":"Britannia Inn",
"address1":"54 Mansfield Road",
"rating":2.0,
"lng":0.066,
"lat":51.563
},
{
"name":"Cranbrook Hotel",
"address1":"22 24 Coventry Road",
"rating":2.0,
"lng":0.069,
"lat":51.563
},
{
"name":"Park Hotel",
"address1":"327 Cranbrook Road",
"rating":2.0,
"lng":0.065,
"lat":51.568
},
{
"name":"Rest Up London",
"address1":"Driscoll House",
"rating":2.0,
"lng":-0.096403,
"lat":51.494554
},
{
"name":"Ascot Hyde Park",
"address1":"11 Craven Road",
"rating":3.0,
"lng":-0.176815,
"lat":51.514745
},
{
"name":"Fairways Bayswater",
"address1":"186 Sussex Gardens",
"rating":2.0,
"lng":-0.1748,
"lat":51.5144
},
{
"name":"Four Stars",
"address1":"26 28 Sussex Gardens",
"rating":3.0,
"lng":-0.1685,
"lat":51.5179
},
{
"name":"King Solomon",
"address1":"155 159 Golders Green Road",
"rating":3.0,
"lng":-0.21045388,
"lat":51.58082156
},
{
"name":"Somerset.",
"address1":"6 Dorset Square",
"rating":2.0,
"lng":-0.1607,
"lat":51.5229
},
{
"name":"Trinity House",
"address1":"26 Blackhorse Raod",
"rating":2.0,
"lng":-0.0356,
"lat":51.5832
},
{
"name":"Viking",
"address1":"162 Romford Road",
"rating":2.0,
"lng":0.01299262,
"lat":51.54324077
},
{
"name":"Wedgewood",
"address1":"49 51 Leinster Square",
"rating":2.0,
"lng":-0.1928,
"lat":51.51365
},
{
"name":"Kensington Suite",
"address1":"128 130 Holland Road",
"rating":3.0,
"lng":-0.2121,
"lat":51.5015
}
]
以下是我运行脚本以将地图添加到View
的方法let centerLatlng = new mapboxgl.LngLat(gon.destination_city.lng,gon.destination_city.lat);
mapboxgl.accessToken = token;
let map = new mapboxgl.Map({
container: 'map-canvas',
style: 'mapbox://styles/mapbox/streets-v9',
center:centerLatlng,
zoom: 9
});
map.addControl(new mapboxgl.NavigationControl());
});
但是我想知道,我需要添加标记来映射(对于json中的每个元素我需要得到lat和lon)来映射。因为根据文档我需要像这样的json
var geojson = { type:'FeatureCollection', 特征: [{ 类型:'功能', 几何:{ 类型:'点', 坐标:[ - 77.032,38.913] }, 属性:{ 标题:'Mapbox', 描述:'Washington,D.C。' } }, { 类型:'功能', 几何:{ 类型:'点', 坐标:[ - 122.414,37.776] }, 属性:{ 标题:'Mapbox', 描述:'旧金山,加利福尼亚' } }] };
我可以用我的json制作标记
答案 0 :(得分:0)
我找到了解决方法
这很简单
像这样var data1 = [a,b,c,d,e,f,g,h];
var data2 = c; // this is element, that i want to find from data1 and
remove all next element i.e. d, e, f, g, h .
或使用jQuery
for(var i = 0; i < json.length; i++) {
var obj = json[i];
let myLatlng = new mapboxgl.LngLat(obj.lng, obj.lat);
let marker = new mapboxgl.Marker()
.setLngLat(myLatlng)
.setPopup(new mapboxgl.Popup({ offset: 25 })
.setHTML('<h3>' + obj.name + '</h3><p>' + obj.address1 + '</p>'))
.addTo(map);
}
答案 1 :(得分:0)
还有其他方法可以更快地在 imo 中工作,例如通过层添加源,geoJson 必须采用您从文档链接的格式:
map.on('load', function () {
// Add an image to use as a custom marker
map.loadImage(
'img',
function (error, image) {
if (error) throw error;
map.addImage('custom-marker', image);
// Add a GeoJSON source with 2 points
map.addSource('points',geoJson);
map.addLayer(
{
'id': 'points',
'type': 'symbol',
'source': 'points',
'layout':
{
'icon-image': 'custom-marker',
// get the title name from the source's "title" property
'text-field': ['get', 'title'],
'text-font': ['Open Sans Semibold','Arial Unicode MS Bold'],
'text-offset': [0, 1.25],
'text-anchor': 'top'
}
});
});
这个例子可以在https://docs.mapbox.com/mapbox-gl-js/example/geojson-markers/
看到