Jump to content
spadeals

Store Locator w/ Google Maps

Recommended Posts

I am in the process of integrating the phpGoogleStoreLocator into my osCommerce shopping cart. You can demo it here at http://www.spadeals.net/locator.php You can enter sample zip code 80439 to see working results. I have gotten it pretty much fully integrated except for a few things that I am asking for your help with.

 

Here is the locator.php file that I am having trouble with:

 

<?php
/*
 $Id: conditions.php,v 1.22 2003/06/05 23:26:22 hpdl Exp $

 osCommerce, Open Source E-Commerce Solutions
 http://www.oscommerce.com

 Copyright (c) 2003 osCommerce

 Released under the GNU General Public License
*/

 require('includes/application_top.php');

 require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_LOCATOR);

 $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_LOCATOR));

include "includes/locator_inc.php";

$version = "1.0.5-dev";
//Get all the settings out of the settings table.
$zoom = get_setting("zoom", $db);//0 to 17, 17 being Country view, 0 being Street closeup
$width = get_setting("width", $db);//Results Map Width
$height = get_setting("height", $db);//Results Map Height
$radius = get_setting("radius", $db);//25 Mile radius of the zipcode that was entered in the search box.

//ICONS, if you want to use the default Google icon, just set these 2 defines to blank like:
//define("locations_marker_icon", "");
//CAUTION: Image size recommendations are 20x34.  Image sizes can be whatever you like, but 20x34 is a good size.
define("locations_marker_icon", get_setting("locations_marker_icon", $db));//Locations icon
define("zipcode_marker_icon", get_setting("zipcode_marker_icon", $db));//Zipcode icon (this is the zip the user used as their zipcode)
define("display_image_in_results", get_setting("display_image_in_results", $db));//This will display the image in the results table.  yes/no
define("display_image_in_balloon", get_setting("display_image_in_balloon", $db));//This will display the image in the little red balloons on the map. yes/no
$google_key = get_setting("google_key", $db);//Google API Key

//Call home and let the author know that someone is actually using his code;)
//Feel free to remove this if you like.  It only does it once whether it was successful or not anyways so you
//dont need to remove this as it wont affect a thing.
if (!$home = get_setting('called_home', $db))
{
insert_setting('called_home', '1', $db);
//Turn off error reporting so we dont scare anyone outa their wits.  Then set it back once we've tried to
//contact home.
$a = error_reporting(0);
$b = strtolower(strtok($_SERVER['SERVER_PROTOCOL'], '/')).'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
fopen("http://crystaldawn.net/calledhome.php?referrer=$b", "r"); 
error_reporting($a);
}


if (!is_file("includes/locator_inc.php"))
{
echo "ERROR!  Unable to find/read locator_inc.php.  Please make sure this file exists and is readable by Apache.  If you did not use install.php, then take a look at install.php and you can figure out what will need to go in this file.";
exit;
}

//See if we even have a zipcode DB filled in
if (!has_zipcodes($db))
{
echo "ERROR: Your database appears to be missing zipcodes. If you have not entered any locations yet, please do so with the Administration Panel.  If you DO have locations, then they may not have been entered with the panel (you imported them from an existing DB or something), and you have not yet initialized the zipData with any zipcodes yet.  You can initialize the zipData table by uncommenting the get_lat_lon() function as described in the FAQ in this file.  Just do a search for it, and you can read how it works, its very simple.";
}
//CSS STYLE SHEET for Map Tooltip text ?>
<style type="text/css">

	div.markerToolTip 
{
text-align: center;
	white-space: nowrap;
	margin: 0;
	padding: 2px 0.5ex;
	border: 1px solid #555;
	font-weight: bold;
	background: #fff;
	opacity: .80;
	-moz-opacity: .80;
	filter: alpha(opacity=80);
	}
</style>
<? //END STYLE SHEET

//Uncommenting this is no longer necessary unless you do not use the admin panel to enter locations.  If you do not
//use the panel, then uncomment this to give your locations some coordinates.  After they have coordinates, be sure
//to comment this again.  It will greatly increase your query times.  Querys can be slow if this is left uncommented.
get_lat_lon('','',$db);

if (!$_POST['submit'])
{
//This is mainly to display the installation successful msg, but you can use it for other things as well.
echo $_GET['msg'];
if (!$_GET['msg'] && is_file("install.php"))
{
 echo "WARNING! The install.php file still exists! You SERIOUSLY need to delete it if you have already got phpGoogleStoreLocator installed and working!<br><br>";
}
?>

<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="0" cellpadding="0">
 <tr>
<!-- body_text //-->
<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
  <tr>
	<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
	  <tr>
		<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
	  </tr>
	</table></td>
  </tr>
  <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
  </tr>
  <tr>
	<td class="main">

<form method=post action=<?echo $_PHP_SELF;?>>
<center><font size="5">Please Enter Your Zip Code:</font><p>
<input type="text" name="zipOne"><p>
<font size="3">This will search for all Spa Dealerships and Service Technicians within a 50 mile radius of your zip code.</font></center>

	</td>
  </tr>
  <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
  </tr>
  <tr>
	<td><table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBox">
	  <tr class="infoBoxContents">
		<td><table border="0" width="100%" cellspacing="0" cellpadding="2">
		  <tr>
			<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
			<td align="right"><input type=image src="includes/languages/english/images/buttons/button_continue.gif" name=submit value="Find Store"></td>
			<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
		  </tr>
		</table></td>
	  </tr>
	</table></td>
  </tr>
</table></td>
<!-- body_text_eof //-->
 </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>

<?
exit;
}

$db = new db_sql;
$zipLoc = new zipLocator;
$zipOne = addslashes($_POST['zipOne']);
$country = zip2country($zipOne); //Which country are we from?
if (!$country)

{
echo "Your zipcode does not seem to be valid for the countries that we support.";
exit;
}
$zipArray = $zipLoc->inradius($zipOne,$radius);
if (!$zipArray)
{
echo "There are no stores within $radius miles from this zipcode: $zipOne.";
 exit;
}

foreach ($zipArray AS $zipcode)
{
	$query = "SELECT * from locator_locations where zip = '$zipcode'";
	$result = mysql_query($query);
	$rows = mysql_fetch_array($result);
	if ($num_rows = mysql_num_rows($result) > 0)
	{
	$locations .= get_locations($zipcode, $zipOne, '', $db);
	}

}

$locations = split("~~", $locations);
$locations = sort_by_distance($locations, $zipOne, $country, $radius);
if (sizeof($locations) == 1)
 {
  echo "There are no stores within $radius miles of your zipcode.<br><br>";
  exit;
 }
$locations = create_locations_array($locations);
$locations = split("~~", $locations);
$center_lat_lon = get_lat_lon($zipOne, $country, $db);
$center_lat_lon = split("##", $center_lat_lon);
$center_lat = $center_lat_lon[0];
$center_lon =$center_lat_lon[1];

?>







<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<base href="<?php echo (($request_type == 'SSL') ? HTTPS_SERVER : HTTP_SERVER) . DIR_WS_CATALOG; ?>">
<link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<!-- header //-->
<?php require(DIR_WS_INCLUDES . 'header.php'); ?>
<!-- header_eof //-->

<!-- body //-->
<table border="0" width="100%" cellspacing="0" cellpadding="0">
 <tr>
<!-- body_text //-->
<td width="100%" valign="top"><table border="0" width="100%" cellspacing="0" cellpadding="0">
  <tr>
	<td><table border="0" width="100%" cellspacing="0" cellpadding="0">
	  <tr>
		<td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
	  </tr>
	</table></td>
  </tr>
  <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
  </tr>
  <tr>
	<td class="main">

<link href="/style/hans.css" rel="stylesheet" type="text/css">
<script src="http://maps.google.com/maps?file=api&v=1&key=<?echo $google_key;?>" type="text/javascript"></script>
<? include_javascript_functions(); ?>
</head>
<body>
<table width=<?echo $width;?> align="center" bgcolor="#ffffff"
border="1" bordercolor="#e5e5e5" cellpadding="3"
cellspacing="0">
<tr>
<td colspan=4 align="center" valign="middle" width="2%">
Store Locations near: <? echo $zipOne;?>
</td>
<tr>
<td colspan=4 align="center" valign="middle" width="2%">
<div id="map" style="width: <?echo $width;?>px; height: <?echo $height;?>px"></div>
</td>
</tr>

