0

EDIT:

$.getJSON(url, function (data) {
var x = 0;
var places = [];
$.each(data.lugar, function (i, user) {
    places[x] = {
        canEdit: false,
        lat: user.latt,
        lng: user.lng,
        name: "Somewhere "+x
    };
    alert(isNumber(places[x].lng));
    x++;
}).promise().done(function () {
    $(document).ready(function() {
        runExample5();
    });
});
});

So Im trying to populate a map in javascript like this

{ showOnLoad: places ....

And it gets the job done with a for loop like this (it does show the markers) THIS WORKS

 var places = [];
  for(var x= 0; x<10; x++){
  places[x] = {
    canEdit: false,
    lat: 53.79+x,
    lng:-1.5426760000000286+x,
    name: "Somewhere "+x

}
}

But when I try to populate it with the info I receive on JSON/PHP from anothe page, it does not work, and it does not populate with markers...

 var places = [];
$.getJSON(url, function (data) {
var x = 0;
$.each(data.lugar, function (i, user) {
    places[x] = {
        canEdit: false,
        lat: user.latt,
        lng: user.lng,
        name: "Somewhere "+x
    };
    x++;
}).promise().done(function () {
});
});



function runExample5(a) {
 $("#search-for-places").mapsed({

    showOnLoad: places,
    allowGeo: true, 
    disablePoi: true


});                                 
 }


 $(document).ready(function() {
runExample5();

 });

I get no population unless I call another function and then it automatically show the markers but I cant seem to get it at the beginning when the map first loads but I get it when I use the FOR loop.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Enrique Bravo
  • 133
  • 1
  • 1
  • 11
  • WTH do you think does `.promise().done(function () { });` do on the return value of the [`$.each()`](http://api.jquery.com/jQuery.each/) call? It most likely will throw an exception, as `data.lugar` hardly has such a method. – Bergi May 19 '14 at 04:41
  • You are using the empty `places` array before it is populated from the **asynchronous** `$.getJSON` callback. – Bergi May 19 '14 at 04:44
  • I think I get what you are trying to say (you are saying that I am trying to use places on runExample5 before it is even populated because it is just being populated at the same time with $.get, right?)... So is there any way to tell it to use it right after it is being populated without messing everything out? Thanks for the response by the way – Enrique Bravo May 19 '14 at 04:50
  • Move everything (the `places` declaration, the `$(document).ready()` with the `runExample5()` call) within the callback of the ajax function. – Bergi May 19 '14 at 04:53
  • Thanks for your kind replies. That is a thing I have been considering before but if I do that, for some strange reason it does not show anything, the map simply vanish. I added the modified code on my original post, am I doing it right? Thanks! – Enrique Bravo May 19 '14 at 05:04
  • No, as I said that `.promise().done(function () { });` is a complete error. Remove it. Or tell me what it was supposed to do. – Bergi May 19 '14 at 05:06
  • You are all and completely right. I thought it would be some kind of conditional if it worked and then ran the missing code. Thank you very much, you should have posted it as an answer so I can give you rep. – Enrique Bravo May 19 '14 at 05:15

1 Answers1

1

Just for the sake of completion, here's the correct solution:

$.getJSON(url).then(function (data) {
    // instead of map(), you could also use that tedious `each` loop
    // it would work the same
    return $.map(data.lugar, function (i, user) {
        return {
            canEdit: false,
            lat: user.latt,
            lng: user.lng,
            name: "Somewhere "+x
        }
    });
}).done(function (places) {
    $(document).ready(function() {
        // if you wanted to call a runExample5 function, make `places` a parameter
        $("#search-for-places").mapsed({
            showOnLoad: places,
            allowGeo: true, 
            disablePoi: true
        });
    });
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375