Javascript 如何更改Mapbox中的标记图像?
我对mapbox不熟悉。我在mapbox地图中更改标记图像时遇到问题。 我从我无法更改标记图像获得代码 这是我的密码:Javascript 如何更改Mapbox中的标记图像?,javascript,mapbox-gl-js,Javascript,Mapbox Gl Js,我对mapbox不熟悉。我在mapbox地图中更改标记图像时遇到问题。 我从我无法更改标记图像获得代码 这是我的密码: var geojson = { "type": "FeatureCollection", "features": [ { "type": "Feature", "properties": { "message": "IN", "iconSize": [30, 30] },
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "IN",
"iconSize": [30, 30]
},
"geometry": {
"type": "Point",
"coordinates":<?php echo $in; ?>
}
},
{
"type": "Feature",
"properties": {
"message": "Check IN",
"iconSize": [30, 30]
},
"geometry": {
"type": "Point",
"coordinates":<?php echo $c; ?>
}
},
{
"type": "Feature",
"properties": {
"message": "OUT",
"iconSize": [30, 30]
},
"geometry": {
"type": "Point",
"coordinates":<?php echo $o; ?>
}
}
]
};
geojson.features.forEach(function(marker) {
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(images/check_in.jpg)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.addEventListener('click', function() {
window.alert(marker.properties.message);
});
new mapboxgl.Marker(el, {offset: [-marker.properties.iconSize[0] / 2, -marker.properties.iconSize[1] / 2]})
.setLngLat(marker.geometry.coordinates)
.addTo(map);
});
var geojson={
“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“财产”:{
“消息”:“在”,
“iconSize”:[30,30]
},
“几何学”:{
“类型”:“点”,
“坐标”:
}
},
{
“类型”:“功能”,
“财产”:{
“消息”:“签入”,
“iconSize”:[30,30]
},
“几何学”:{
“类型”:“点”,
“坐标”:
}
},
{
“类型”:“功能”,
“财产”:{
“消息”:“退出”,
“iconSize”:[30,30]
},
“几何学”:{
“类型”:“点”,
“坐标”:
}
}
]
};
geojson.features.forEach(函数(标记){
var el=document.createElement('div');
el.className='marker';
el.style.backgroundImage='url(images/check_in.jpg)';
el.style.width=marker.properties.iconSize[0]+'px';
el.style.height=marker.properties.iconSize[1]+'px';
el.addEventListener('click',函数(){
window.alert(marker.properties.message);
});
新的mapboxgl.Marker(el,{offset:[-Marker.properties.iconSize[0]/2,-Marker.properties.iconSize[1]/2]})
.setLngLat(标记、几何、坐标)
.addTo(地图);
});
如果从他们的网站:
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(https://placekitten.com/g/' + marker.properties.iconSize.join('/') + '/)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
el.addEventListener('click', function() {
window.alert(marker.properties.message);
});
// add marker to map
new mapboxgl.Marker(el, {offset: [-marker.properties.iconSize[0] / 2, -marker.properties.iconSize[1] / 2]})
.setLngLat(marker.geometry.coordinates)
.addTo(map);
});
我想你错过了
+marker.properties.iconSize.join('/')+'/')代码>在这部分:el.style.backgroundImage='url(https://placekitten.com/g/'+marker.properties.iconSize.join('/')+'/')代码>有关详细信息,请访问此页面
var geojson={
“类型”:“FeatureCollection”,
“特点”:[
{
“类型”:“功能”,
“财产”:{
“消息”:“在”,
“iconSize”:[30,30]
},
“几何学”:{
“类型”:“点”,
“坐标”:
}
},
{
“类型”:“功能”,
“财产”:{
“消息”:“签入”,
“iconSize”:[30,30]
},
“几何学”:{
“类型”:“点”,
“坐标”:
}
},
{
“类型”:“功能”,
“财产”:{
“消息”:“退出”,
“iconSize”:[30,30]
},
“几何学”:{
“类型”:“点”,
“坐标”:
}
}
],
“财产”:{
“图标”:{
“iconUrl”:https://www.mapbox.com/mapbox.js/assets/images/astronaut2.png",
“iconSize”:[50,50],//图标的大小
“iconAnchor”:[25,25],//对应于标记位置的图标点
“popupAnchor”:[0,-25],//相对于iconAnchor打开弹出窗口的点
“类名称”:“点”
}
}
};
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "IN",
"iconSize": [30, 30]
},
"geometry": {
"type": "Point",
"coordinates":<?php echo $in; ?>
}
},
{
"type": "Feature",
"properties": {
"message": "Check IN",
"iconSize": [30, 30]
},
"geometry": {
"type": "Point",
"coordinates":<?php echo $c; ?>
}
},
{
"type": "Feature",
"properties": {
"message": "OUT",
"iconSize": [30, 30]
},
"geometry": {
"type": "Point",
"coordinates":<?php echo $o; ?>
}
}
],
"properties": {
"icon": {
"iconUrl": "https://www.mapbox.com/mapbox.js/assets/images/astronaut2.png",
"iconSize": [50, 50], // size of the icon
"iconAnchor": [25, 25], // point of the icon which will correspond to marker's location
"popupAnchor": [0, -25], // point from which the popup should open relative to the iconAnchor
"className": "dot"
}
}
};