Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Mikey - April 2, 2011 - edited: April 2, 2011

Is it possible to create a geocoder "Show map with multiple addresses" pulling map records from 3 different "multi" menu types?

I've tried this code below, but it only returns one multi menu type's list of map records:
//////// geo records //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_sites' , 'available_buildings',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record

I've tried this code below, but it only returns one multi menu type's list of map records:
//////// geo records //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_sites',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record

//////// geo records //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_buildings',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record

Here's my full code:
<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'abcd/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/22/3331322/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
if (!@$GLOBALS['GEOCODER_PLUGIN']) { die("You must activate the Geocoder plugin before you can access this page."); }

////////// geo record //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'all_maps',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record
////////// end geo record //////////

////////// geo record //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_sites',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record
////////// end geo record //////////

////////// geo record //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_buildings',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record
////////// end geo record //////////


// load records
list($all_mapsRecords, $all_mapsMetaData) = getRecords(array(
'tableName' => 'all_maps',
//'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));

$all_mapsRecord = @$all_mapsRecords[0]; // get first record

// show error message if no matching record is found
// if (!$all_mapsRecord) {
// header("HTTP/1.0 404 Not Found");
// print "Record not found!";
// exit;
// }

?>
<?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'] = '50000'; } // 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'],
'tableName' => 'available_sites',
'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
));

list($addressRecords, $addressMetaData) = getRecords(array(
//'tableName' => $GLOBALS['GEOCODER_SAMPLE_TABLENAME'],
'tableName' => 'available_buildings',
'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=ISO-8859-1" />
<title>Map</title>

<!-- map -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {
var mapCanvasId = 'map_canvas';
var mapOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP };
var map = new google.maps.Map(document.getElementById(mapCanvasId), mapOptions);
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
<?php
foreach ($myRecords as $record) {
if (!$record['latitude'] || !$record['longitude']) { continue; }
$jsFunctionArgs = "{$record['latitude']}, {$record['longitude']}, {$record['num']}, '" .escapeJs($record['_link']). "'";
print " _geocoder_addMarker($jsFunctionArgs);\n";
}
?>

//
function _geocoder_addMarker(latitude, longitude, recordNum, detailLink) {
var latLng = new google.maps.LatLng(latitude, longitude);
var infowindowEl = document.getElementById('marker_infowindow_' + recordNum);
var marker = new google.maps.Marker({ map: map, position: latLng });
google.maps.event.addListener(marker, 'click', function() {
if (infowindowEl) {
infowindow.setContent(infowindowEl.innerHTML);
infowindow.open(map, marker);
}
else {
window.location = detailLink;
}
});
bounds.extend(latLng);
}

//
map.fitBounds(bounds);
}

</script>
<!-- map -->

</head>

<body onload="initialize();">

<!-- map -->
<?php $hasAddresses = array_filter(array_pluck($myRecords, 'latitude')); ?>
<?php if ($hasAddresses): ?>
<div id="map_canvas" style="width: 596px; height: 299px; "></div>
<?php endif ?>

<?php if (!$hasAddresses): ?>
<div style="width: 596px; height: 299px; border: 1px solid #000;">
<div style="text-align: center; padding-top: 135px">
<h3>No map available!</h3>
</div>
</div>
<?php endif ?>

<?php ?>

<div id="marker_details" style="display: none;">
<?php foreach ($myRecords as $record): ?>

<?php // marker_infowindow_### is the content displayed in the info-window on click ?>
<div id="marker_infowindow_<?php echo $record['num']; ?>">
<p><strong><?php echo htmlspecialchars( @$record['title']); ?></strong><br />
<?php echo htmlspecialchars( @$record['address']); ?><br/>
<?php echo htmlspecialchars( @$record['city']); ?>, <?php echo htmlspecialchars( @$record['state']); ?> <?php echo htmlspecialchars( @$record['zipcode']); ?><br/>
<a href="<?php echo $record['_link']; ?>">details</a></p><br />
</div>

<?php endforeach ?>
</div>
<!-- /map -->

<!-- search map -->
<h1>Proximity Search</h1>

<?php if (@$errorsAndAlerts): ?>
<div style="color: #C00; font-weight: bold; font-size: 12px; font-family: arial;">
<?php echo $errorsAndAlerts; ?><br/>
</div>
<?php endif ?>

<p>Enter your address or postal code to see nearby results:</p>

<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="">at any distance</option>
<option value="100" <?php selectedIf(100, @$_REQUEST['maxDistance']) ?> >within 100 miles</option>
<option value="50" <?php selectedIf(50, @$_REQUEST['maxDistance']) ?> >within 50 miles</option>
<option value="25" <?php selectedIf( 25, @$_REQUEST['maxDistance']) ?> >within 25 miles</option>
<option value="10" <?php selectedIf( 10, @$_REQUEST['maxDistance']) ?> >within 10 miles</option>
<option value="5" <?php selectedIf( 5, @$_REQUEST['maxDistance']) ?> >within 5 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['county']); ?></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 ?>
<!-- /search map -->

