Hi,
Zooming with wheel works, but not without scrolling the page.
The Code:
<?php
/*
Snippet Name: GoogleMap
Short Desc: Outputs a Google map for a given postcode
Created By: Mark Croxton ([email protected])
Modifed to allow lat/lng passing, added large control option and bubbletpl option by Andrew Ayre
Last modified: 01/29/2007
Version: 1.2
Google Map API Version: 2.0
Example call:
[!GoogleMap?key=`ABQIAAAA...` &zip=`bn14fb` &title=`My Office` &address=`101 My Road, My town, My Country` &width=`300` &height=`300`!]
[!GoogleMap?key=`ABQIAAAA...` &lat=`53.5632` &lng=`45.655` &title=`My Office` &address=`101 My Road, My town, My Country` &width=`300` &height=`300`!]
Params:
&key = Google API Key (string - required) - see http://www.google.com/apis/maps/
&zip = UK or US postal code (string) (overrides lat/lng)
&lat = latitude (float)
&lng = longitude (float)
&title = Map title - used in the pop-up bubble (string)
&address = Full address - used in the pop-up bubble (string)
&width = Map width (integer)
&height = Map height (integer)
&zoom = Map zoom level (integer)
&mapid = Map div css id (string)
&bubblewidth = pop-up bubble width (integer)
$phmode = output to placeholders? (recommended) (boolean)
[+gmapjs+] place in the head of your template file
[+gmap+] place where you want the map to appear
$largecntrl = set to 1 for large map control (boolean)
$bubbletpl = name of chunk to use inside bubble (string) (overrides title and address)
*/
$key = isset($key)? "$key" : '';
$zip = isset($zip)? "$zip" : '';
$lat = isset($lat)? "$lat" : '';
$lng = isset($lng)? "$lng" : '';
$title = isset($title)? "$title" : '';
$address = isset($address)? "$address" : '';
$width = isset($width)? "$width" : '400';
$height = isset($height)? "$height" : '400';
$zoom = isset($zoom)? "$zoom" : '15';
$mapid = isset($mapid)? "$mapid" : 'map';
$bubblewidth = isset($bubblewidth)? "$bubblewidth" : '230';
$phmode = (!isset($phmode)) ? false : ($phmode==true);
$largecntrl = isset($largecntrl)? "$largecntrl" : '0';
$bubbletpl = isset($bubbletpl)? "$bubbletpl" : '';
# geocode the postcode
if (strlen($zip)) {
$coord = geocode($zip); // coord[0]=lng and coord[1]=lat
}
else
{
$coord[0] = $lng;
$coord[1] = $lat;
}
$gmapjs = '
<script src="http://maps.google.com/maps?file=api&v=2&key='.$key.'" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function GloadMap() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("'.$mapid.'"));
map.setCenter(new GLatLng('.$coord[1].','.$coord[0].'),'.$zoom.');
';
if ($largecntrl == 1) {
$gmapjs .= 'map.addControl(new GLargeMapControl());';
}
else {
$gmapjs .= 'map.addControl(new GSmallMapControl());';
}
$gmapjs .= '
map.addControl(new GMapTypeControl());
map.addControl(new GOverviewMapControl());
map.enableScrollWheelZoom();
// Create our "tiny" marker icon
var icon = new GIcon();
icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
icon.iconSize = new GSize(12, 20);
icon.shadowSize = new GSize(22, 20);
icon.iconAnchor = new GPoint(6, 20);
icon.infoWindowAnchor = new GPoint(5, 1);
// Creates one of our tiny markers at the given point
function createMarker(point, index) {
var marker = new GMarker(point, icon);';
if (strlen($bubbletpl)) {
$gmapjs .= 'var myMarkerContent = "'.$modx->getChunk($bubbletpl).'";';
}
else {
$gmapjs .= 'var myMarkerContent = "<div style=\"width:'.$bubblewidth.'px; overflow:auto;\"><h4>'.$title.'</h4><p>'.$address.'</p></div>";';
}
$gmapjs .= '
map.addOverlay(marker);
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(myMarkerContent);
});
}
createMarker(new GPoint('.$coord[0].','.$coord[1].'));
}
}
//initialise Google map
window.addEvent(\'load\',GloadMap);
window.addEvent(\'unload\',GUnload);
// Prevent page scroll
function wheelevent(e) {
if (!e){
e = window.event;
}
if (e.preventDefault){
e.preventDefault();
}
e.returnValue = false;
}
var map = document.getElementById("'.$mapid.'");
GEvent.addDomListener(map.getContainer(), "DOMMouseScroll", wheelevent);
map.getContainer().onmousewheel = wheelevent;
//]]>
</script>
';
$gmap ='<div id="'.$mapid.'" style="width: '.$width.'px; height: '.$height.'px">
<noscript>
<center>
<img src="http://maps.google.com/mapdata?Point=b&Point.latitude_e6=[*breitengrad:math=`?*1000000`*]&Point.longitude_e6=[*laengengrad:math=`?*1000000`*]&Point.iconid=15&Point=e&latitude_e6=[*breitengrad:math=`?*1000000`*]&longitude_e6=[*laengengrad:math=`?*1000000`*]&zm=4800&w=490&h=265&cc=&min_priority=1">
<span style="font-size:0.8em;clear:left;float:none;"><br></br>
<a href="http://maps.google.com/maps?f=q&output=html&q=[*breitengrad*]+[*laengengrad*]&zoom=0&zp=OOOOOOOOOIIIIII" target="_blank">GoogleMaps - Karte: [*google_maps_beschreibung*]</a>
</span>
</center>
</noscript>
</div>';
# put the js in the <head>
#$modx->regClientStartupScript($gmapjs, true); // note: this is broken in 0.91, so we can't use just yet
if ($phmode) {
# output to placeholders
$modx->setPlaceholder('gmapjs', $gmapjs);
$modx->setPlaceholder('gmap', $gmap);
return;
} else {
$output = $gmapjs."\n".$gmap;
return $output;
}
/* ---------------------------------------------------
Functions
--------------------------------------------------- */
function geocode($postcode) {
$postcode = urlencode($postcode);
$coord = array();
// The URL of google to append the postcode
$url = "http://maps.google.com/maps?output=kml&q=";
$haystack = file_get_contents($url.$postcode);
$start = strpos($haystack, '<coordinates>') + 13;
$end = strpos($haystack, '</coordinates>');
$coords = substr($haystack, $start, ($end-$start));
$coord = explode(",", $coords);
return $coord;
}
?>
I changed something, because troubles with slimbox/maxigallery and because I’m using TVs.