<?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)
    * $GmapCtrl = default control size (small|large) defaults to large
    * $GmapType = default map type (normal, satellite, hybrid).

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

    * ctrl = designates the size of the arrow button control (small|large)
    * daddr = destination address (which allows directions to the 
      address)
    * lat = lattitude
    * lon = longitude
    * msg = what text is displayed in the window over the marker. 
      You will have to provide the HTML. 
    * type = the map type ('nor'mal, 'hyb'rid, 'sat'ellite)
    * zom = the zoom level.

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

    * v1.0 - May 17, 2006: Initial Public Release by Ben Wilson.
    * v1.0.1 - May 22, 2006: Release by Ben Wilson.
        * Incorporated Google Map API v2.0+ compliance corrections by Des.
        * Replace $GmapCert with $HTMLHeaderFmt call.
        * Fixed problem with CSS via $HTMLStylesFmt.
        * Added support for dynamic scaling of controller.
        * Added the overview map.
        * Added support for map type (satellite, normal or hybrid).
        * Only add the directions block when directions are offered.
          Before, directions were only offered when a message was given.
        * Only add the message block when the message is offered.
	* v1.0.2
		* Added map type control buttons
		* Fixed default operation of map control sizing
		* Fixed spelling error that prevented the parser selecting the 
		  desired map control size.

*/

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

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

function Gmap($input='') {
    global $GmapLon, $GmapLat, $GmapZom, $GmapJS;
    global $GmapType, $GmapKey, $GmapCtrl;
    global $MapHtml, $ScriptHtml;
    global $HTMLStylesFmt, $HTMLHeaderFmt;

    $HTMLStylesFmt['gmap_api'] = 'div#map{ height: 400px; width:100%;}';
    $HTMLHeaderFmt[] = '<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;
	
    #--------------------------------
    # Check for directions
    $dad = ($opt['daddr']) ? $opt['daddr'] : $GmapDaddr;
    if ($dad != '') {
        $directions =<<<DIRECTIONS
      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>'; 
DIRECTIONS;
    }
    else { $directions = ''; }
    #--------------------------------
    # Get the Controller Size. v1.0.1
    $cki = ($opt['ctrl']) ? $opt['ctrl'] : $GmapCtrl; // v.1.0.2 crtl to ctrl spellfix by Des
	$map_controller_size = (strtolower($cki) != 'large') // v.1.0.2 == to != bugfix by Des
		? "map.addControl(new GSmallMapControl());"
		: "map.addControl(new GLargeMapControl());";

    #--------------------------------
    # Get the Map type. v1.0.1 (inserted in Gmap2.setCenter())
    $map_type = array(
        'sat' => 'G_SATELLITE_MAP',
        'hyb' => 'G_HYBRID_MAP',
        'nor' =>'G_NORMAL_MAP',
    );
    $map_type_key = ($opt['type']) ? $opt['type'] : $GmapType;
    $map_type = $map_type[strtolower(substr($map_type_key,0,3))];

    #--------------------------------
    # Handle the Message.
    if ($opt['msg']) { 
        $opt['msg'] = preg_replace('/&gt;/','>',$opt['msg']); 
        $opt['msg'] = preg_replace('/&lt;/','<',$opt['msg']); 
        $message = "      html +='$opt[msg]';";
    }
    else {
      $message = '';
    }
    if ($message != '' or $directions != '') {
        $output = "var html   = '<br />';\n$message$directions";
        $window = "marker.openInfoWindowHtml(html);";
    }

$GmapJS =<<<GMAPJS
<script type="text/javascript">
  //<![CDATA[
    if (GBrowserIsCompatible()) {
      var map    = new GMap2(document.getElementById("map"));
      var point  = new GLatLng($lat,$lon); // v1.0.1 - by Des
      var marker = new GMarker(point);
      $output
      map.setCenter(point, $zom, $map_type); // v1.0.1 - by Des, $map_type by Ben Wilson
      map.addControl(new GScaleControl()); // v1.0.1 - by Des
	  map.addControl(new GMapTypeControl());  // v1.0.2 - by Des
      $map_controller_size  // v1.0.1 - by Ben Wilson
      map.addControl(new GOverviewMapControl()); // v1.0.1 - by Ben Wilson
      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";
}