<?php
/*

Google Map API for PmWiki
=========================

Copyright Statement
-------------------

Copyright (c) 2006, Benjamin C. Wilson. All Rights Reserved. You have
permission to use this software for web site use provided this copyright
statement remains intact. You may not otherwise republish this software
without prior permission of the author. License to publish granted
PmWiki.org for distribution to the PmWiki community.

You may contact the author at ameen@dausha.net. This software is 
maintained at http://www.dausha.net/GoogleMapAPI/.

Introduction
------------

The Google Map API is a simple markup way to add map support to 
your PmWiki site. It complies with the Google Map API v2.0 released
in April, 2006.

Installation
------------

   1. First, you must have a Google Map API certificate. To obtain one,
      visit: http://www.google.com/apis/maps/signup.html. One is needed
      for each web site.
   2. In the configuration file (e.g. local/config.php), set the 
      certificate: $GmapCert = '/your gmap cert/';
   3. After any other $Gmap-variables, include the script:
      include_once('/path/to/gmap_api-1.0.php');
   4. In your site template, add the $GmapJS to the end of the page
      to avoid javascript problems: $GmapJS</body></html>

Usage
------

At a minimum, you must use "(:gmap:)," but to set the latitude and
longitude, you will have to use a free geocoder (e.g. http://geocoder.us/).
The examples below show how you can add text to a bubble over the
marker and destination address that allows the site visitor to find
directions to the location.

Examples:
~~~~~~~~~

(:gmap lat=38.897639 lon=-77.036583:) "Minimum for pointing to a location."

(:gmap lat=38.897639 lon=-77.036583 daddr="1600 Pennsylvania, 22303" 
msg="Joe's Bar and Grill 1600 Ranch Drive Washington, DC 22223":) 
"Full example with the destination address and message."

Variables
---------

These are the variables that influence the behavior of the markup. The 
defaults are recommended for use in the configuration file for your 
PmWiki Installation. The markup options may be added by the author.

Defaults
~~~~~~~~

    * $GmapLon = default longitude
    * $GmapLat = default latitude
    * $GmapZom = default zoom
    * $GmapJS (leave blank)
    * $GmapCert = site certificate. 

Markup Options
~~~~~~~~~~~~~~

    * zom = the zoom level.
    * lat = lattitude
    * lon = longitude
    * daddr = destination address (which allows directions to the 
      address)
    * msg = what text is displayed in the window over the marker. 
      You will have to provide the HTML. 

Release Notes
-------------

* v1.0 - May 17, 2006: Initial Public Release by Ben Wilson.

*/

$MapHtml = "<div id='map'></div>";
SDV($GmapLon,  '-122.141944');
SDV($GmapLat,  '37.441944');
SDV($GmapZom,  8);
SDV($GmapJS,   ' ');
SDV($GmapCert,   ' ');

Markup('gmap', 'block', '/\(:gmap(.*?):\)/e', 'Gmap("$1");');

function Gmap($input='') {
    global $GmapLon, $GmapLat, $GmapZom, $GmapJS, $GmapCert, $GmapKey;
    global $MapHtml, $ScriptHtml;
    $GmapCert = '<script src="http://maps.google.com/maps?file=api&v=2&key='.$GmapKey.'" type="text/javascript"></script>';
    $opt = ParseArgs($input);
    $zom = ($opt['zom']) ? $opt['zom'] : $GmapZom;
    $lat = ($opt['lat']) ? $opt['lat'] : $GmapLat;
    $lon = ($opt['lon']) ? $opt['lon'] : $GmapLon;
    $dad = ($opt['daddr']) ? $opt['daddr'] : $GmapDaddr;
    if ($opt['msg']) { 
        $opt['msg'] = preg_replace('/&gt;/','>',$opt['msg']); 
        $opt['msg'] = preg_replace('/&lt;/','<',$opt['msg']); 
    $crap =<<<CRAP
      html += '<form action="http://maps.google.com/maps" method="get">';
      html += '<i>Directions to here from</i>: <br />';
      html += '<input type="text"   name="saddr" value="" size="25"><br />';
      html += '<input type="hidden" name="daddr" value="$dad" />';
      html += '<input type="hidden" name="hl"    value="en">';
      html += '<input type="submit" value="Directions"/></form>'; 
CRAP;
      $window = "marker.openInfoWindowHtml(html);";
    }

$GmapJS =<<<GMAPJS
<script type="text/javascript">
  //<![CDATA[
    if (GBrowserIsCompatible()) {
      var map    = new GMap(document.getElementById("map"));
      var point  = new GPoint($lon, $lat);
      var html   = '<br />$opt[msg]';
$crap
      var marker = new GMarker(point);
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.centerAndZoom(point, 4);    
      map.addOverlay(marker);
      $window
    }
    else {
      alert("Sorry, the Google Maps API is not compatible with this browser. Please visit http://www.mapquest.com for an alternative.");
    }
  //]]>
</script>
GMAPJS;
    return "$MapHtml";
}