我在JSON响应上每隔2秒在JSON文件上构建一个获取用户位置,我在下面尝试了这个代码,但标记没有显示。
locate();
var map = L.map('map', {scrollWheelZoom:true}).setView([51.505, -0.09], 15);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
var myMovingMarker;
function locate() {
$.getJSON('<?php echo base_url();?>index.php/vehicle/get_vehicle_location/2',
function(response) {
var longitude = response['longtitude'];
var latitude = response['latitude'];
document.getElementById("coordinates").innerHTML =
"<span style='font-weight: bold;''>Longitude:</span> "
+ longitude + " | <span style='font-weight: bold;''>Latitude:</span> "
+ latitude + " ";
if (!myMovingMarker) {
myMovingMarker = L.marker([latitude,longitude]).addTo(map);
}
myMovingMarker.setLatLng([latitude,longitude]).update();
setTimeout(locate, 2000);
});
}