Closed (fixed)
Project:
GMap Module
Version:
6.x-1.x-dev
Component:
Miscellaneous
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
11 Jun 2007 at 13:49 UTC
Updated:
29 Jun 2013 at 17:58 UTC
Hi
I have created a node location map using the Gmap module and the location module.
I would like to be able to control the map via Javascript using the Google Maps API,but I can't figure out how to reference the map object.
The code I want to use is:
drupalMap.setCenter(new GLatLng($lat, $lon), 3);
but of course the drupalMap variable needs to be a pointer to the actual map object.
I figured out that the map is built under a Drupal object, the Drupal object has a gmap object and there is a global variable called mapid is set to "loc1".
I just can't figure out the correct path to the object
can you help?
Thanks
Ron Heywood
Comments
Comment #1
bdragon commentedI just committed some code to DRUPAL-5 to allow doing this.
Drupal.gmap.getMap(mapid);
You have to know the map id to retrieve it.
Your code would look like this:
Comment #2
swentel commentedCool, I was looking for the same functionality and this works.
Now in my setup, I have a block in the right sidebar displaying the results from a view. So you see which markers are on the map also. I've implemented your code snippet and when I click on a title, I see the map moving to the right marker.
Would it be possible to then also open this marker's infowindow ?
Comment #3
swentel commentedHi, after a couple of hourse of firebug digging I found out how to get to open the markers window.
So following on your code after the change, I added this:
m.vars.markers[count].marker.openInfoWindow(html);
count = id of marker;
html = html you want to display in this marker.
In my case, the id points to the exact marker, since the view results to the map or in my right sidebar are equal, so no problems for me here! The html is also passed via the onclick javascript function.
Pfew, glad I found this one out!
Comment #4
texas-bronius commentedme too-- this looks like it will solve my related request of opening an infowindow in a node map linked from a node. Specifically relating to this solution, however, where does the
Drupal.gmap.getmapjs go? Custom node theme?Comment #5
texas-bronius commentedme too-- this looks like it will solve my related request of opening an infowindow in a node map linked from a node. Specifically relating to this solution, however, where does the
Drupal.gmap.getmapjs go?Comment #6
swentel commentedi've created my own function in a javascript file which I included in my theme
hope this helps!
Comment #7
assasin commentedAs the original poster - Thank you for this change. Sorry it took me so long to reply.
Pleased to see that others have benefited from your change also.
Regards
Ron Heywood
Comment #8
assasin commentedThat's wierd, I had this working yesterday and now it's broken "Drupal.gmap.getMap is not a function".
Not sure exactly what I did to cause it - I changed the maps default location to be centered and zoomed on the UK, but I don't see why that woud break anthing.
The gmap.js file has a getMap function showing in the code, but trying to call it gives a "Drupal.gmap.getMap is not a function" error. Wierd.
I cleared my cache, uninstalled the module and re-installed it, can't think of anything else to try?
Ron
Comment #9
assasin commentedThis is the strangest bug. I installed Drupal 5.1, 2007-01-29 on a test server and a dev server.
I setup a site on my dev server and copied it onto my test server.
I copied the database off my dev server onto the test server.
I updated the gmap API key on the test server and tested the map javascript.
Drupal.gmap.getMap was a function type and calling it returns the map as expected.
I loaded the modules admin page on the test server.
I then tested the map javascipt.
Drupal.gmap.getMap was "undefined".
Repeated 3 of 3.
Just trying to verify that the files on the dev server are identical, then I'll try it on a different server.
Regards
Ron
Comment #10
assasin commentedSo it turns out this bug was caused by an old version of Gmap in a /drupal/modules folder.
Sorry for cluttering your nodes!
Regards
Ron
Comment #11
bdragon commentedAhh.
No problem.
Comment #12
(not verified) commentedComment #13
kobnim commentedI am having the same problem.
I believe Drupal.gmap.getMap('locmap') gives me a reference to gmap's map object, but not to the googlemap itself. I wouldn't need a reference to the googlemap itself if I could figure out how to to add a marker to the map using the gmap api. But I have not succeeded in figuring that out.
So I ended up hacking the gmap.js code, and added a function that returns a reference to the googlemap. I am hoping there is a better solution.
Is there a way to get a reference to the googlemap object without hacking gmap.js?
Or is there a way to add (and delete) markers using the gmap api?
Comment #14
osu_bucks commentedHi, I'm a newbie, but learning. So far, I have a map of events that shows markers on the map where events held at. But I like to have lists of events on the left table or div, and map on the right. So, if I click on one of the event, the corresponding marker will pop up. After reading this post, I think this is what I need? Can anyone give me some more instruction on implementing please? :) I searched everwhere on google and drupal past few days.
Do I make a js file with the function above and put it in my theme folder (currently ubiquity). Do I call the file or the function from template.php?
thanks
Comment #15
andybrace commentedI am also having the same problem, when I use the code listed in this posting eg:
var m = Drupal.gmap.getMap("gmap--gmap0"); // gmap--gmap0 being the id of my map
the gmap object (m) is undefined.
I am not so good with JS so kobnim would it be possible for you to post or describe how you modified gmap.js so that you could access a reference to gmap object? that would really help me out.
thanks
Comment #16
kobnim commentedHi Andy,
Well, it's ugly, but here goes:
a. I added a variable to gmap.js which will be used to store a pointer to the googlemap object:
b. The googlemap object is created inside the function Drupal.gmap.addHandler():
Immediately after that line, I added this line, so that I could get my hands on the map from outside the function:
c. Finally, I added the following function to gmap.js, so that I could get my hands on the map from outside the gmap.js file:
As I said, it's ugly. Perhaps someone knows a better way?
- Mindy
Comment #17
matt_harrold commented+1 ... keen to see better than a patch (subscribing).
Comment #18
andybrace commentedGreat! thanks for your post mindy, ill give it a try and post back my findings.
Comment #19
magoo commented@kobnim and @andyb82
I installed Gmap (alpha version) this morning and quickly got the m.blabla() is not a function issue.
The solution in that version I found was simpler than hacking the .js of the module:
this:
returns something.
mis notundefined.but
did not work.
However, this
works.
hope it helps.
Comment #20
metaltoad commentedIf you can't access the map object, it may be that it hasn't been initialized yet. You can tell by outputting the content of the object:
m = Drupal.gmap.getMap('YOURMAPID');
output = "";
jQuery.each(m, function(i, val) {
output += i + ":" + val + "\n";
});
alert(output);
If you see Map is undefined, you'll need to boot it like so:
m = Drupal.gmap.getMap('YOURMAPID');
m.change("boot",-1);
From here on out you should be able to access the map object.
Comment #21
jimcarter commentedwhats the best way to find the map_id ? I'm just not sure if I'm looking in the right place.
Comment #22
topwaters commentedHi there,
The above method(s) don't work for me either. I can't output the content of the object as described. I really need access to the map to do a lot of custom interaction and event handling. I do want to implement GMap 100% as its javascript handlers (event handling system) develops and gets documented. Right now, if I can just use GMap for some of the basics and still be able to manipulate it with my scripts, I would be happy.
Appreciate any info regarding this in the meantime.
Thanks
Comment #23
pivica commentedHere is mine solution for getting map object that is initialized properly. For example I am using direction form with gmap and here is javascript for that:
and then somewhere in the code i have next couple of lines
Hopes this will help somebody.
Comment #24
topwaters commentedpivica, THANK YOU!!!!!
I'm finally getting somewhere with this :)
You obviously know what you're doing.
All the best!
Comment #25
beanjammin commentedpivica, let me 2nd what topwaters said. Thank-you very much!
Comment #26
diond commentedpivica you are a life saver!!!
Re-centering, zooming, adding markers on the fly ... GMap works like a charm now.
Thanks for sharing your knowledge.
Comment #27
prinds commentedIf anyone needs to remove or otherwise change the existing markers, remember to use the 'markersready' instead of 'ready'.
This way all GMarkers will be loaded in the m.vars.markers objects..
I hope this info can make someone happy..
Please let me know, if there's a better place for this kind of info..
Comment #28
stella commentedsubscribe
Comment #29
vosechu commentedprinds++
If you're using Gmap from a View you'll have to wait for markersready instead of ready. Views is going to want to put its own markers down and rework the map a little bit so changes you make at 'ready' are going to be overwritten.
Closing since this was a 5.x support request.
Comment #30
ju commentedThank you, Pivica!!! It really helps!
Comment #31
kndr#23 is very helpfull. It works. Thanks!
Comment #32
youngelpaso commentedThat was a lifesaver, especially Pivica's commentary. Cheers guys!
Comment #33
couloir007 commented+1 @pivica
Comment #34
dwatts3624 commentedThanks for this! I spent an hour troubleshooting this before finding this fix.
I changed my approach a bit with the following (adapting feedback in #27):
I guess it's really just a change in structure but seems to work well. I'm using the latest version of Chrome so I'm not sure what conditions have to be met for GBrowserIsCompatible() to be true...