Gmap api / function for calling from argument handling.

spydor - June 20, 2007 - 13:32
Project:GMap Module
Version:5.x-1.x-dev
Component:Code
Category:support request
Priority:normal
Assigned:Unassigned
Status:closed
Description

Maybe I'm just a knuckle head but I'm trying to display a gmap by calling it from a template. I've created a cck content type called "outfitters" and currently I have a block view of the gmap that I display above of the content, but I would like to change the dimensions of the map and integrate it better into the content. Is there an api / function I can call with the lat / long and settings such as width, height, etc. that will display the map from within the template?

I want to get away from having the map above the content and instead show it themed better in the content.

Here is what I currently have.
http://www.wildpaddle.com/bwca/outfitters/boundary-waters-experience

The solution might be obvious, but I'm just not seeing it.

#1

tom_o_t - June 20, 2007 - 13:50

Are you using a GMap macro in your .tpl.php template file to display the map? If that's the case then you can set the width and height of the map using that. And just put the Macro in your chosen place in the template if you don't want the map at the top of the page.

The other easy way to change the size of the gmap is to use CSS to set the width and height of the div containing the map.

Hope this helps.

#2

spydor - June 20, 2007 - 14:00

I would like to use the gmap in the tpl.php, but I'm not sure how.

Just pasting this in the tpl.php file doesn't work.
[gmap markers=sunday::47.98256841921402,-90.791015625 |zoom=8 |center=47.956823800497475,-91.3568115234375 |width=200px |height=200px |id=map |control=Large |type=Map]

How would I include that macro in the tpl.php file?

My current set up: I created a view that produces a block. This view is simply a gmap view. I then created a region called "map" and assigned the block to this region.

#3

tom_o_t - June 20, 2007 - 15:07

Just a couple of quick steps will get that working in your template. First you need to call a function to parse the macro, then you need to call a second function to display the map:

<?php
$macro
= '[gmap markers=sunday::47.98256841921402,-90.791015625 |zoom=8 |center=47.956823800497475,-91.3568115234375 |width=200px |height=200px |id=map |control=Large |type=Map]';

$mymap = gmap_parse_macro($macro);

print
gmap_draw_map($mymap);
?>

And instead of hard-coding the marker into the Macro you can get the latitude and longitude for that node in the normal way.

This is how it works for Drupal 4.7, and I'm making the assumption it's the same for Drupal 5. I'm sure someone will be able to help if this isn't the case.

There are lots of other ways to do this. You can insert a Macro into your view, but this requires hacking the code a bit. Details at http://drupal.org/node/74920. Also, if you're using views to create a block there are other ways of inserting that block into a page, which will give you more flexibility. See http://drupal.org/node/26502

Cheers.

#4

spydor - June 20, 2007 - 16:17

gmap 5 is different from 4.7. The API as it was in 4.7 is basically gone. When I implement your code it spits this out.

GMap Module: DRAW_MAP CALLED -- This function is scheduled for deletion!

What is the alternative in 5.x to the 4.7 API?

#5

tom_o_t - June 20, 2007 - 16:38

Sorry, my ability to help here is pretty limited.

Looking at API.txt in the GMap module for 5.x it still uses the gmap_draw_map function:

<?php
$mymap
=array('id' => 'mymap',
            
'latitude' => '49.19258642226091',
            
'longitude'=>' -123.17647933959961',
            
'zoom' => 13,
            
'width' => '100%',
            
'height' => '400px',
            
'type' => 'Satellite',
            
'shapes' => $shapes,
            
'markers' => $mymarkers);
             
gmap_draw_map($mymap);
?>

though in README.txt it says:

A gmap can also be inserted into any page or template by using either the
macro text and the function gmap_from_text($macro); or using the
function gmap_from_var($gmapvar); where $gmapvar is an associative array.

So if none of those work, then I can only suggest sticking with your current method of creating the map as a block in views, and then using the techniques I linked to earlier to customise the size and placement on your page.

Hopefully someone who's used GMap on 5.x will be able to help more though.

#6

spydor - June 20, 2007 - 16:50

Ok, kind of answering my own question.

1. Create a block with views using the "Gmap View" View Type.

2. In the View under Arguments > Argument Handling Code you'll want to insert

