3

I'm working on google map for live tracking. i have to change markers according to database Updation.

This is a testLocs variable. i want to make it dynamic.

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); 

So, For that from ajax call every 3000 (3 seconds) i call ajax script. So, I got response from ajax in this format.

In Console i got 3 Arrays from database in ajax response,

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

Now,I want to use it in Above var testLocs.

Whole Function,

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);

I tried with this but its not working.

var testLocs = {

      new_latitude.forEach(function(single_value)
         {
            single_value_latitude = single_value;
            // alert(single_value_latitude);
         });
}

EDIT :

Ajax code

    (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);
            }
    });
})();

I got below array in after ajax response, console.log(data);

Array :

Array with 4 Object and in each object i got all the information device_id, device_name, latitude, longitude. In Console i got array in this information.

 [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"}]
6
  • What is the exact response you get from your AJAX request? Your current list of 3 arrays is not valid syntax. Also, is it possible you can change the response? Having it return in the format you expect would be much easier than hacking it around in JS afterwards Commented Nov 5, 2016 at 13:00
  • @RoryMcCrossan. thank you for the reply. i got whole array in ajax response from database. But var testLocs is in different format so i think that if i divide it using lat, long etc than it may be possible. I'm updating my question with whole array. and ajax code. Commented Nov 5, 2016 at 13:04
  • @Bhavin there is no "name" property in your objects received. So "var namei=v['name']" inside each callback is the line which must be failing. Can you double check the value or code? Commented Nov 5, 2016 at 13:23
  • @PratikGaikwad. thank you for point it out. it was by mistake i got name in array. Commented Nov 5, 2016 at 13:25
  • @Bhavin is your problem solved or is it still there? Commented Nov 5, 2016 at 13:29

1 Answer 1

3

transformation code (3 arrays into object with another 3 objects):

var new_name = ["Device-1", "Device-2", "Device-3"];
var new_latitude = ["31.933517", "32.073266", "32.23139"]; // all latitude 
var new_longitude = ["74.910278", "76.997681", "78.425903"];

var testLocs = new_name.reduce(function (res, curr, currentIndex) {
  res[currentIndex + 1] = {
    info: new_name[currentIndex],
    lat: new_latitude[currentIndex],
    lng: new_longitude[currentIndex]
  };
  return res;
}, {});

console.log(testLocs);

so, your function may look like:

function  myTimer(new_latitude, new_longitude,new_name) {
  console.log(new_name);
  console.log(new_latitude);
  console.log(new_longitude);

  var testLocs = new_name.reduce(function (res, curr, currentIndex) {
    res[currentIndex + 1] = {
      info: new_name[currentIndex],
      lat: new_latitude[currentIndex],
      lng: new_longitude[currentIndex]
    };
    return res;
  }, {});

  setMarkers(testlocs);
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the reply. I got this error : Cannot read property 'reduce' of undefined(…).
It's perfectly working thank you so much for this solution. I still got this error can't read property 'reduce' of undefined(…).
new_name should be an array of your names (["Device-1", "Device-2", "Device-3"]), do you see it when printed by console.log(new_name)? If not, then you are probably not receiving it in your function
Oh got it it's perfectly working now. Thank you for this solution :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.