Center Gmap on Node passed as argument in view
| Project: | GMap Module |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | reviewed & tested by the community |
Desired Behavior: I want each of my location-enabled nodes to show a zoomed-in map centered on that node's marker and also displaying any adjacent nodes of the same type (or whatever types I want). Users should also be able to zoom out and see nodes anywhere on the globe. Users are able to create location-enabled nodes on my site, so I want these maps to be created automatically, without anyone having to manually add any map-related information. Specifying the node location should be enough.
Problems with existing methods:
The built-in Location Map block only shows the marker for the current node, so that's inadequate.
A Gmap View allows me to display the right subset of nodes, but the map either uses the default location (if I specify nothing), zooms-out to include all nodes with no centering (if I use the autozoom behavior flag), or centers on the first node it processes (if I use the autocenter behavior flag). None of those cut it.
If I pass the NID to my view as an argument, the gmap correctly centers on the passed node, but it ignores my set filters and only displays the passed node. Basically, this is views replicating the Location Map, which I don't want.
Solution:
I'm sure there's a way to do what I want with php, but I like being able to only manipulate the gmap macros, so I created a new behavior flag for gmap that gets the current nid from the url and centers the gmap on that node.
I added the following to gmap_views.module directly following the views_autocenter code:
<?php
if ($thismap['#settings']['behavior']['views_currentnodecenter']) {
// Set center location for map to current node
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$thismap['#settings']['latitude'] = $node->locations[0]['latitude'];
$thismap['#settings']['longitude'] = $node->locations[0]['longitude'];
}
}
?>And then I can call it out in my Views argument code to get the correct map displayed:
<?php
$view->gmap_macro = "[gmap |id=mapblock123 |zoom=11 |behavior=+views_currentnodecenter ]";
?>
#1
is there a way to favorite a post? (other than commenting on it!)
#2
I'd appreciate a review (and ultimately inclusion) of the attached patch.
This adds an options for views to take a passed node ID and use that as the location for the map center.
As for the initial feature request - you can get about 90% of the way currently without this patch. When you pass the Node by arg, you are correct; Views simply shows that node. But. Make sure you check the "exclude" option for that arg and Views then lists all nodes which are NOT the one you passed. You can then get the Filter by Distance to link up to the arg and use the arg as the center of the filter.
I'm using this on a site I'm launching soon (in D6 with Views 2)... I cant strongly enough recommend upgrading to D6. Views 2 + Panels (2 or 3) makes life so much easier!
Also - Bdragon... Thanks SO much for all your efforts on GMap & Location! You've done an awesome job!
#3
will this work for users too?
#4
so i tried it and it appears to work.. but my map background is grey.
#5
Interesting...
It probably wont work for Users - but I imagine it wouldn't take a "lot" more development to make it work for users too...
#6
my map background was grey because I was using cck location. I love the concept but cck location has given me more headaches so going back to the good old fashion location node settings.
#7
subscribing
#8
Excellent valuable post. Thanks!!
#9
The description in #2 is great, but there is one part I can't get. If I manually type in the Lat and Long into the Distance filter everything works great, but I can't figure out how to
Sorry if I'm missing something obvious but a nudge in the right direction would be much appreciated.
#10
subscribing
#11
Same as #9 I don't understand how to filter by distance with nid argument.
I've tested patch #2.
View form appears as described but for me map isn't centered on node in argument.
In my view Macro settings is just : [gmap]
so it uses default gmap macro settings.
Maybe that's why lat/long replacement doesn't work.
In javascript code lat/long are always same and don't change depending on nid argument
==> "longitude": "57.5244140625", "latitude": "-20.255755447998474"
#12
OK I resolved the problem.
Patch #2 is incompatible with Location CCK.
That's why node's position isn't read properly and gmap can't be centered on it.
I also made a little correction.
Node indicated by argument Node:Nid will be exclude from gmap.
This node will only be usefull to center gmap.
I think it's better to show all nodes and to center on a particular one of them.
So I changed argument Node:Nid to Global:Null so that node to center on will be shown.
I will send my patch soon.
#13
Here is patch inspired from one in #2.
GMap views expose new checkbox parameter "Center on node argument" (default = FALSE).
User must add argument Global:Null which will receive Node:Nid value (let's say "NodeToCenterOn").
If Checkbox is set to TRUE and Nid is present in argument then center map is updated to lon/lat of NodeToCenterOn.
Location and Location CCK field would work.
Thx if you could make reviews and hope it could be commited.
#14
I've posted another patch to highlight markers with circle on mouse rollover event.
See #568976: Highlight marker on mouse rollover event
It would be great to also be able to highlight node's marker we center on (with different color).
I don't really know how to merge these 2 features.
Maybe someone could help.
#15
Thx for the patch submited on comment #13,
It works on 6.x-1.1-rc1 (with manual changes, I didn't apply the patch).
#16
The patch at #13 worked perfectly for me.
I have managed to place the view into a panel as well, without the need to configure panels to handle any arguments.
The only thing that remains for me is the same as #9 and #11.
The gmap being shown, if zoomed out, reveals all of the nodes which satisfy the views filter. There are a few hundred at the moment, but I have a few thousand more in the publishing queue. This will impact load times for sure. Has anybody been able to filter the nodes marked with the Location/Distance filter? The description in #2 is too vague for me to make sense out of.
#17
subscribing
#18
Subscribing, greetings, Martijn
#19
subscribing
#20