Hi, zzolo,

I'm trying to setCenter on a map, but I can't find the instance to call the function on. I am in my own JS code, called from a user action; that code calculates certain lat/lon values and is supposed to center the map around these values.

I have a map-id as -- say "openlayers-map-12345678" -- from Drupal.settings.openlayers.maps
and I find the containing div with $('openlayers-map-12345678').

In some of the code examples I also see $('#openlayers-map-12345678') and $('#openlayers-map-12345678').data(...) but that first expressions returns NULL for me.

So I tried to dig into the Drupal.openlayers data tree, but no luck; following the construction of the map object also didn't enlighten me. Any help appreciated!

Thanks for your support!
-Karsten
PS: You guessed it, I am writing my own "enter your address here and the map will center around it" function because I could not find any support (yet) for that; did I overlook something?

Comments

kmadel’s picture

The following works for me:

var ol = $('.openlayers-map').data('openlayers');//assuming there is just one map on the page
var lonLat = new OpenLayers.LonLat(lon, lat).transform(new OpenLayers.Projection("EPSG:4326"), ol.openlayers.getProjectionObject());
ol.openlayers.setCenter(lonLat, 11);
nod_’s picture

Status: Active » Fixed

You can't use the map ID to select it in JS, it changes all the time. You should do it like kmadel described.
I'll have to implement the 'enter you address and i'll center the map' too there should be a patch in the comming weeks to take care of it.

I'm closing unless you need more help with that.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

gc23’s picture

Sorry to dig up this thread, but I too am experiencing this same problem and the code posted didn't help.
Basically, I am using openlayers using a view, and don't know if this could cause an issue.

I have a single map showing on the page and used firebug to execute JS.

var ol = $('.openlayers-map').data('openlayers');
Does not work, it cannot find the cmap and returns null, so instead I used the full id of the div like so:

var ol = $('openlayers-map-99401a36').data('openlayers');
Which then generates the following:
TypeError: $("openlayers-map-99401a36").data is not a function
Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.
[error] return jQuery(contentView).hasClass('popupWindow');

PS. I am using 7.x-2.0-beta1.

Thanks

gc23’s picture

Status: Closed (fixed) » Active
nod_’s picture

Well, $() is not registered anymore. You should use jQuery() from the command line.

gc23’s picture

Status: Active » Closed (fixed)

Ah yes indeed. Thank you. All works fine now.