简体   繁体   中英

Google Maps V3 cannot call function from infowindow

I have created a zoom function for my Google maps:

function zoomIn(){

map.setZoom(parseInt(map.getZoom())+2)

}

I keep on getting a 'Object expected' error when I try to call it. When I debug this

Error: zoomIn is not defined

Source File: javascript:zoomIn()

Any ideas of why this is happening is much appreciated.

The map can be seen using the link below, the function is located on line 189

http://tinyurl.com/3ryuxvy

Your problem is with function scope. You define the zoomIn function inside initialize . When the user clicks on the control to zoom in and zoomIn() is called it is looking for zoomIn on the global window object but it doesn't exist there.

To fix this you need to refactor your javascript so zoomIn is available in the global scope. This could mean implementing the function outside of initialize() .

Read more about JS scoping here: http://www.digital-web.com/articles/scope_in_javascript/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM