Skip to content
This repository has been archived by the owner on May 12, 2022. It is now read-only.

Commit

Permalink
Changed API for infowindows.
Browse files Browse the repository at this point in the history
Infowindows now contain marker and not vice versa. This might first seem
counterintuitive but actually is more logical. I have seen lots of other
code (including Google's) to go this way.

Code is now something like following:

-cut-
$map = Google_Maps::create('static');

$coord_1  = new Google_Maps_Coordinate('58.378700', '26.731110');
$marker_1 = new Google_Maps_Marker($coord_1);

$bubble_1 = new Google_Maps_Infowindow('Foo Bar');
$bubble_1->setMarker($marker_1);

$map->addInfowindow($bubble_1);
$map->addMarker($marker_1);
-cut-
  • Loading branch information
tuupola committed Oct 22, 2008
1 parent 40b23ed commit 2f2bf2e
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
8 changes: 3 additions & 5 deletions Google/Maps/Infowindow.php
Expand Up @@ -19,7 +19,7 @@

class Google_Maps_Infowindow extends Google_Maps_Overload {

protected $coordinate;
protected $marker;
protected $content;
protected $display = 'none';
protected $template = '
Expand Down Expand Up @@ -92,11 +92,9 @@ public function isVisible() {
return $this->getDisplay() == 'block' ? true : false;
}

/*
TODO Consider switching back to Google_Maps_Location because of getId()
*/
public function toHtml(Google_Maps_Static $map, Google_Maps_Location $location) {
public function toHtml(Google_Maps_Static $map) {
$template = $this->getTemplate();
$location = $this->getMarker();
return sprintf($template, $location->getId(),
$location->getContainerX($map) - 160,
$location->getContainerY($map) - 235,
Expand Down
9 changes: 0 additions & 9 deletions Google/Maps/Marker.php
Expand Up @@ -58,15 +58,6 @@ public function toArea(Google_Maps_Static $map) {
return sprintf('<area shape="circle" coords="%d,%d,12" href="?%s" name="%s">',
$marker_x, $marker_y, $url, $marker_id);
}

/**
* Set the coordinate of current marker object.
*
* @param object $location Google_Maps_Coordinate|Point
*/
public function setCoordinate($location) {
$this->coordinate = $location->toCoordinate();
}

/**
* Return marker as coordinate in Google Maps.
Expand Down
56 changes: 50 additions & 6 deletions Google/Maps/Static.php
Expand Up @@ -26,6 +26,7 @@ class Google_Maps_Static extends Google_Maps_Overload {
protected $format;
protected $maptype;
protected $markers = array();
protected $infowindows = array();
protected $path;
protected $span;
protected $frame;
Expand Down Expand Up @@ -103,6 +104,8 @@ public function calculateCenter() {
* Set and return zoom of the map so all markers fit to screen. Takes map
* center into account when it is set.
*
* TODO zoomToFit() does not respect maz and min zoom.
*
* @return integer New zoom level.
*/
public function zoomToFit() {
Expand All @@ -125,6 +128,23 @@ public function zoomToFit() {
return $zoom;
}

/**
* Set visible infowindow. Data usually passed from querystring.
*
* @param string id of infowindow
* @return integer number of visible infowindows in map.
*/
public function setInfowindow($id = '') {
foreach ($this->getInfowindows() as $infowindow) {
if ($infowindow->getMarker()->getId() == $id) {
$infowindow->show();
}
}
/* TODO Hardcoded because there can be only one infowindow open ATM. */
return 1;
}


/**
* Return smallest possible bounds where all markers fit in.
*
Expand Down Expand Up @@ -298,7 +318,7 @@ public function addControl($control) {
}

/**
* Remove controls from map.
* Remove control from map.
*
* @param object Google_Maps_Control
* @return integer Total number of controls in map.
Expand All @@ -307,6 +327,27 @@ public function removeControl() {

}

/**
* Add infowindow to map.
*
* @param object Google_Maps_Infowindow
* @return integer Total number of infowindows in map.
*/
public function addInfowindow($infowindow) {
$this->infowindows[] = $infowindow;
return count($this->getInfowindows());
}

/**
* Remove infowindow from map.
*
* @param object Google_Maps_Infowindow
* @return integer Total number of infowindows in map.
*/
public function removeInfowindow() {

}

/**
* Zoom out one level
*
Expand Down Expand Up @@ -393,9 +434,10 @@ public function panEast($pixels) {
public function toQueryString($include_all = false) {
$url['center'] = $this->getCenter()->__toString();

foreach ($this->getMarkers() as $marker) {
if ($marker->getInfowindow()->isVisible()) {
$url['infowindow'] = $marker->getId();
$url['infowindow'] = '';
foreach ($this->getInfowindows() as $infowindow) {
if ($infowindow->isVisible()) {
$url['infowindow'] = $infowindow->getMarker()->getId();
}
}

Expand Down Expand Up @@ -449,9 +491,11 @@ public function toHtml() {
foreach ($this->getControls() as $control) {
$retval .= $control->toHtml($this) . "\n";
}
foreach ($this->getMarkers() as $marker) {
$retval .= $marker->getInfowindow()->toHtml($this, $marker) . "\n";

foreach ($this->getInfowindows() as $infowindow) {
$retval .= $infowindow->toHtml($this) . "\n";
}

$retval .= '</div>' . "\n";
$retval .= '</div>' . "\n";

Expand Down
23 changes: 13 additions & 10 deletions controls.html
Expand Up @@ -15,28 +15,31 @@

$coord_1 = new Google_Maps_Coordinate('58.378700', '26.731110');
$coord_2 = new Google_Maps_Coordinate('58.379646', '26.764090');

$marker_1 = new Google_Maps_Marker($coord_1);
$marker_2 = new Google_Maps_Marker($coord_2);

$bubble_1 = new Google_Maps_Infowindow('Foo Bar');
$bubble_2 = new Google_Maps_Infowindow('Pler pop');

$marker_1->setInfowindow($bubble_1);
$marker_2->setInfowindow($bubble_2);
$bubble_1->setMarker($marker_1);
$bubble_2->setMarker($marker_2);

//$marker_1->setInfowindow($bubble_1);
//$marker_2->setInfowindow($bubble_2);

//$bubble_1->show();
//$marker_2->getInfowindow()->show();
//$bubble_2->show();

$map->addInfowindow($bubble_1);
$map->addInfowindow($bubble_2);
$map->addMarker($marker_1);
$map->addMarker($marker_2);

$map->setZoom(13);

$map->setProperties($_GET);

if ($_GET['infowindow']) {
$$_GET['infowindow']->getInfowindow()->show();
}

//print $map->toQueryString();

?>
Expand Down Expand Up @@ -83,7 +86,7 @@ <h1>PHP Google Static Maps</h1><br />

<div class="entrytitle">
<h2>Add Controls to Static Map</h2>
<h4>This page demonstrates how to add zoom and pan controls to static map.</h4>
<h4>There is no JavaScript in this page (except Google Analytics)</h4>
</div>
<div class="entrybody">
<?php print $map->toHtml(); ?>
Expand Down Expand Up @@ -137,7 +140,7 @@ <h3>Where's the Source?</h3>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
/*
/*
$(function() {
$('map area').bind('click', function() {
$('.bubble').hide();
Expand All @@ -146,7 +149,7 @@ <h3>Where's the Source?</h3>
return false;
});
});
*/
*/
</script>

<script src="/mint/?js" type="text/javascript"></script>
Expand Down

0 comments on commit 2f2bf2e

Please sign in to comment.