I’m pretty new to javascript and google maps. I have an input that, when submitted, geocodes the value and I’d like it to:
- if a map exists (something has been entered and successfully geocoded
already), then recenter it - else create the map centered on that point.
My code is kind of like this:
var map;
function getlatlong() { //this function gets the input value and then geocodes
if(map.length){recentermap()} //if map exists, recenter map
else{createmap()} //create map
}
function createmap(){ //this function creates the map (by editing `var map`)
var map = new google.maps.Map();
}
I think I just don’t understand how js variables work… my question is, how do I see if var map has been changed if I change it within the second function?
When you say:
You are creating a local variable called map that only exists in the function createmap. You want to use your
mapvariable that you declared in the outer scope, so you should not use thevarkeyword: