I don't know anything about PHP but this might help a lot of peoples request for a feature. Perhaps somebody who does know how to code can take this up.

The problem:
Each node is giving a location as a latitude field and a longitude field. How do I create a Views block that contains a list of other nodes ordered by proximity. For example, the site has a database of gas stations. I chose to look at a gas station node and in the block on the side is a Views unordered list of the closest gas stations to it ordered by proximity.

Currently:
You can specify a location and openlayers_proximity makes an ajax call to Google map services to determine the location and then you can create the proximity list from that. However, if each node has a lat and long field there is no reason to not take the information from each node to create the view.

First I use the context_set() function of the context.module to inside of node_api() to set several variables. Then I hacked openlayers_proximity to use context_get() function to access the lat and long variables of each node. If each node has geocoded fields with lat and long using the openlayers_geolocation.module you can just straight use node_api() here, so I'm not going to copy my implemtation of that function.

I have included a zip archive of the hacked to hell module which works very well for me.

This:

  /**
   * Distance from a node: rough calculation of a centroid for the node in
   * argument: centroid is calculate directly on values stored in the
   * proximity_index table.
   */
  function process_node_proximity() {
    $nid = $this->value['node'];
    
    // Get square boundaries.
    $x1 = db_result(db_query("SELECT lon FROM {openlayers_proximity} WHERE nid = %d ORDER BY lon ASC LIMIT 0, 1", $nid));
    $y1 = db_result(db_query("SELECT lat FROM {openlayers_proximity} WHERE nid = %d ORDER BY lat ASC LIMIT 0, 1", $nid));
    $x2 = db_result(db_query("SELECT lon FROM {openlayers_proximity} WHERE nid = %d ORDER BY lon DESC LIMIT 0, 1", $nid));
    $y2 = db_result(db_query("SELECT lat FROM {openlayers_proximity} WHERE nid = %d ORDER BY lat DESC LIMIT 0, 1", $nid));
    
    $delta = (sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2)) / 2) * OPENLAYERS_PROXIMITY_KM_PER_LAT; 
    $this->value['value'] += $delta;
    $this->value['min'] += $delta;
    $this->value['max'] += $delta;
    $this->value['lon'] = ($x1 + $x2) / 2;
    $this->value['lat'] = ($y1 + $y2) / 2;
    drupal_alter('process_node_proximity', $this->value);
  }

becomes

  /**
   * Distance from a node: rough calculation of a centroid for the node in
   * argument: centroid is calculate directly on values stored in the
   * proximity_index table.
   */
  function process_context_proximity() {
    $this->value['lat'] = $this->value['lon'] = NULL;
	$context = context_get();
    $this->value['lon'] = $context['node']['lon'];
    $this->value['lat'] = $context['node']['lat'];
	$this->value['location'] = 'location';
    drupal_alter('process_node_proximity', $this->value);
  }
CommentFileSizeAuthor
openlayers_proximity.zip26.64 KBAdam S