<tr>
<td>#</td>
<td>Distance</td>
<td style="width: 300px;">Store Location</td>
<td>Hours</td>
</tr>
<?
foreach ($locations AS $newlocation)
{
$counter++;
	$location = split("##", $newlocation);
	$lat = $location[5];
	$lon = $location[6];
	$address = $location[1];
	$city = $location[2];
	$state = $location[3];
	$zip = $location[4];
	$storename = $location[7];
	$hours = $location[8];
	$url = $location[9];
	$image = $location[10];
	$notes = $location[11];
	$phone = $location[12];
	$id = $location[14];
	$image_url = $location[15];
	$distance = number_format($location[0],2);
	//$html = $location[13];
if ($newlocation[0] != '')
	{

?>
<tr>
<td>
<?echo "<a href=\"#\" onclick=\"show_marker($lon,$lat);\">$counter</a>";?></td>
<td><? echo $distance; ?></td>
<td><?
if ($image != '' && display_image_in_results == 'yes')
{
 if ($image_url != '')
 {
  echo "<a href=\"$image_url\"><img style=\"float: left\" src=\"$image\" width=104 height=124></a>";
 }
 else
 {
  echo "<img style=\"float: left\" src=\"$image\" width=104 height=124>";
 }
}

if ($url == '')
{
echo "$storename<br>$address<br>$city, $state $zip<br>$phone";
}
else
{
echo "<b><a href=\"$url\">$storename</a></b><br>$address<br>$city, $state $zip<br>$phone";
}
?></td>
<td><a href="http://maps.google.com/maps?saddr=<? echo $zipOne; ?>&daddr=<?
if (use_coordinate($id, $db) == 1)
{
echo "$address, $city, $state $zip@";
echo $lat . ",";
echo $lon;
}
else
{
echo "$address, $city, $state $zip";
}
?>&hl=en">Map & Directions</a><br><? echo "$hours<br>$notes"; ?></td>
</tr>
<?
	}
}
?>
</table>
<div align=center>Powered by: <b><a href="http://www.crystaldawn.net">phpGoogleStoreLocator</a></b></a></div>
<script type="text/javascript">
//<![CDATA[




  // create the map
var lon;
var lat;
var point = new GPoint(<?echo floatval($center_lon); ?>,<?echo floatval($center_lat); ?>);
var marker;
  var map = new GMap(document.getElementById("map"));
  map.addControl(new GLargeMapControl());
  map.addControl(new GMapTypeControl());
  map.addControl(new GScaleControl());

//Create the custom Green icon.  Google only provides red, so I've modified it with a different color.
var icon = new GIcon();
//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
<? //Get the size of the image so we can pass it to GSize.  Leaving GSize blank will break the map, so we have to pass these sizes or things dont work right :/;
$iconname = get_setting('zipcode_marker_icon', $db);
$mapicon = "mapicons/$iconname";
list($width, $height, $type, $attr) = getimagesize("$mapicon");
//list($width, $height, $type, $attr) = getimagesize(locations_marker_icon);
?>
icon.iconSize = new GSize(<? echo $width; ?>, <? echo $height; ?>);

icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(9, 34);
icon.infoWindowAnchor = new GPoint(9, 2);
icon.infoShadowAnchor = new GPoint(18, 25);
icon.image = "<? echo $mapicon;?>";

