使用 OpenWeather 图标作为传单地图上的标记
Use OpenWeather Icons as Markers on Leaflet Map
我正在尝试将从 openweather API 调用返回的天气图标添加到我的传单地图中作为标记,但没有成功。
它们将显示在标记中的弹出窗口中,当我 console.log iconUrl 时 link 出现在正确的图标上,但它们只会显示为默认的蓝色标记地图 - 有什么想法吗?
我脚本的相关部分:
var weatherMarker = L.markerClusterGroup();
(Inside Ajax Call success function):
if (result.status.name == "ok") {
var temperature = Math.round(result.data[1].temp);
var weatherIcon = L.icon({
iconUrl: "http://openweathermap.org/img/w/" + result.data[1].icon + ".png",
iconSize: [25, 30],
iconAnchor: [22, 94],
popupAnchor: [-3, -76],
});
weatherMarker.addLayer(new L.marker([lat, lng], {iconUrl: weatherIcon, crossOrigin: true})).bindPopup(temperature + " °C " + result.data[1].weather).addTo(mymap);
```
[![Appearance of Marker on the map][1]][1]
[1]: https://i.stack.imgur.com/K1xgA.png
您需要添加选项 icon
而不是 iconUrl
:
weatherMarker.addLayer(new L.marker([lat, lng], {icon: weatherIcon, crossOrigin: true})).bindPopup(temperature + " °C " + result.data[1].weather).addTo(mymap);
我正在尝试将从 openweather API 调用返回的天气图标添加到我的传单地图中作为标记,但没有成功。
它们将显示在标记中的弹出窗口中,当我 console.log iconUrl 时 link 出现在正确的图标上,但它们只会显示为默认的蓝色标记地图 - 有什么想法吗?
我脚本的相关部分:
var weatherMarker = L.markerClusterGroup();
(Inside Ajax Call success function):
if (result.status.name == "ok") {
var temperature = Math.round(result.data[1].temp);
var weatherIcon = L.icon({
iconUrl: "http://openweathermap.org/img/w/" + result.data[1].icon + ".png",
iconSize: [25, 30],
iconAnchor: [22, 94],
popupAnchor: [-3, -76],
});
weatherMarker.addLayer(new L.marker([lat, lng], {iconUrl: weatherIcon, crossOrigin: true})).bindPopup(temperature + " °C " + result.data[1].weather).addTo(mymap);
```
[![Appearance of Marker on the map][1]][1]
[1]: https://i.stack.imgur.com/K1xgA.png
您需要添加选项 icon
而不是 iconUrl
:
weatherMarker.addLayer(new L.marker([lat, lng], {icon: weatherIcon, crossOrigin: true})).bindPopup(temperature + " °C " + result.data[1].weather).addTo(mymap);