<p><?php if (!$all_mapsRecord): ?>
no records</p>
<?php endif ?>
<!-- /records -->

</body>
</html>

Re: [zick] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Jason - April 4, 2011

Hi Zick,

You should be able to do this by selecting records from a couple of different tables and then using array_merge to combine them into 1 array. You can then use that array to populate your map.

One issue I'm seeing with the code you posted:

//////// geo records //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_sites',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record

//////// geo records //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'available_buildings',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record


The first issue is that you're using the same variable name ($myRecords). What will happen is the second select will overwrite the first.

The other issue is using the "whereRecordNumberInUrl()" function. This won't work unless both records you want use the exact same record number. From the sounds of it, you're wanting to get all of these records, so you might want to try something like this:

//////// geo records //////////
list($availableSites, $availableSitesMetaData) = getRecords(array(
'tableName' => 'available_sites',

));

//////// geo records //////////
list($availableBuildings, $availableBuildingsMetaData) = getRecords(array(
'tableName' => 'available_buildings',
));


After that, you can combine the two arrays like this:

$myRecords = array();
$myRecords = array_merge($availableSites, $availableBuildings);


Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Mikey - April 4, 2011

Thanks Jason!
You're the man!!!!!

Works like a charm.

Re: [Jason] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Mikey - April 5, 2011

Jason,
Can I trouble you for a little more help. I'm trying to get the Proximity Search to work with this multi record map now, but can't seem to figure out the proper sequence of code under the $addressRecords = array().

Can you tell me how to combine the list so the proximity search will pull all the markers based on distance. Presently it only pulls one of the list's marker / distance and not all.

Thanks for any help or guidance you can provide.

Here's my latest code:

<?php header('Content-type: text/html; charset=utf-8'); ?>
<?php


// load viewer library
$libraryPath = 'abcd/lib/viewer_functions.php';
$dirsToCheck = array('/home/content/22/3331322/html/','','../','../../','../../../');
foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
if (!@$GLOBALS['GEOCODER_PLUGIN']) { die("You must activate the Geocoder plugin before you can access this page."); }

////////// geo record //////////
list($myRecords, $myMetaData) = getRecords(array(
'tableName' => 'all_maps',
'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));
$record = @$myRecords[0]; // get first record
////////// end geo record //////////

// load records
list($all_mapsRecords, $all_mapsMetaData) = getRecords(array(
'tableName' => 'all_maps',
//'where' => whereRecordNumberInUrl(1),
//'limit' => '1',
));

$all_mapsRecord = @$all_mapsRecords[0]; // get first record

// show error message if no matching record is found
// if (!$all_mapsRecord) {
// header("HTTP/1.0 404 Not Found");
// print "Record not found!";
// exit;
// }

//////// pull geo records //////////
list($availableSites, $availableSitesMetaData) = getRecords(array(
'tableName' => 'available_sites',
));

//////// pull geo records //////////
list($availableBuildings, $availableBuildingsMetaData) = getRecords(array(
'tableName' => 'available_buildings',
));

//////// pull geo records //////////
list($availableHomes, $availableHomesMetaData) = getRecords(array(
'tableName' => 'available_homes',
));

$myRecords = array();
$myRecords = array_merge($availableSites, $availableBuildings, $availableHomes);

?>

<?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'] = '50000'; } // 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' => 'available_sites',
'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
));

list($addressRecords, $addressMetaData) = getRecords(array(
'tableName' => 'available_buildings',
'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
));

