Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

[Google Maps API v3] How to get a reference to an existing map?

4,943 views
Skip to first unread message

Blaine

unread,
Apr 21, 2010, 6:43:49 PM4/21/10
to Google Maps JavaScript API v3
I have a div with id="mapCanvas"

I create a map inside the div
var map = new google.maps.Map(document.getElementById("mapCanvas"),
myOptions);


Assuming that the function that creates the map has completed and I
have now lost the map var.

How do I get a reference back to the existing map when I only know the
div it is in?

var oldmap = document.getElementById("mapCanvas").theMapInisdeThisDiv;

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

Jason Sanford

unread,
Apr 21, 2010, 9:49:15 PM4/21/10
to google-map...@googlegroups.com
Hi Blaine
It sounds like you might be declaring your map variable inside a function. Try declaring it outside of a function and initializing inside like so:
var map;
function initialize() {
var myLatlng = new google.maps.LatLng(-34.397, 150.644);
var myOptions = {
zoom: 8,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP

Blaine

unread,
Apr 22, 2010, 10:30:47 AM4/22/10
to Google Maps JavaScript API v3
Yes, I am and that does work when I know how many maps are going to be
on a page but I've made my map function generic enough to use on
multiple maps on the same page.

I have looked into making a 2 dimensional array to store map var | div
id, but I was hoping there would be a way to get a reference to the
map in the div with only the div id. This is one of those situations
where I can force it to work, but I would just like something a bit
more elegant.

Any ideas?
> > google-maps-js-a...@googlegroups.com<google-maps-js-api-v3%2Bunsu...@googlegroups.com>
> > .
> > For more options, visit this group at
> >http://groups.google.com/group/google-maps-js-api-v3?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
> To post to this group, send email to google-map...@googlegroups.com.
> To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
> For more options, visit this group athttp://groups.google.com/group/google-maps-js-api-v3?hl=en.

Luke Mahé

unread,
Apr 22, 2010, 1:49:07 PM4/22/10
to google-map...@googlegroups.com
Hi Blane,

You could do something like:

  var mapDiv = document.getElementById('map-div-id');
  var map = new google.maps.Map(mapDiv, myOptions);
  mapDiv.map = map;

then later you can get the map by doing:

  document.getElementById('map-div-id').map

I'm just writing this off the top of my head so it might not work in all browsers.

- Luke
Reply all
Reply to author
Forward
0 new messages