admin管理员组文章数量:1287791
I have a marker on my map representing the current location of the International Space Station (pulled from .json?callback=?). I'm also trying to get it to move every 1 second to follow along with the station's orbit.
This is the code I have now:
$.getJSON('.json?callback=?', function(data) {
var latitude = data["data"]["iss_position"]["latitude"];
var longitude = data["data"]["iss_position"]["longitude"];
var iss = L.marker([latitude,longitude]).bindPopup("I am the ISS").addTo(map);
$(function() {
setInterval(function() {
iss.setLatLng([latitude,longitude]).update();
},1000);
});
});
Here's everything in a jsFiddle: /
It seems to place the marker in the correct position on load, but does not update every second as it should be. Any tips on what I'm doing wrong?
Thanks!
I have a marker on my map representing the current location of the International Space Station (pulled from http://open-notify-api.herokuapp./iss-now.json?callback=?). I'm also trying to get it to move every 1 second to follow along with the station's orbit.
This is the code I have now:
$.getJSON('http://open-notify-api.herokuapp./iss-now.json?callback=?', function(data) {
var latitude = data["data"]["iss_position"]["latitude"];
var longitude = data["data"]["iss_position"]["longitude"];
var iss = L.marker([latitude,longitude]).bindPopup("I am the ISS").addTo(map);
$(function() {
setInterval(function() {
iss.setLatLng([latitude,longitude]).update();
},1000);
});
});
Here's everything in a jsFiddle: http://jsfiddle/zmkqu/
It seems to place the marker in the correct position on load, but does not update every second as it should be. Any tips on what I'm doing wrong?
Thanks!
Share Improve this question asked Jan 22, 2013 at 19:54 FebriumFebrium 551 gold badge2 silver badges5 bronze badges 02 Answers
Reset to default 9You should put the whole Ajax call inside the setInterval
callback. Right now you are setting the marker to the same position very second, because data
does not change. You have to make a new call every second:
var iss;
function update_position() {
$.getJSON('http://open-notify-api.herokuapp./iss-now.json?callback=?', function(data) {
var latitude = data["iss_position"]["latitude"];
var longitude = data["iss_position"]["longitude"];
if (!iss) {
iss = L.marker([latitude,longitude]).bindPopup("I am the ISS").addTo(map);
}
iss.setLatLng([latitude,longitude]).update();
setTimeout(update_position, 1000);
});
}
update_position();
DEMO
Note: It's better to use setTimeout
together with Ajax calls, since you don't know how a long it takes to get the response.
If you need to guarantee that it updates every second you probably want to modify this a bit... Right now it fires 1 second after the server responds with the json, which is most likely not 1 second since the last update.
What you want is something more like this:
var iss
, timeElapsed = 0;
function update_position() {
elapsedTime = new Date().getMilliseconds();
$.getJSON('http://open-notify-api.herokuapp./iss-now.json?callback=?', function(data) {
var latitude = data["data"]["iss_position"]["latitude"];
var longitude = data["data"]["iss_position"]["longitude"];
elapsedTime = new Date().getMilliseconds() - elapsedTime;
if (!iss) {
iss = L.marker([latitude,longitude]).bindPopup("I am the ISS").addTo(map);
}
iss.setLatLng([latitude,longitude]).update();
setTimeout(update_position, 1000 - elapsedTime);
});
}
update_position();
You could probably go even farther with this... there will be some stuttering if a request takes more then a second for instance if your request takes longer then a second. But if that request is taking longer then a second you probably have other issues you need to deal with.
本文标签: javascriptUpdating Leaflet Marker Position Every x Seconds with JSONStack Overflow
版权声明:本文标题:javascript - Updating Leaflet Marker Position Every x Seconds with JSON - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741238091a2363424.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论