list($addressRecords, $addressMetaData) = getRecords(array(
'tableName' => 'available_homes',
'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=ISO-8859-1" />
<title>Map</title>

<!-- map -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {
var mapCanvasId = 'map_canvas';
var mapOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP };
var map = new google.maps.Map(document.getElementById(mapCanvasId), mapOptions);
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
<?php
foreach ($myRecords as $record) {
if (!$record['latitude'] || !$record['longitude']) { continue; }
$jsFunctionArgs = "{$record['latitude']}, {$record['longitude']}, {$record['num']}, '" .escapeJs($record['_link']). "'";
print " _geocoder_addMarker($jsFunctionArgs);\n";
}
?>

//
function _geocoder_addMarker(latitude, longitude, recordNum, detailLink) {
var latLng = new google.maps.LatLng(latitude, longitude);
var infowindowEl = document.getElementById('marker_infowindow_' + recordNum);
var marker = new google.maps.Marker({ map: map, position: latLng });
google.maps.event.addListener(marker, 'click', function() {
if (infowindowEl) {
infowindow.setContent(infowindowEl.innerHTML);
infowindow.open(map, marker);
}
else {
window.location = detailLink;
}
});
bounds.extend(latLng);
}

//
map.fitBounds(bounds);
}

</script>
<!-- map -->

</head>

<body onload="initialize();">

<!-- map -->
<?php $hasAddresses = array_filter(array_pluck($myRecords, 'latitude')); ?>
<?php if ($hasAddresses): ?>
<div id="map_canvas" style="width: 596px; height: 299px; "></div>
<?php endif ?>

<?php if (!$hasAddresses): ?>
<div style="width: 596px; height: 299px; border: 1px solid #000;">
<div style="text-align: center; padding-top: 135px">
<h3>No map available!</h3>
</div>
</div>
<?php endif ?>

<div id="marker_details" style="display: none;">
<?php foreach ($myRecords as $record): ?>

<div id="marker_infowindow_<?php echo $record['num']; ?>">
<p><strong><?php echo htmlspecialchars( @$record['title']); ?></strong><br />
<?php echo htmlspecialchars( @$record['address']); ?><br/>
<?php echo htmlspecialchars( @$record['city']); ?>, <?php echo htmlspecialchars( @$record['state']); ?> <?php echo htmlspecialchars( @$record['zipcode']); ?><br/>
<a href="<?php echo $record['_link']; ?>">details</a></p><br />
</div>

<?php endforeach ?>
</div>
<!-- /map -->

<!-- search map -->
<h1>Proximity Search</h1>

<?php if (@$errorsAndAlerts): ?>
<div style="color: #C00; font-weight: bold; font-size: 12px; font-family: arial;">
<?php echo $errorsAndAlerts; ?><br/>
</div>
<?php endif ?>

<p>Enter your address or postal code to see nearby results:</p>

<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="">at any distance</option>
<option value="150" <?php selectedIf(150, @$_REQUEST['maxDistance']) ?> >within 150 miles</option>
<option value="100" <?php selectedIf(100, @$_REQUEST['maxDistance']) ?> >within 100 miles</option>
<option value="50" <?php selectedIf( 50, @$_REQUEST['maxDistance']) ?> >within 50 miles</option>
<option value="25" <?php selectedIf( 25, @$_REQUEST['maxDistance']) ?> >within 25 miles</option>
<option value="10" <?php selectedIf( 10, @$_REQUEST['maxDistance']) ?> >within 10 miles</option>
<option value="5" <?php selectedIf( 5, @$_REQUEST['maxDistance']) ?> >within 5 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['county']); ?></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 ?>
<!-- /search map -->

<p><?php if (!$all_mapsRecord): ?>
no records</p>
<?php endif ?>
<!-- /records -->

</body>
</html>

Re: [Jason] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Mikey - April 5, 2011 - edited: April 5, 2011

Thanks for the help Jason!
Works great!!!!

The next task I need to accomplish is to create check boxes to show and hide markers based on the record/map's association with categories.

For example, when I create a new record for a Home, Site or Building, I enter in the address for that record, then check off on a list of categories that the record is associated with, like "Home, Building or Sites, etc.

I then need to be able to make this multi map display markers based on whether they are a Home, Site and/or Buildings. So the site visitor who is only interested in "Homes" can un-check Buildings and Sites... thereby revealing only map markers for "Homes" and vise-versa.

Is it possible to make this work with this multi-map and the method it is calling records from multiple multi-list?

Ultimately, I intend to build this to have additional marker layers that can be turned on and off to also reveal, schools, bike paths, railroads, airports, etc.

Thanks, Michael

Re: [Jason] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Mikey - August 9, 2012 - edited: August 9, 2012

Jason,
I was working on the Geocoder map that merges various multi pages together from different records and I noticed that some of the map markers have the same record NUM and therefore the map marker list the first record's info on the map marker's info window and links to the first instance of the record NUM. Any suggestions on how to make the merging of records retain their correct information?

You can see where the map markers have the same record NUM below - which I colored:

