0

I am trying to use a timer in my javascript to poll certain data every 5seconds from my other objects in cshtml and update it into the function in javascript.

i have explored with the following code but it seems to throw the error of initialize is undefined

function initialize() {

    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googleMap")
        , mapProp);


}

$(document).ready(function () {
    initialize();
});
window.setInterval(function () {
    /// call your function here
    initialize();
}, 5000);

However, this works perfectly fine

function initialize() {

    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("googleMap")
        , mapProp);


}

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

As i want to make the variables global such that i can update the focus on the map every 5 seconds, i have tried to make it a global variable

    var mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

However, it seems that the script keeps on giving me an error of google is undefined.

Is there anyway i could resolve my problem.

For example i would like to update the parameters in new google.maps.LatLng(51.508742, -0.120850) every 5 seconds.

7
  • If you use the var keyword inside a function then it's not a global variable, it's scoped to that function. Commented Oct 9, 2013 at 8:58
  • There is nothing global with var - it sets the local variable - global variable is typed without var or explicitly as a window property => window.myVariable = "something"; Commented Oct 9, 2013 at 8:59
  • 3
    Have you included the script? Commented Oct 9, 2013 at 8:59
  • What about writing the setInterval function inside document ready ? Commented Oct 9, 2013 at 9:00
  • if ur getting google is undefined check whether google api ur using is correct i.e the script(google) ur using in the page Commented Oct 9, 2013 at 9:00

3 Answers 3

1

Here is the working code,

<!DOCTYPE html>
<html>
    <head>
        <script src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&sensor=false">
        </script>
        <script src="http://code.jquery.com/jquery-1.9.0.min.js"></script>
        <script>
            var mapProp;
            $(document).ready(function(){initialize();})
            function initialize()
            {
                mapProp = changeMapProp();
                var map=new google.maps.Map(document.getElementById("googleMap")
                ,mapProp);
            }
            function changeMapProp() {
                mapProp = {
                    center:new google.maps.LatLng(52.508742,-0.120850),
                    zoom:5,
                    mapTypeId:google.maps.MapTypeId.ROADMAP
                };
                return mapProp;
            }
            window.setInterval(function () {
                /// call your function here
                initialize();
            }, 5000);

        </script>
    </head>

    <body>
        <div id="googleMap" style="width:500px;height:380px;"></div>

    </body>
</html> 
Sign up to request clarification or add additional context in comments.

Comments

1

Try:

var mapProp;
var map;

var initialize = function() {

    mapProp = {
        center: new google.maps.LatLng(51.508742, -0.120850),
        zoom: parseInt(5),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("googleMap")
        , mapProp);

}

$(document).ready(function () {

    setTimeout(initialize, 5000);

});

1 Comment

the debugger shows the error at initialize in the window.setinterval function
1

You can't make var mapProp global because it will fire the center: new google.maps... straight away, and your google variable won't be in place yet (which is why you get google undefined).

Regarding the initialize undefined, put your setInterval inside your $(document).ready function.

Comments

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.