Limit a Geocoder multi pin map to a radius from a visitor entered Zip/Postal code

By daniel - November 14, 2018

Hi Jerry,

The general idea of what you'll want to do is add an input field to your page for users to enter their ZIP/Postal code, and then use the code from sample_search.php to apply the proximity limits. The relevant code is here:

  // get geocoding data
  list($myLat, $myLng) = geocodeAddress( @$_REQUEST['fromAddress'] );
  $kmOrMiles           = 'miles'; // can be 'miles' or 'km'
  $geoOptions          = geocoder_getOptions($myLat, $myLng, @$_REQUEST['maxDistance'], $kmOrMiles);

  // get records
  list($addressRecords, $addressMetaData) = getRecords(array(
    'tableName'     => $GLOBALS['GEOCODER_SAMPLE_TABLENAME'],
  ) + $geoOptions);  // geoOptions WILL NOT override the above options

This getRecords() call can likely replace the existing one for the page and should work with the existing code to output the markers (though the variable name may need to be changed/updated), and you may want to hard-code in a max distance rather than using a form value. I'd also recommend adding an "if" condition to check for the user's entered value so that it doesn't attempt to limit by proximity if no value is supplied. A simple way to do this would be to set $geoOptions as an empty array first, something like this:

$geoOptions = [];
if (!empty( $_REQUEST['fromAddress'] )) {
  // get geocoding data
  ...
}

// get records
...

There's also some error checking code in sample_search.php that you may find useful.

Hope that helps!

Cheers,

Daniel
Technical Lead
interactivetools.com

Thanks for the thorough response Daniel,

I'll implement this first thing in the morning.

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Hi Daniel,

Got everything working as it should and added some MySQL where arguments to exclude admin and junior admin records from being included in the map.

I’m still stuck on one issue, and that is excluding records that have not been updated in 6 months from being included in the search results.

I’ve been doing this successfully as part of a foreach loop with if statements but can’t figure out how to translate the concept to MySQL

Here’s what I’m up to in the MySQL at the moment:

// get records
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => $GLOBALS['GEOCODER_SAMPLE_TABLENAME'],
'where' => " (isAdmin = '0' OR isAdmin = '') AND (notAdmin = '0' OR notAdmin = '')",


) + $geoOptions); // geoOptions WILL NOT override the above options

And here’s how I’ve been handling the exclusions in the foreach situation.

<?php foreach ($myRecords as $record): ?>
<?php
$updateUnixTime = strtotime( $record['updatedDate'] ); // seconds since 1970
$sixMonths = time() - (180*60*60*24) ;
$sixMonthsGone = $updateUnixTime > $sixMonths;

?>
<?php if(@$_REQUEST['save'] && !$record['isAdmin'] == ‘1' && !$record['notAdmin'] == '1' && ( $sixMonthsGone) ):?>
<br />
Display the valid record information here.

<?php endif ?>

<?php endforeach ?>

I know it's asking a lot, but could you offer some of your expert guidance on this?

Thanks,

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - November 15, 2018

Hey Jerry,

It sounds like you're asking how to exclude the older records within the WHERE clause, rather than skipping them with PHP? If that's the case, you can do so relatively easily, as you can use comparison operators on datetime values in MySQL so long as it's the right format ("yyyy-mm-dd hh:mm:ss"). This format can be produced in PHP with the date() function format "Y-m-t H:i:s".

Also, a helpful tidbit is that strtotime() can use relative date formats (http://php.net/manual/en/datetime.formats.relative.php), so you can use things like strtotime( "6 months ago" ); to save yourself some math.

Putting that all together will give you something like this:

$sixMonthsAgo = strtotime( '6 months ago' );

// get records
list($myRecords, $myMetaData) = getRecords(array(
  'tableName' => $GLOBALS['GEOCODER_SAMPLE_TABLENAME'],
  'where' => " (isAdmin = '0' OR isAdmin = '') AND (notAdmin = '0' OR notAdmin = '') AND updatedDate > '" . date('Y-m-t H:i:s', $sixMonthsAgo) . "'", 
) + $geoOptions); // geoOptions WILL NOT override the above options

Hope that helps!

Cheers,

Daniel
Technical Lead
interactivetools.com

Wow! Thanks Daniel,

So this will look at all the records updatedDate field and if the date is more than 6 months old not include that record in the list?

Seems really easy (when you know what you're doing!)...

Jerry Kornbluth

The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

By daniel - November 15, 2018

Hey Jerry,

I'd make sure to do some thorough testing to make sure that it's limiting the records appropriately, but yes that's the idea!

Glad to help.

Daniel
Technical Lead
interactivetools.com