라이브 추적을 위해 Google지도에서 작업 중입니다. 데이터베이스 업데이트에 따라 마커를 변경해야합니다.변수에 Foreach 루프 사용
이것은 testLocs 변수입니다. 나는 그것을 역동적으로 만들고 싶다.
var testLocs = {
1: { info:'Demo', lat:31.933517, lng:74.910278 },
2: { info:'Demo', lat:32.073266 , lng:76.997681 },
3: { info:'Demo', lat:32.23139 , lng:78.425903 },
}
setMarkers(testlocs);
이렇게 아약스를 호출 할 때마다 3000 초마다 (3 초) i는 ajax 스크립트를 호출합니다. 그래서이 형식으로 아약스의 응답을 받았습니다. 내가 아약스 응답 데이터베이스에서 3 개 배열을 가지고 콘솔에서
,
Array-1 : ["Device-1", "Device-2", "Device-3"]; // all device name
Array-2 : ["31.933517", "32.073266", "32.23139"]; // all latitude
Array-3 : ["74.910278", "76.997681", "78.425903"]; // all longitude
는 지금, 나는 위 var에 testLocs에서 그것을 사용하려고합니다.
전체 기능,
function myTimer(new_latitude, new_longitude,new_name)
{
var new_latitude = new_latitude;
var new_name = new_name;
var new_longitude = new_longitude;
console.log(new_name);
console.log(new_latitude);
console.log(new_longitude);
var testLocs = {
1: { info:'Demo', lat:31.933517, lng:74.910278 },
2: { info:'Demo', lat:32.073266 , lng:76.997681 },
3: { info:'Demo', lat:32.23139 , lng:78.425903 },
}
setMarkers(testlocs);
}
var myVar = setInterval(function(){ myTimer() }, 3000);
나는이 있지만 작동하지 않습니다와 함께했습니다.
var testLocs = {
new_latitude.forEach(function(single_value)
{
single_value_latitude = single_value;
// alert(single_value_latitude);
});
}
편집 :
Ajax 코드
(function worker() {
$.ajax({
type: "POST",
dataType: "json",
// data: {business1: business1},
url: '<?php echo site_url('home/automatic_fetch_data'); ?>',
success: function (data) {
//alert(data);
var new_lat = [];
var new_lng = [];
var new_name = [];
$.each(data, function(k, v) {
var lati = v['latitude'];
var lngi = v['longitude'];
var namei = v['name'];
new_lat.push(lati);
new_lng.push(lngi);
new_name.push(namei);
});
var new_latitude = new_lat;
var new_longitude = new_lng;
var new_name = new_name;
// console.log(new_latitude);
// console.log(new_longitude);
myTimer(new_latitude, new_longitude,new_name);
},complete: function() {
// Schedule the next request when the current one's complete
setTimeout(worker, 3000);
}
});
})();
나는 아약스 응답 이후에 배열 아래에있어, console.log(data)
;
배열 :
배열 4 객체와 내가 모든 정보 DEVICE_ID, DEVICE_NAME, 위도, 경도를 가지고 각 개체있다. 콘솔에서이 정보에 배열이 있습니다.
[Object { id="1", device_id="1", device_name="Device-1", latitude="29.630771", longitude="74.910278"}, Object { id="2", device_id="2", device_name="Device-2", latitude="32.073266", longitude="76.997681"}, Object { id="3", device_id="5", device_name="Device-3", latitude="29.630771", longitude="74.910278"}, Object { id="5", device_id="3", device_name="Device-3", latitude="29.630771", longitude="74.910278"}]
은 무엇 인 AJAX 요청에서 얻은 정확한 응답? 현재 배열 3 개가 유효한 구문이 아닙니다. 또한 응답을 변경할 수 있습니까? 나중에 JS에서 해킹하는 것보다 훨씬 쉬울 것으로 예상되는 형식으로 돌아 오는 것이 –
@RoryMcCrossan입니다. 답장을 보내 주셔서 감사합니다. 데이터베이스에서 아약스 응답 전체 배열 있어요. 하지만 var testLocs 다른 형식으로 그래서 나는 그것이 가능할 수 있습니다, 내가 등, 긴 등을 사용하여 그것을 분할 생각합니다. 내 질문을 전체 배열로 업데이트하고 있습니다. 및 아약스 코드. – Bhavin
@Bhavin받은 개체에 "이름"속성이 없습니다. "var namei = v [ 'name']"각 콜백 내부에서 실패해야하는 라인입니다. 가치 또는 코드를 다시 확인할 수 있습니까? –