Location: PHPKode > projects > Kwalbum > kwalbum/ajax/gmap/getmarkers.php
<?php
/**
 * Input min/max lat/lon boundaries through the URL and return marker
 * coordinates using JOSM.
 *
 * @author Tim Redmond
 * @package kwalbum
 * @since 2.2 Apr 28, 2009
 */
define('MAX_MARKERS_PER_SECTION', 10);
define('MAX_DEPTH', 3);

header("Content-type: text/plain; charset=UTF-8");
//header("Cache-Control: no-store, no-cache");

require_once '../../include/DBConnection.php';
$DB = new DBConnection();
require_once '../../include/verifyLogin.php';
require_once '../../include/funcGetImageCode.php';
require_once '../../include/URLParser.php';
$URLP = new URLParser();
$where = $URLP->GetWhereQuery(false);

echo "{'markers': [\n";

$left = (float)$_GET["l"];
$top = (float)$_GET["t"];
$right = (float)$_GET["r"];
$bottom = (float)$_GET["b"];
$zoom = (int)$_GET["z"];

if ($bottom>$top)
{
    $temp =$top;
    $top=$bottom;
    $bottom=$temp;
}
/*  // reduce the area being divided from the entire viewable map to only the area containing items/locations
    // commented out because right now when you zoom in too close it does not include items on the far edges
$query = 'SELECT MAX(ItemLatitude) as t1, MAX(LocationLatitude) as t2, MIN(ItemLatitude) as b1, MIN(LocationLatitude) as b2,'
			.' MAX(ItemLongitude) as r1, MAX(LocationLongitude) as r2, MIN(ItemLongitude) as l1, MIN(LocationLongitude) as l2'
			.' FROM '.LOCATION_TABLE.', '.ITEM_TABLE
			.' WHERE '
				."LocationId=ItemLocationIdFk"
				." AND ((ItemLatitude IS NOT NULL AND ItemLatitude != 0"
				." AND ItemLatitude > '$bottom' AND ItemLatitude < '$top'"
				.($left>$right
					? " AND (ItemLongitude > '$left' OR ItemLongitude < '$right')"
					: " AND ItemLongitude > '$left' AND ItemLongitude < '$right'")
				.") OR (LocationLatitude IS NOT NULL AND LocationLatitude != 0"
				." AND LocationLatitude > '$bottom' AND LocationLatitude < '$top'"
				.($left>$right
					? " AND (LocationLongitude > '$left' OR LocationLongitude < '$right')"
					: " AND LocationLongitude > '$left' AND LocationLongitude < '$right'")
				.'))'
				.(USER_CAN_VIEW_QUERY ? ' AND '.USER_CAN_VIEW_QUERY : null)
			.' LIMIT 1';
$row = $DB->Query($query)->fetch_assoc();
if (!$row['t1'])
	$top = $row['t2'];
else if (!$row['t2'])
	$top = $row['t1'];
else
	$top = ($row['t1'] > $row['t2'] ? $row['t1'] : $row['t2']);

if (!$row['b1'])
	$bottom = $row['b2'];
else if (!$row['b2'])
	$bottom = $row['b1'];
else
	$bottom = ($row['b1'] < $row['b2'] ? $row['b1'] : $row['b2']);
if ($right>$left)
{
	if (!$row['l1'])
		$left = $row['l2'];
	else if (!$row['l2'])
		$left = $row['l1'];
	else
		$left = ($row['l1'] < $row['l2'] ? $row['l1'] : $row['l2']);
	if (!$row['r1'])
		$right = $row['r2'];
	else if (!$row['r2'])
		$right = $row['r1'];
	else
		$right = ($row['r1'] > $row['r2'] ? $row['r1'] : $row['r2']);
}
else
{
	if (!$row['r1'])
		$right = $row['r2'];
	else if (!$row['r2'])
		$right = $row['r1'];
	else
		$left = ($row['r1'] > $row['r2'] ? $row['r1'] : $row['r2']);
	if (!$row['l1'])
		$left = $row['l2'];
	else if (!$row['l2'])
		$left = $row['l1'];
	else
		$right = ($row['l1'] < $row['l2'] ? $row['l1'] : $row['l2']);
}
*/
LoadMarkers($left, $right, $top, $bottom, 0);

