gpt4 book ai didi

javascript - 如何将标记添加到 MapBox GL JS map ?

转载 作者:可可西里 更新时间:2023-11-01 13:22:53 25 4
gpt4 key购买 nike

我正在尝试向 HTML/Javascript map 中的 MapBox-GL-JS map 添加标记。

我已经尝试构建一个小示例:这是我的代码

<html>
<head>
<meta charset="utf-8">
<title>Test MapBox</title>

<!-- *** References for MapBox GL JS ... -->
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.37.0/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.37.0/mapbox-gl.css' rel='stylesheet' />

<style>
html, body {
min-height: 100%;
min-width: 100%;
margin: 0;
padding: 0;
}
table {
width: 95vw;
height: 95vh;
}
td {
width: 50%;
height: 50%;
}
</style>
</head>
<body>
<div id="maps-images">
<center>
<table border=1>
<!-- second row -->
<tr id="row2">
<td id="osm">
<div id="osm_map" style="width:100%;height:100%"></div>
</td>
</tr>
</table>
</center>
</div>

<script>
mapboxgl.accessToken = '<PUT_YOUR_MAPBOX_KEY_HERE';
// *** Create and configure the map ...
var osm_map = new mapboxgl.Map({
container: 'osm_map', // container id
style: 'mapbox://styles/mapbox/bright-v9', //stylesheet location
center: [13.291408333333237,42.628386111111126], // starting position
zoom: 16 // starting zoom
});
// *** Add zoom and rotation controls to the map ...
osm_map.addControl(new mapboxgl.NavigationControl());

marker = new mapboxgl.Marker()
.setLngLat([13.291408333333237, 42.628386111111126])
.addTo(osm_map);

</script>
</body>
</html>

代码工作正常,但我的 map 上没有显示任何标记....

建议/示例?

最佳答案

标记没有任何默认样式。您需要创建一个 HTML 元素并传递它。参见示例 here :

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

关于javascript - 如何将标记添加到 MapBox GL JS map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44208200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com