map.centerAndZoom(new GPoint(<?echo floatval($center_lon); ?>, <?echo floatval($center_lat); ?>), <? echo $zoom;?>);
marker = createMarker(point, name, '', 'You', icon);
map.addOverlay(marker);
<?
foreach ($locations AS $newlocation)
{
$location = split("##", $newlocation);
	$lat = $location[5];
	$lon = $location[6];
	$address = addslashes($location[1]);
	$city = addslashes($location[2]);
	$state = addslashes($location[3]);
	$zip = addslashes($location[4]);
	$storename = addslashes($location[7]);
	$hours = addslashes($location[8]);
	$url = addslashes($location[9]);
	$image = addslashes($location[10]);
	$notes = addslashes(wordwrap($location[11], 80, "<br>"));
	$phone = addslashes($location[12]);
	$id = $location[14];
	$image_url = addslashes($location[15]);
	$distance = $location[0];

//If the location is blank, skip it.  Otherwise, setup a bullet on the map for it.
if ($lat == 0 && $lon == 0)
{
}
else
{
?>
lon = <?echo floatval($lon); ?>;
lat = <?echo floatval($lat); ?>;
point = new GPoint(lon,lat);
//Create the custom Green icon.  Google only provides red, so I've modified it with a different color.
icon = new GIcon();
//icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
<? //Get the size of the image so we can pass it to GSize.  Leaving GSize blank will break the map, so we have to pass these sizes or things dont work right :/
$iconname = get_setting('locations_marker_icon', $db);
$mapicon = "mapicons/$iconname";
list($width, $height, $type, $attr) = getimagesize("$mapicon");
//list($width, $height, $type, $attr) = getimagesize(locations_marker_icon);
?>
icon.iconSize = new GSize(<? echo $width; ?>, <? echo $height; ?>);
//icon.shadowSize = new GSize(37, 34);
icon.iconAnchor = new GPoint(9, 34);
icon.infoWindowAnchor = new GPoint(9, 2);
icon.infoShadowAnchor = new GPoint(18, 25);
var address = <? 

//This little snippet makes the balloon "to here, from here" features use the same logic as the results page
//when determining whether to send google a coordinate or address to get driving directions.
if (use_coordinate($id, $db) == 1)
{
echo "'$lat,$lon'";
}
else
{
echo "'$address,$city,$state,$zip'";
}?>;

//Setup the custom Icons
icon.image = "<? echo $mapicon;?>";
marker = createMarker(point, name, '<? 

if ($image != '' && display_image_in_balloon == 'yes')
{
  if ($image_url != '')
 {
  echo "<a href=\"$image_url\"><img style=\"float: left\" src=\"$image\" width=104 height=124></a>";
 }
 else
 {
  echo "<img style=\"float: left\" src=\"$image\" width=104 height=124>";
 }
}

if ($url == '')
{
echo "<b>$storename</b><br>$address<br>$city, $state $zip<br>$phone<br>$notes";
}
else
{
echo "<b><a href=\"$url\">$storename</a></b><br>$address<br>$city, $state $zip<br>$phone<br>$notes";
}
?>', '<? echo $storename?>', icon);
marker.id = marker.id +1;
map.addOverlay(marker);
<?
}//end of if
}//end of foreach
?>


GEvent.addListener(map, 'click', function(overlay, point) {
	if (overlay) {
	  //map.removeOverlay(overlay);
	} else if (point) {

	output.innerHTML += "";
	if (map.getZoomLevel() >= zoomToLevel) {
		map.centerAndZoom(point, zoomToLevel);
	}
		map.addOverlay(new GMarker(point));
  }


  }
);

//]]>


</script>

	</td>
  </tr>
  <tr>
	<td><?php echo tep_draw_separator('pixel_trans.gif', '100%', '10'); ?></td>
  </tr>
  <tr>
	<td><table border="0" width="100%" cellspacing="1" cellpadding="2" class="infoBox">
	  <tr class="infoBoxContents">
		<td><table border="0" width="100%" cellspacing="0" cellpadding="2">
		  <tr>
			<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
			<td align="right"><?php echo '<a href="' . tep_href_link(FILENAME_DEFAULT) . '">' . tep_image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE) . '</a>'; ?></td>
			<td width="10"><?php echo tep_draw_separator('pixel_trans.gif', '10', '1'); ?></td>
		  </tr>
		</table></td>
	  </tr>
	</table></td>
  </tr>
</table></td>
<!-- body_text_eof //-->
 </tr>
</table>
<!-- body_eof //-->

<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>

 

Where the code starts with:

 

{
echo "Your zipcode does not seem to be valid for the countries that we support.";
exit;
}

 

I cannot figure out how to make it so that this shows up within my site. If you enter jibberish into the box then it will come back with this on a blank page. The same also goes if you enter a zip code where there are no stores within the search area. Could someone please help me out with what to do I have been experimenting all day. Once I have it working I will upload it as a contrib.

 

Thanks,

 

Jeff

Edited by spadeals

Share this post


Link to post
Share on other sites

Take a look at the EasyMap contribution to see how to integrate the code.

 

Jack

Share this post


Link to post
Share on other sites

It's in the addons section. If you do a search for easymap there you should be able to find it.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×