So obvious! - no idea why I thought i could not declare map outside of
the function.

Thanks for you help.

C.

On Apr 28, 1:14 pm, Andrew Leach <[email protected]>
wrote:
> On Apr 28, 12:58 pm, Chris Barker <[email protected]> wrote:
>
> > Thanks for this,
>
> > I am actually after calling the setCenter in a seperate function but
> > if i create a new instance of GMap2 I loose the exisiting map.
> > In my new function I want to hook into the already rendered map and
> > then setCenter. (hope this makes sense)
>
> Yes. But if you want to refer to the map object (that is the object
> held in var map) outside the function in which it is assigned, the
> variable must be global.
>
> This won't work:
>
> function start() {
>   var map = new GMap2(...);
>   ...
>   }
> function centre() {
>   map.setCenter(...);
>   }
>
> because "map" only exists within function "start". "map" needs to
> exist outside that function and be available everywhere. This will
> work:
>
> var map;
> function start() {
>   map = new GMap2(...);
>   ...
>   }
> function centre() {
>   map.setCenter(...);
>   }
>
> but of course, "start" needs to be run before "centre" in order that
> "map" holds something useful.
>
> There are lots of resources on Javascript variable scope, including
> Mike's athttp://econym.org.uk/gmap/scope.htm
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Google Maps API" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group 
> athttp://groups.google.com/group/google-maps-api?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"Google Maps API" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-maps-api?hl=en.

Reply via email to