gpt4 book ai didi

javascript - 在 html 中更改自定义 Leaflet 标记

转载 作者:行者123 更新时间:2023-11-27 23:35:04 25 4
gpt4 key购买 nike

我是网络 map 和传单开发的初学者...我找到了一个简单但有用的代码,我想知道如何将下面 HTML 代码中的所有传单标记与 mylocal.png(或 .svg)交换.提前感谢您的任何反馈!祝大家有个美好的一天

<!doctype html>
<meta charset="utf-8">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.css">
<style>
html, body, #map { height: 100%; margin: 0; }
</style>

<body>
<div id="map"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js"></script>
<script src="https://cdn.rawgit.com/tyrasd/osmtogeojson/2.2.5/osmtogeojson.js"></script>
<script>
var api = 'http://overpass-api.de/api/interpreter';
var query = 'area["place"="city"]["name"="Cluj-Napoca"];node[amenity=cafe](area);out;';
var map = L.map('map');
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
$.get(api, {data: query}, function(resp) {
var geojson = osmtogeojson(resp);
var layer = L.geoJson(geojson).addTo(map);
map.fitBounds(layer.getBounds());
});
</script>

最佳答案

默认情况下,当使用 L.GeoJSON 时,GeoJSON 数据中的每个点都会变成默认的 L.Marker。您可以使用 L.GeoJSONpointToLayer 选项返回带有 L.Icon 的自定义 L.Marker它使用你的形象。

new L.GeoJSON(data {
pointToLayer: function (feature, latlng) {
return new L.Marker(latlng, {
icon: new L.Icon({
iconUrl: 'leaf-green.png',
shadowUrl: 'leaf-shadow.png',
iconSize: [38, 95], // size of the icon
shadowSize: [50, 64], // size of the shadow
iconAnchor: [22, 94], // point of the icon which will correspond to marker's location
shadowAnchor: [4, 62], // the same for the shadow
popupAnchor: [-3, -76] // point from which the popup should open relative to the iconAnchor
})
});
}
}).addTo(map);

关于javascript - 在 html 中更改自定义 Leaflet 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34203849/

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