$view-&gt;gmap_macro = '[gmap |zoom=8 |width=590px |height=300px |behavior=+nodrag +autozoom]';

Tweak to your liking.

3. In the template file (tpl.php) place this

<?php

$block
= module_invoke('views', 'block', 'view', REPLACE_WITH_DELTA_NUMBER);
print
$block['content'];
?>

obviously insert the correct views delta number for REPLACE_WITH_DELTA_NUMBER.

The key is the module_invoke function. This seems to work for me, YMMV. Thanks for helping me think this through.

#7

spydor - June 20, 2007 - 18:58
Status:active» closed

I'm a knob. See
http://drupal.org/node/138034

Bascially,

<?php

$mymap
=array('id' => 'outfittermap',
            
'latitude' => $location['latitude'],
            
'longitude'=> $location['longitude'],
            
'zoom' => 13,
            
'width' => '300px',
            
'height' => '300px',
            
'type' => 'Map',
);
$outfittermap = theme('gmap', array('#settings' => $mymap));
print
$outfittermap;
?>

I'm sure other variables are available. I threw this in the tpl.php file and YEAH!!!!

#8

robomalo - July 22, 2007 - 05:10

This works perfectly except the location marker doesn't print on the map. Am I forgetting something or is there a setting?

#9

webavant - August 21, 2007 - 17:46

robmalo: I'm not sure if you have figured this out yet, but for anyone else wondering, modify Spidor's snippet to show the marker also:

<?php
$homes
=array('id' => 'outfittermap',
            
'zoom' => 13,
            
'width' => '300px',
            
'height' => '300px',
            
'type' => 'Map',
            
'latitude' => $location['latitude'],
            
'longitude'=> $location['longitude'],
            
'markers' =>
               array( array(
'markername' => '',
                    
'latitude' => $location['latitude'],
                    
'longitude' => $location['longitude']
               ))
);
$outfittermap = theme('gmap', array('#settings' => $homes));
print
$outfittermap;
?>

#10

coupet - August 25, 2007 - 19:58

should we add instructions to README file and documentation?

#11

kungfu71186 - October 1, 2008 - 19:22

You can wrap

<?php

?>
tags around this in content templates if you prefer to use that and it will load the map.

#12

carpintero - October 7, 2008 - 23:30
Title:Gmap api / function for calling from a template.» Gmap api / function for calling from argument handling.

i am able to insert the following code into the "argument handling code" area of my gmap view, and it works:

<?php
$view
->gmap_macro = "[gmap |center=13, 170 |zoom=6 |height=250px |tcontrol=off|control=Small |type=Terrain]";
?>

but what i want to do is have the center be dynamic. so i made this code, which finds the latitude and longitude of this node.

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$nid = $node->nid;
$query = "SELECT l.latitude, l.longitude FROM {location} l INNER JOIN {node} n ON l.eid = n.vid WHERE n.nid=%d";
$result db_query($query, $nid);
$location = db_fetch_object($result);
print
'Latitude: ' . $location->latitude . ' and ' . $location->longitude;
}
?>

i have tested that in the body of the node and i know it works.

so i would think the following code, placed into the "argument handling" section (without the php tags) *should* work. but it doesn't:

<?php
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
$nid = $node->nid;
$query = "SELECT l.latitude, l.longitude FROM {location} l INNER JOIN {node} n ON l.eid = n.vid WHERE n.nid=%d";
$result db_query($query, $nid);
$location = db_fetch_object($result);
$view->gmap_macro = '[gmap | zoom=6 |height=250|center='.$location->latitude.','.$location->longitude.' |control=Small|tcontrol=off|type=Terrain]';
return
$view;
}
?>

any suggestions? thanks! (drupal 5.7, gmap 5.x-1.x-dev)

#13

Quixote123 - November 13, 2008 - 08:16

I tried out your code to retrieve the lat & long from the current node, but I get an error:

user warning: Unknown column 'l.eid' in 'on clause'
query: SELECT l.latitude, l.longitude FROM location l INNER JOIN node n ON l.eid = n.vid WHERE n.nid=223 in C:\myurl\includes\database.mysql.inc on line 174.

#14

Quixote123 - November 16, 2008 - 00:42

I created a new behavior flag to allow dynamic centering based on current node, see http://drupal.org/node/334846

 
 

Drupal is a registered trademark of Dries Buytaert.