Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何更改Mapbox中的标记图像?_Javascript_Mapbox Gl Js - Fatal编程技术网

Javascript 如何更改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] },

我对mapbox不熟悉。我在mapbox地图中更改标记图像时遇到问题。 我从我无法更改标记图像获得代码

这是我的密码:

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"
      }
    }
};