function LoadMarkers($left, $right, $top, $bottom, $depth = MAX_DEPTH)
{
	global $DB, $where;

	$itemSearch = $where.
			" AND LocationId=ItemLocationIdFk"
			." AND ItemLatitude IS NOT NULL AND ItemLatitude != 0"
			." AND ItemLatitude >= '$bottom' AND ItemLatitude <= '$top'"
			.($left>$right
				? " AND (ItemLongitude >= '$left' OR ItemLongitude <= '$right')"
				: " AND ItemLongitude >= '$left' AND ItemLongitude <= '$right'");
	$itemCount = $DB->GetCount(LOCATION_TABLE.', '.ITEM_TABLE, $itemSearch);

	$locationSearch = $where.
		" AND LocationCount > 0"
		." AND LocationLatitude IS NOT NULL AND LocationLatitude != 0"
		." AND LocationLatitude >= '$bottom' AND LocationLatitude <= '$top'"
		.($left>$right
			? " AND (LocationLongitude >= '$left' OR LocationLongitude <= '$right')"
			: " AND LocationLongitude >= '$left' AND LocationLongitude <= '$right'");

	$locationCount = $DB->GetCount(LOCATION_TABLE, $locationSearch);
	if (0 == $itemCount and 0 == $locationCount)
		return;

	if (MAX_DEPTH > $depth and MAX_MARKERS_PER_SECTION < ($itemCount+$locationCount))
	{
		$depth++;
		$centerLat = ($left+$right)/2;
		$centerLon = ($top+$bottom)/2;
		LoadMarkers($left, $centerLat, $top, $centerLon, $depth);
		LoadMarkers($centerLat, $right, $top, $centerLon, $depth);
		LoadMarkers($left, $centerLat, $centerLon, $bottom, $depth);
		LoadMarkers($centerLat, $right, $centerLon, $bottom, $depth);
	}
	else
	{
		$query = 'SELECT Location, LocationCount, LocationLatitude as lat, LocationLongitude as lon'
			.' FROM '.LOCATION_TABLE
			.' WHERE '.$locationSearch
			." ORDER BY LocationLatitude"
			." LIMIT ".MAX_MARKERS_PER_SECTION;
		$result = $DB->Query($query);
		print_results($result, 1);

		if (MAX_MARKERS_PER_SECTION >= ($itemCount+$locationCount))
		{
			$query = 'SELECT Location, ItemDate, ItemId, ItemPath, ItemFilename, ItemTypeId, ItemLatitude as lat, ItemLongitude as lon'
				.' FROM '.LOCATION_TABLE.', '.ITEM_TABLE
				.' WHERE '.$itemSearch
				.' GROUP BY ItemLatitude, ItemLongitude'
				.' ORDER BY ItemLatitude'
				.' LIMIT '.MAX_MARKERS_PER_SECTION;
			$result = $DB->Query($query);
			print_results($result, 0);
		}
		else
		{
			$query = 'SELECT AVG(ItemLatitude) as lat, AVG(ItemLongitude) as lon'
				.' FROM '.LOCATION_TABLE.', '.ITEM_TABLE
				.' WHERE '.$itemSearch
				.' LIMIT 1';
			$result = $DB->Query($query);
			print_results($result, 2);
		}
		return;
	}
}

function print_results($result, $type)
{
	if (0 < $result->num_rows)
	{
		while ($row = $result->fetch_assoc())
		{
			$row['count'] = $result->num_rows;
			echo "{'lat':$row[lat],'lng':$row[lon]";
			if (0 == $type)
			{
				echo ",'html':'<a href=\'".PAGE_URL."i=$row[ItemId]\'><img src=\'".PAGE_URL."f=$row[ItemId]&amp;s=1\' height=\'".THUMB_Y."px\' width=\'".THUMB_X."px\'/></a><br/>$row[ItemDate]<br/><a href=\'".PAGE_URL."loc=$row[Location]\'>$row[Location]</a>'"
					.",'label':'$row[Location]'";
			}
			else if (1 == $type)
			{
				echo ",'html':'<a href=\'".PAGE_URL."loc=$row[Location]\'>$row[Location]</a>','label':'$row[Location]'";
			}
			else
			{
				echo ",'html':'','label':'Many Items'";
			}
			echo ",'type':$type},\n";
		}
	}
}

echo "]}";

Return current item: Kwalbum