0

I have 3 functions in javascript in my MVC.NET view that run in the wrong order, not from the top to the bottom.

  1. We get cities from our table and push to an array. Also no URL:s are created for the next functions where we call open weather map API with url+cit+key.

  2. Call open weather map with the new URL:s to get the temperature data.

  3. The weather data from open weather map is supposed to go in info window and in the content, for it to print later when a marker is clicked.

The second functions run before the first one every time and we have tried a lot of different things but nothing is working. What can we do?

@model List<IDAbroad.Models.PeopleModell>

@ViewData["Listan"]
@ViewBag.message

<script src="~/Scripts/jquery-1.10.2.min.js"></script>

<div id="map" style="width:100%;height:600px"></div>

<script>
function myMap() {

    var myCenter = new google.maps.LatLng(51.508742, -0.120850);
    var mapOptions = { center: myCenter, zoom: 2 };
    var mapCanvas = document.getElementById("map");
    var map = new google.maps.Map(mapCanvas, mapOptions);
    var cities = [];


    $(document).ready(function () {

        var url = '/People/myJson';
        var url2 = [];
        var url;
        var data;
        var string = "";
        var lat = [];
        var lon = [];
        var namn;
        var i = 1;
        var temp = [];
        var description = [];
        var wIcon = [];

        $(function () {               
            $.getJSON(url, function (data) {
                $.each(data, function () {
                    //cities[i] = this['City'];
                    cities.push(this['City']);
                    url2.push('http://api.openweathermap.org/data/2.5/weather?q=' + this['City'] + '&APPID=MYKEY');

                    console.log("Första loopen: " + this['City']);

                    i++;
                });

                console.log("Hela listan med städer: " + cities + '<br>' + " HEEEEEEEEJ" + url2 + '<br>');
            });
            alert("Ska va etta");
            getCities();
        });

        function getCities() {
            $(function () {
                console.log("Url 2 längd: " + url2.length);

                for (var i = 1; i <= url2.length; i++) {
                    alert("kom in");
                    var url3 = url2[i]

                    $.getJSON(url3, function (data) {
                        $.each(data, function () {

                            temp[i] = this['main.temp'];
                            description[i] = this['description'];
                            wIcon[i] = this['icon'];
                            console.log("temperatur: " + temp[i]);

                        })
                    });
                }
            });

        };



            var i = 1;

        $(function () {
            $.getJSON(url, function (data) {

                $.each(data, function () {
                    namn = this['Firstname'];

                    console.log("Position X: " + lat[i]);
                    console.log("Position Y: " + lon[i]);
                    var peoplePosition = new google.maps.LatLng(this['Latitude'], this['Longitude']);
                    var ico = {
                        url: "/profileImg/" + i + ".jpg",
                        scaledSize: new google.maps.Size(35,50)
                    }
                    var marker = new google.maps.Marker({
                        position: peoplePosition,
                        icon: ico

                    })
                    i++;
                    marker.setMap(map);
                    var content = this['Firstname'] + '<br>Stad: ' + this['City'] + '<br>Temp: ' + temp[i] + '<br>' + description[i];
                    var infowindow = new google.maps.InfoWindow()
                    google.maps.event.addListener(marker, 'click', (function (marker, content, infowindow) {

                        return function () {
                            infowindow.setContent(content);
                            infowindow.open(map, marker);
                        };
                    i++    
                    })(marker,content,infowindow));


                })
            })
        });  
    });
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=MYSECONDKEY">
</script>
SynozeN Technologies
  • 1,337
  • 1
  • 14
  • 19
Cia
  • 13
  • 5
  • Possible duplicate: [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – abhishekkannojia May 25 '17 at 08:08
  • OMG you have a `document.ready` in a `function` in a `document.ready` in a `function`, no wonder your execution is totally messed up. Create ONE main `document.ready` and put all your functions inside it. The document is ready only once. – Jeremy Thille May 25 '17 at 08:09
  • BTW `$(document).ready(` is the same thing as `$(function () {` (the latter is a shorthand) – Jeremy Thille May 25 '17 at 08:09
  • That´s funny! Now we put myMap at the top, and after that document ready, and all the functions without jquery $ but now it says that myMap is not a function. Maybe something about the google maps key at the bottom. The key contains callback=myMap at the end of the key. – Cia May 25 '17 at 08:24

1 Answers1

0

why you have 2 On Ready Functions Nested

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

this is meaning less and it wouldn't work

the other thing when you define 'getCities' function inside $(document).ready(function () {
you limit the scope of this function

more over that you have defined whole your $(document).ready inside the 'myMap()' function this means you have to call the function myMap first to register your other code to run at Document ready which is really ambiguous why don't you just define things like this way

var myCenter = new google.maps.LatLng(51.508742, -0.120850);
var mapOptions = { center: myCenter, zoom: 2 };
var mapCanvas = document.getElementById("map");
var map = new google.maps.Map(mapCanvas, mapOptions);
var cities = [];

function getCities() {

                console.log("Url 2 längd: " + url2.length);

                for (var i = 1; i <= url2.length; i++) {
                    alert("kom in");
                    var url3 = url2[i]

                    $.getJSON(url3, function (data) {
                        $.each(data, function () {

                            temp[i] = this['main.temp'];
                            description[i] = this['description'];
                            wIcon[i] = this['icon'];
                            console.log("temperatur: " + temp[i]);

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

    var url = '/People/myJson';
    var url2 = [];
    var url;
    var data;
    var string = "";
    var lat = [];
    var lon = [];
    var namn;
    var i = 1;
    var temp = [];
    var description = [];
    var wIcon = [];


        $.getJSON(url, function (data) {
            $.each(data, function () {
                //cities[i] = this['City'];
                cities.push(this['City']);
                url2.push('http://api.openweathermap.org/data/2.5/weather?q=' + this['City'] + '&APPID=MYKEY');

                console.log("Första loopen: " + this['City']);

                i++;
            });

            console.log("Hela listan med städer: " + cities + '<br>' + " HEEEEEEEEJ" + url2 + '<br>');
        });
        alert("Ska va etta");
        getCities();





        var i = 1;


        $.getJSON(url, function (data) {

            $.each(data, function () {
                namn = this['Firstname'];

                console.log("Position X: " + lat[i]);
                console.log("Position Y: " + lon[i]);
                var peoplePosition = new google.maps.LatLng(this['Latitude'], this['Longitude']);
                var ico = {
                    url: "/profileImg/" + i + ".jpg",
                    scaledSize: new google.maps.Size(35,50)
                }
                var marker = new google.maps.Marker({
                    position: peoplePosition,
                    icon: ico

                })
                i++;
                marker.setMap(map);
                var content = this['Firstname'] + '<br>Stad: ' + this['City'] + '<br>Temp: ' + temp[i] + '<br>' + description[i];
                var infowindow = new google.maps.InfoWindow()
                google.maps.event.addListener(marker, 'click', (function (marker, content, infowindow) {

                    return function () {
                        infowindow.setContent(content);
                        infowindow.open(map, marker);
                    };
                i++    
                })(marker,content,infowindow));


            })
        })

});
Hasan Elsherbiny
  • 598
  • 2
  • 14
  • 31