Geocoder Proximity Search - twist on map delivery
2 posts by 2 authors in: Forums > CMS Builder: Plugins & Add-ons
Last Post: August 13, 2012 (RSS)
By Mikey - August 10, 2012
So I reworked the delivery of the map pins on my proximity search to delivery map markers within a 50 mile radius, then changed the drop down to allow for a search of "at any distance". This way I can get my map started within a specific region I wish to target - while allowing the user to search and show results for any distance if they so desire. This is a simple change, but I thought it may be useful for anyone who may not have thought of delivering map content in this method.
I've marked the changes that effect what I'm talking about in red.
Hope someone finds this useful.
I've marked the changes that effect what I'm talking about in red.
Hope someone finds this useful.
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php require_once "../../../lib/viewer_functions.php"; ?>
<?php if (!@$GLOBALS['GEOCODER_PLUGIN']) { die("You must activate the Geocoder plugin before you can access this page."); } ?>
<?php
$kmOrMiles = 'miles'; // can be 'miles' or 'km'
// set default values
if (!array_key_exists('fromAddress', $_REQUEST)) { $_REQUEST['fromAddress'] = '10001'; } // this is the default address/postalcode if none entered
if (!@$_REQUEST['maxDistance']) { $_REQUEST['maxDistance'] = '50'; } // this will include all results
// get coordinates
list($myLat, $myLng) = geocodeAddress( @$_REQUEST['fromAddress'] );
// error checking
$errorsAndAlerts = '';
if (@$_REQUEST['search']) {
if (!@$_REQUEST['fromAddress']) { $errorsAndAlerts .= "No address entered!<br/>\n"; }
elseif (!$myLat || !$myLng) { $errorsAndAlerts .= "We couldn't determine your location, please try again!<br/>\n"; }
}
// get records
$addressRecords = array();
if ($myLat && $myLng) {
$maxDist = floatval(@$_REQUEST['maxDistance']);
list($addressRecords, $addressMetaData) = getRecords(array(
'tableName' => $GLOBALS['GEOCODER_SAMPLE_TABLENAME'],
'addSelectExpr' => geocoder_getSelectExprForDistance($myLat, $myLng, '_distance', $kmOrMiles), // adds '_distance' field to output records
'where' => geocoder_getWhereForDistanceWithin($myLat, $myLng, $maxDist, $kmOrMiles), // optimization: remove results outside of minimum bounding rectangle
'having' => "_distance <= " . $maxDist, // only show results within max distance
'orderBy' => 'ISNULL(_distance), _distance', // sort nearest records first -and- unknown or undefined distances last
));
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Proximity search with results sorted by distance</title>
<style type="text/css">
body, td { font-family: arial; font-size: 14px; }
</style>
</head>
<body>
<h1>Proximity search with results sorted by distance</h1>
<?php if (@$errorsAndAlerts): ?>
<div style="color: #C00; font-weight: bold; font-size: 12px; font-family: arial;">
<?php echo $errorsAndAlerts; ?><br/>
</div>
<?php endif ?>
Enter your address or postal code to see nearby results:<br/>
<form method="post" action="?">
<input type="hidden" name="search" value="1" />
<input type="text" name="fromAddress" value="<?php echo htmlspecialchars(@$_REQUEST['fromAddress']); ?>" size="30" />
<select name="maxDistance">
<option value="5000" <?php selectedIf(5000, @$_REQUEST['maxDistance']) ?> >at any distance</option>
<option value="5" <?php selectedIf( 5, @$_REQUEST['maxDistance']) ?> >within 5 miles</option>
<option value="10" <?php selectedIf( 10, @$_REQUEST['maxDistance']) ?> >within 10 miles</option>
<option value="25" <?php selectedIf( 25, @$_REQUEST['maxDistance']) ?> >within 25 miles</option>
<option value="50" <?php selectedIf( 50, @$_REQUEST['maxDistance']) ?> >within 50 miles</option>
<option value="100" <?php selectedIf(100, @$_REQUEST['maxDistance']) ?> >within 100 miles</option>
<option value="150" <?php selectedIf(150, @$_REQUEST['maxDistance']) ?> >within 150 miles</option>
<option value="200" <?php selectedIf(200, @$_REQUEST['maxDistance']) ?> >within 200 miles</option>
<option value="250" <?php selectedIf(250, @$_REQUEST['maxDistance']) ?> >within 250 miles</option>
<option value="300" <?php selectedIf(300, @$_REQUEST['maxDistance']) ?> >within 300 miles</option>
<option value="400" <?php selectedIf(400, @$_REQUEST['maxDistance']) ?> >within 400 miles</option>
<option value="500" <?php selectedIf(500, @$_REQUEST['maxDistance']) ?> >within 500 miles</option>
</select>
<input type="submit" value="Search" />
</form>
<?php if (@$_REQUEST['search'] && !$errorsAndAlerts): ?>
<ul style="line-height: 1.5">
<?php foreach ($addressRecords as $record): ?>
<li>
<a href="<?php echo $record['_link']; ?>">
<?php echo htmlspecialchars($record['address']); ?>,
<?php echo htmlspecialchars($record['city']); ?>,
<?php echo htmlspecialchars($record['state']); ?>
<?php echo htmlspecialchars($record['zipcode']); ?>,
<?php echo htmlspecialchars($record['country']); ?>
</a>
(<?php echo floor($record['_distance'] * 10) / 10; ?> <?php echo $kmOrMiles ?> away)
</li>
<?php endforeach ?>
<?php if (!$addressRecords): ?>
<li>No results found!</li>
<?php endif ?>
</ul>
<?php endif ?>
</body>
</html>
Re: [zick] Geocoder Proximity Search - twist on map delivery
By Jason - August 13, 2012
Hi Zick,
Looks good! Thanks for posting.
Looks good! Thanks for posting.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/
Jason Sauchuk - Project Manager
interactivetools.com
Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/