0

I have the following code and I want to call initialize function here with the following; google.maps.event.addDomListener(window, 'load', initialize);

But I am getting this error; Error: initialize is not defined.

What is wrong here?

$(function CheckinMap() {
        $.ajax({
            type: "GET",
            url: "content/home/index.cs.asp?Process=ViewCheckinMap",
            success:     function initialize(data) {
                var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);

                var map = new google.maps.Map(document.getElementById('map'), {
                  zoom: 2,
                  center: center,
                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                  maxZoom: 4
                });

                var markers = [];
                for (var i = 0; i < data.users.length; i++) {
                  var location = data.users[i];
                  var latLng = new google.maps.LatLng(location.latitude,
                      location.longitude);
                  var marker = new google.maps.Marker({
                    position: latLng
                  });
                  markers.push(marker);
                }
                var markerCluster = new MarkerClusterer(map, markers);
            },
            error: function (data) {
                $("#checkinmap").append(data);
            }
        });
    });
google.maps.event.addDomListener(window, 'load', initialize);
0

2 Answers 2

1

If you want to use a function for both the jQuery success method and the addDomListener third argument, then you have to store it somewhere when you define it rather than passing it directly to success.

That somewhere also has to be in scope for both your call to ajax and to addDomLister.

Move function initialize(data) so it appears before your current line 1.

Then say success: initialize.

Sign up to request clarification or add additional context in comments.

1 Comment

When the success event handler fires, it gets passed the data from the Ajax request. Then the window load event handler gets fired it gets an event object … and the data from the Ajax request hasn't event been retrieved from the server. Having looked more closely, it doesn't make any sense to run that initialise function onload.
0

You're defining intialize inside CheckinMap scope and then trying to use it outside of that scope in last line of you code. If you really need to have it in both places, define it outside and then reference if in success property value.

function initialize(data) {
    var center = new google.maps.LatLng(48.404840395764175, 2.6845264434814453);

    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 2,
      center: center,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      maxZoom: 4
    });

    var markers = [];
    for (var i = 0; i < data.users.length; i++) {
      var location = data.users[i];
      var latLng = new google.maps.LatLng(location.latitude,
          location.longitude);
      var marker = new google.maps.Marker({
        position: latLng
      });
      markers.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, markers);
}

$(function CheckinMap() {
        $.ajax({
            type: "GET",
            url: "content/home/index.cs.asp?Process=ViewCheckinMap",
            success: initialize,
            error: function (data) {
                $("#checkinmap").append(data);
            }
        });
    });
google.maps.event.addDomListener(window, 'load', initialize);

3 Comments

Actually, using a function expression always produces an anonymous function, and does not create a reference in the current context, even if the anonymous function is named. The name of anonymous functions is defined in the context of the function itself, i.e. it is available inside the anonymous function, not in the outside scope.
Any time you create any object, including function, you also create a single reference to it, that can be dropped just after you've used the object, unless you hold it somewhere. I'm talking about those references, not sure if you do too.
console.log or use a network monitor in debugging tools of your browser on your received data and insure that you've got what you've expected and not some error message.

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.