javascript - 错误的图像 url 将图像添加到 mapbox 弹出窗口
问题描述
今天,我试图找到一种将图像属性插入mapbox
弹出窗口的方法。因此,在网站上搜索,我找到了一种将此属性添加到弹出窗口中的方法,但由于未知原因,结果是:
http://2.bp.blogspot.com/-uitX7ROPtTU/Tyv-G4NA_uI/AAAAAAAAFBY/NcWLPVnYEnU/s1600/no+image.jpg/
而不是http://2.bp.blogspot.com/-uitX7ROPtTU/Tyv-G4NA_uI/AAAAAAAAFBY/NcWLPVnYEnU/s1600/no+image.jpg
.
因此/
,图像 url 的末尾是给出未找到图像的原因。你知道为什么吗?
<script>
mapboxgl.accessToken = '........';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11',
center: [25.147437, 37.548452],
zoom: 9.5
});
// Add zoom and rotation controls to the map.
map.addControl(new mapboxgl.NavigationControl());
map.on('load', function () {
var location = document.getElementById("1234").innerText;
var locations= eval('['+location+'];');
// Add a layer showing the places.
map.addLayer({
"id": "places",
"type": "symbol",
"source": {"type": "geojson","data": {"type": "FeatureCollection","features": locations}},"layout": {"icon-image": "{icon}-15","icon-allow-overlap": true}});
// When a click event occurs on a feature in the places layer, open a popup at the
// location of the feature, with description HTML from its properties.
map.on('click', 'places', function (e) {
var coordinates = e.features[0].geometry.coordinates.slice();
// Ensure that if the map is zoomed out such that multiple
// copies of the feature are visible, the popup appears
// over the copy being pointed to.
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
new mapboxgl.Popup()
.setLngLat(coordinates)
.setHTML('<b>'+ e.features[0].properties.title + '</b>' + e.features[0].properties.description + '<p><img src='+e.features[0].properties.image+'></img></p>')
.addTo(map);
});
// Change the cursor to a pointer when the mouse is over the places layer.
map.on('mouseenter', 'places', function () {
map.getCanvas().style.cursor = 'pointer';
});
// Change it back to a pointer when it leaves.
map.on('mouseleave', 'places', function () {
map.getCanvas().style.cursor = '';
});
});
</script>
解决方案
好吧,我不得不改变
<img src='+e.features[0].properties.image+'></img>
使用以下代码:
<img src="'+e.features[0].properties.image+'"/>
推荐阅读
- android - int android.support.v7.widget.GridLayoutManager.getItemCount()' 在空对象引用上
- regex - 如何大写和更改存储在变量中的字符串中的最后一个特殊字符?
- wordpress - 从跨域的 wp rest api 获取数据
- sql - 通过单个可空字段选择行数的正确方法?
- javascript - 将具有参数的无状态 React 组件转换为有状态
- java - 如何使用 jasper 报告使用某些 mysql 数据库打印 jLabel 值
- java-7 - Java 控制台:readPassord()- 密码的大小/长度
- mysql - 如何使用父表主键获取子表的最新值(有条件)
- visual-studio-2008 - Visual Studio 2008 是否允许您保存工作区(打开的代码文件集及其顺序)
- sql-server - 使用 SQL Server 脚本远程移动文件