I am trying to get access to the latitude and longitude variables to use in a block on a page with content which is geolabelled with location.module. I notice that the lat & lon variables are in the meta headers of the page. I am trying

<?php
$longlat=$node->location['longitude'] . ', ' . $node->location['latitude'];
?>

, have searched high and low for the answer and still am unable to get my hands on this info. Would very much appreciate a pointer in the right direction... Thanks :)

Comments

ankur’s picture

Sorry, you're right. This should be included in the API documentation. In any case, you can pull the lat and lon for a loaded node (if one has been set explicitly or determined implicitly by postal code) by using

$latitude = $node->location['lat'];
$longitude = $node->location['lon'];

It looks like you were using $node->location['longitude'] and $node->location['latitude'] instead.

-Ankur

paulr’s picture

Priority: Critical » Normal

Thanks Ankur, I'm not quite there however... Even using lat & lon I am getting no results. The loaded node has lat lon values explicitly set, and they are showing correctly in the meta tags on the page. Trying to retrieve them in on-page php is currently fruitless. I am assuming that for a page with location data the location information is loaded into $node already, am I correct in this assumption?

$node->location['lat'];
$node->location['lon']

are returning empty. If this data is not loaded into $node, can you point out how to load it, otherwise I'm stumped. Thanks again for your help.

ankur’s picture

Ah. I didn't realize you were trying to this this in a block. To this end, you'll have to explicity call node_load() for the node (or nodes) that are being loaded on the page.

Usually, blocks are not aware of the node(s) being listed or viewed in the current page, so you'll have to figure out a way of grabbing the node nids. If you have one nid, then code could be as simple as:

$node = node_load(array('nid' => $nid));
$lat = $node->location['lat'];
$lon = $node->location['lon'];

How you get this $nid variable is up to you and depends on what you're doing. If you're trying to get the nid of node currently being displayed, you may have to use the $_GET['q'] to grab the URL to figure out the nid. Of course, you might run into problems if a particular node happens to use a URL alias (in which case you might have to write a db_query to the path table to figure out the real nid from the alias.... and keep in mind that aliases aren't exclusively used for nodes).

If you are trying to use multiple nids because you want all the lat/lons for the nodes on a page listing teasers or something, you might have to replicate the query that generates that page and iterate through the nids in the result set.

Finally, you can try, from within your block's PHP code, to grab the RSS feed of the page being loaded (the RSS feed for a node will contain the lat/lon, however, the RSS feed for a *listing* of nodes may require some hacks if I'm not mistaken) and extract the lat/lon from the RSS feed to use in your block. This is discouraged because it is not quite as efficient as pulling the data from the database. (Since pulling the feed means that every page request to a page on your site that uses this block will trigger another page request from the web server to itself for the related RSS feed).

-Ankur

paulr’s picture

Ankur,

Thank you so much for your help with this. In the interests of making this thread come to a conclusion, I'm attaching the code which successfully lets me use location.module's latitude and longitude of a node to generate a map in a block using the gmap module's macro maker.

<?php
$path = drupal_get_normal_path($_GET['q']);
list($node, $nid) = array_values(explode('/', $path));
if ($node == 'node') {
  $node = node_load(array('nid' => $nid));
  $latitude = $node->location['lat'];
  $longitude = $node->location['lon'];
  $longlat = $longitude . ', ' . $latitude;
  $mygmap=array('id'=>'blockgmap',
          'width'=>150,
          'height'=>300,
          'zoom'=>'11',
          'controls'=>'small',
          'center'=>$longlat,
          'type'=>'hybrid',
          'points'=>$longlat);
  print gmap_from_var($mygmap);
}
?> 

Thanks again,
Paul.

sethcohn’s picture

Paul, thanks for the code... It looks like we really need to have a better way than url parsing for a block to know what node is currently displaying.... (I modified the concept you used to make the Amazon block display truly related items to the node instead of just random ones (unless there is no related item, then we fall back to random))

bdragon’s picture

Status: Active » Closed (fixed)
if (arg(0) == 'node' && is_numeric(arg(1))) {
  if ($node = node_load(arg(1)) {
    ...
  }
}

is actually a pretty common pattern nowadays...