Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,131
    Thanks
    10
    Thanked 1,162 Times in 1,153 Posts

    Building a JSON array within each loop

    I have a Google Map that places markers on a map.

    If I do this, it places 4 markers on the map (sending 'locs' to setMarker function) ... it works fine ...
    Code:
    	var locs = {
    	154554: { info:'11111. Some random info here', lat:44.251695, lng:-93.167002 },
    	245623: { info:'22222. Some random info here', lat:44.251695, lng:-93.164002 },
    	37634: { info:'33333. Some random info here', lat:44.352695, lng:-93.168002 },
    	47345: { info:'44444. Some random info here', lat:44.454695, lng:-93.161002 }
    	};
    	setMarkers(locs);
    Now I want to build that same thing dynamically in an "each" loop.

    My problem is how to create that JSON locs 'array' (or variable?)

    The part that gets data from an API ...
    Code:
                        $.ajax({
                            url: 'https://public-api/.../.../LocationList.json?fDstL=0&fDstU=100',
                            crossDomain: true,
    			dataType : 'jsonp'
                        })
                            .done(function(data) {
                            //console.log('data.acList:'+data.acList);
    	                  $.each(data.acList, function(key, val) {
                                latitd = val.Lat;
                                longtd = val.Long;
                                titleName = val.Op;
    			    unitID =val.Id;
    							
    				// The variables contain valid values with each pass in the loop.
    				// somewhere here I need to add each item to the 'locs' array.
    				// or maybe the better word is to 'build' the 'locs' JSON.
    				// When the loop is finished, I will then set the markers after the loop.
    							
    				// The variables fall into the JSON like this ...
    				//  { unitID: { info:'titleName', lat:latitd, lng:longtd }};
    			});
                        })
                            .fail(function(e){
                            console.log(e);
    			})
                            .always(function(){
                            console.log('ajax executed');
    			});
    						
    			// Now it will send 'locs' to the setMarkers function ...
    			setMarkers(locs);
    Could someone please provide some direction to me so I can set markers by building the JSON?

    Thanks
    Last edited by mlseim; 01-10-2017 at 12:31 AM.

  2. #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,619
    Thanks
    58
    Thanked 662 Times in 657 Posts
    you're right - it's not an array you're building, but an object and thus it can accept either dot or square bracket notation...

    PHP Code:
    .done(function(data) {
                    var 
    locs={}; // set up an empty object to store your values
             
                    
    $.each(data.acList, function(keyval) {
                    var 
    unitID=val.Id;
                    
    locs[unitID]={}; //create inner object
                    
    locs[unitID].info=val.Op// start adding properties
                    
    locs[unitID].lat=val.Lat;
                    
    locs[unitID].lng=val.Long;                
                });
    }); 

  3. #3
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,131
    Thanks
    10
    Thanked 1,162 Times in 1,153 Posts
    Thanks for the hints.
    It's so close now ... I'll do some more messing around and see if I can make it work.

  4. #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Location
    Here
    Posts
    3,619
    Thanks
    58
    Thanked 662 Times in 657 Posts
    It would seem to me that the only thing left to do would be to put your setMarkers call inside the .done callback

  5. #5
    Supreme Master coder!
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    10,131
    Thanks
    10
    Thanked 1,162 Times in 1,153 Posts
    All of the help I got on this thread .....

    I thought I should update everyone on my progress. The help given to me here drove me to a working solution. Thanks everyone.

    I am grabbing data from a free api, the ADS-B Exchange which provides airplane data for any GPS location and radius. The data doesn't always update (some planes get stuck) and not all planes get reported. But the data is good enough to see the flight paths in real time. So I am monitoring my local airport MSP (Minneapolis St. Paul International, Minnesota).

    Here's my first rendition:
    Http://www.catpin.com/airtraffic/map

    I had to blank planes that are landed and those flying over the airspace. Those seem to stay on the list, but quit reporting. JavaScript updates every 5 seconds.

    A filtered list and link to raw Json data found here:
    Http://www.catpin.com/airtraffic

    Thanks again
    Last edited by mlseim; 01-12-2017 at 01:59 AM.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •