Javascript 在地图角传单中每5秒移动一个标记
我想每5秒在地图上移动一个标记,现在我的代码在调试模式下工作,我的意思是,每当我从调试模式切换到标记直接到达目标的最后一个lat long点时,我都能看到标记在调试模式下的移动 控制器代码:Javascript 在地图角传单中每5秒移动一个标记,javascript,angularjs,leaflet,angular-leaflet-directive,Javascript,Angularjs,Leaflet,Angular Leaflet Directive,我想每5秒在地图上移动一个标记,现在我的代码在调试模式下工作,我的意思是,每当我从调试模式切换到标记直接到达目标的最后一个lat long点时,我都能看到标记在调试模式下的移动 控制器代码: app .controller('AboutCtrl', function ($scope,$http,leafletData,$timeout) { $scope.markers = []; var iss; angular.extend($scope, { osloCen
app
.controller('AboutCtrl', function ($scope,$http,leafletData,$timeout) {
$scope.markers = [];
var iss;
angular.extend($scope, {
osloCenter: {
}
});
function updatePoints(){
$http.get('views/newdata.json').success(function(response) {
leafletData.getMap('mymap').then(function(map) {
for(var i = 0; i < response.length; i++){
var latitude = response[i].lat
var longitude = response[i].lng
if (!iss) {
iss = L.marker([latitude,longitude]).bindPopup("Vehicle is Here").addTo(map);
}
iss.setLatLng([latitude,longitude]).update();
setTimeout(updatePoints, 5000);
}
});
});
}
updatePoints();
angular.extend($scope, {
osloCenter: {
lat: 12.98254,
lng: 77.59258,
zoom: 5
},
markers: {
},
defaults: {
scrollWheelZoom: false
}
});
});
可能是卷发坑:
setTimeout(updatePoints, 5000);
替换为:
$timeout(updatePoints, 5000);
这确保了angularJS摘要周期的启动。好的,在我仔细阅读了您的代码之后。超时部分是问题所在,但您需要从更改为下一个,以便执行其他操作,例如:
function updatePoints(i){
$http.get('views/newdata.json').success(function(response) {
leafletData.getMap('mymap').then(function(map) {
i = i || 0; //set default
var latitude = response[i].lat
var longitude = response[i].lng
if (!iss) {
iss = L.marker([latitude,longitude]).bindPopup("Vehicle is Here").addTo(map);
}
iss.setLatLng([latitude,longitude]).update();
if (i<response.length) {
$timeout(function () {updatePoints(i+1)}, 5000);
}
});
});
}
函数更新点(i){
$http.get('views/newdata.json').success(函数(响应){
getMap('mymap')。然后(函数(map){
i=i | | 0;//设置默认值
变量纬度=响应[i]。纬度
var经度=响应[i].lng
如果(!iss){
iss=L.标记([纬度,经度]).bindPopup(“车辆在这里”).addTo(地图);
}
iss.setLatLng([纬度,经度]).update();
如果这不起作用,标记器又到了最后一个lat long点。
$timeout(updatePoints, 5000);
function updatePoints(i){
$http.get('views/newdata.json').success(function(response) {
leafletData.getMap('mymap').then(function(map) {
i = i || 0; //set default
var latitude = response[i].lat
var longitude = response[i].lng
if (!iss) {
iss = L.marker([latitude,longitude]).bindPopup("Vehicle is Here").addTo(map);
}
iss.setLatLng([latitude,longitude]).update();
if (i<response.length) {
$timeout(function () {updatePoints(i+1)}, 5000);
}
});
});
}