![](/img/trans.png)
[英]React-leaflet LayersControl throws error when moving code into function
[英]Leaflet error when moving marker: setLatLng is not a function
我是Leaflet的新手,並使用Open Notify API( http://open-notify.org/Open-Notify-API/ )在地圖上跟蹤ISS。 創建標記並將其添加到地圖后,我調用Open Notify提供的以下功能:
function moveISS () {
$.getJSON('http://api.open-notify.org/iss-now.json?callback=?', function(data) {
var lat = data['iss_position']['latitude'];
var lon = data['iss_position']['longitude'];
iss.setLatLng([lat, lon]);
isscirc.setLatLng([lat, lon]);
map.panTo([lat, lon], animate=true);
});
setTimeout(moveISS, 5000);
}
這成功獲取了lat和lon值,但后來我得到了錯誤:
Uncaught TypeError: iss.setLatLng is not a function
我知道setLatLng方法存在於Leaflet文檔( http://leafletjs.com/reference-1.0.2.html#marker-setlatlng )中,為什么它不被調用? 是否有其他插件需要先被安裝才能被識別?
從open-notify示例中,示例代碼中有一條注釋:
有關設置圖標和地圖圖層的信息,請參閱傳單文檔
問題中顯示的代碼未顯示iss
的定義。 該錯誤表明它可能不是傳單marker
。
可以像Leaflet示例代碼中那樣定義iss
和isscirc
:
var iss = L.marker([51.5, -0.09]).addTo(map).bindPopup("<b>The International Space Station</b>").openPopup();
var isscirc = L.circle([51.508, -0.11], 500, {
color: 'red',
fillColor: '#f03',
fillOpacity: 0.5
}).addTo(map);
上面的起始坐標是任意的,並將在第一次調用moveISS
更新。
有關從Leaflet示例代碼改編的工作示例,請參閱jsfiddle 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.