javascript - 使用 OpenWeather 图标作为传单地图上的标记
问题描述
我正在尝试将从 openweather API 调用返回的天气图标添加到我的传单地图作为标记,但没有得到任何运气。
它们将显示在标签中标记的弹出窗口中,当我 console.log iconUrl 链接显示为正确的图标但它们只会显示为地图上的默认蓝色标记 - 有什么想法吗?
我的脚本的相关部分:
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);
推荐阅读
- mailchimp - 在 mailchimp 帐户下可以管理哪些兴趣类别?
- c - 当我以写入模式打开文件时,文件是否加载到 RAM 中?
- linux - 关于预期行为的一般信号问题
- startup - 有没有办法根据 FastAPI 启动事件的条件取消启动?
- c# - 如何捕获登录PC的Microsoft帐户信息
- python - Python Opencv-python 运行时崩溃
- python - Python while循环输出到列表
- apache-kafka - 在 Apache kafka 中使用 camel-sftp-kafka-connector 时遇到问题?
- python - 从*.vtu 文件中提取数据
- c++ - 为什么我不能在 Eclipse 上运行我的代码,即使我可以构建它?