<!-- map -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function initialize() {
var mapCanvasId = 'map_canvas';
var mapOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP };
var map = new google.maps.Map(document.getElementById(mapCanvasId), mapOptions);
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
_geocoder_addMarker(34.2057166, -79.2544902, 6, 'sites_details.php?Industrial-Site-6');
_geocoder_addMarker(34.2057166, -79.2544902, 4, 'sites_details.php?Industrial-Site-4');
_geocoder_addMarker(34.0649562, -79.3194611, 7, 'sites_details.php?Site-7');
_geocoder_addMarker(34.178216, -79.4006069, 5, 'sites_details.php?Industrial-Park-5');
_geocoder_addMarker(34.2772395, -79.4780422, 3, 'sites_details.php?Logistics-Center-3');
_geocoder_addMarker(34.0649767, -79.761812, 10, 'buildings_details.php?Paper-10');
_geocoder_addMarker(34.1757309, -79.4518708, 8, 'buildings_details.php?TECH-8');
_geocoder_addMarker(34.1818424, -79.3935941, 9, 'buildings_details.php?Sportscenter-9');
_geocoder_addMarker(34.2388054, -79.3957206, 5, 'buildings_details.php?Call-Center-5');
_geocoder_addMarker(34.2001583, -79.3494484, 7, 'buildings_details.php?Building-7');
_geocoder_addMarker(34.1755675, -79.4508923, 3, 'buildings_details.php?Candies-3');
_geocoder_addMarker(34.1940552, -79.3489407, 4, 'buildings_details.php?Warehouse-4');
_geocoder_addMarker(34.1845392, -79.3553842, 1, 'map_directions.php?1');
_geocoder_addMarker(34.2015747, -79.2449732, 20, 'gallery_details.php?Medical-20');
_geocoder_addMarker(34.1955465, -79.8024289, 19, 'gallery_details.php?University-19');
_geocoder_addMarker(34.2442541, -79.8177072, 18, 'gallery_details.php?College-18');

//
function _geocoder_addMarker(latitude, longitude, recordNum, detailLink) {
var latLng = new google.maps.LatLng(latitude, longitude);
var infowindowEl = document.getElementById('marker_infowindow_' + recordNum);
var marker = new google.maps.Marker({ map: map, position: latLng });
google.maps.event.addListener(marker, 'click', function() {
if (infowindowEl) {
infowindow.setContent(infowindowEl.innerHTML);
infowindow.open(map, marker);
}
else {
window.location = detailLink;
}
});
bounds.extend(latLng);
}

//
map.fitBounds(bounds);
}

</script>
<!-- map -->

Re: [zick] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Jason - August 9, 2012

Hi Zick,

Yes, you can easily run into recordNum collisions when bringing in records from multiple sections. One way around this would be to append table names to the identifier as well.

For example, in the _geocoder_addMarker function:

function _geocoder_addMarker(latitude, longitude, recordNum, $tableName, detailLink) {
var latLng = new google.maps.LatLng(latitude, longitude);
var infowindowEl = document.getElementById('marker_infowindow_' + recordNum + "_" + $tableName);
var marker = new google.maps.Marker({ map: map, position: latLng });
google.maps.event.addListener(marker, 'click', function() {
if (infowindowEl) {
infowindow.setContent(infowindowEl.innerHTML);
infowindow.open(map, marker);
}
else {
window.location = detailLink;
}
});
bounds.extend(latLng);
}


Then, you would change how you were outputting your records into the javascript like this:


<?php
foreach ($myRecords as $record) {
if (!$record['latitude'] || !$record['longitude']) { continue; }
$jsFunctionArgs = "{$record['latitude']}, {$record['longitude']}, {$record['num']}, '" .escapeJs($record['_tableName']). "', '" .escapeJs($record['_link']). "'";
print " _geocoder_addMarker($jsFunctionArgs);\n";
}
?>


Finally, you'll need to make sure that tablename was appended to the id of your info div.

For example:

<div id="marker_details" style="display: none;">
<?php foreach ($myRecords as $record): ?>

<?php // marker_infowindow_### is the content displayed in the info-window on click ?>
<div id="marker_infowindow_<?php echo $record['num']; ?>_<?php echo $record['_tableName'];?>">
<h3><?php echo htmlspecialchars( @$record['address']); ?></h3>
Add any extra content you like here...<br/>
<a href="<?php echo $record['_link']; ?>">details</a>
</div>

<?php endforeach ?>
</div>


Give this a try and let me know if you run into any issues.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

Re: [Jason] Geocoder - show map with multiple addresses, pulling map records from 3 multi menu types

By Mikey - August 9, 2012

Jason - YOU ROCK! Thanks for the help with this.