注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

天涯的博客

如果你是我眼中的一滴泪,那我永远也不会哭!

 
 
 

日志

 
 
 
 

2010最新php应用谷歌地图api  

2011-01-15 10:42:42|  分类: php学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

<?php


class simpleGMapAPI {

/**
* mapMarkers : array
* Holds data (coords etc.) of Markers
*/
private $mapMarkers = array();

/**
* mapCircles : array
* Holds data (coords etc.) of Circles
*/
private $mapCircles = array();

/**
* mapRectangles : array
* Holds data (coords etc.) of Rectangles
*/
private $mapRectangles = array();

/**
* mapWidth
* width of the Google Map, in pixels
*/
private $mapWidth = 400;

/**
* mapHeight
* height of the Google Map, in pixels
*/
private $mapHeight = 400;

/**
* mapBackgroundColor
* string : color for the map background
*/
private $mapBackgroundColor = "";

/**
* apiSensor : boolean
* True/False wether the device has a sensor or not
*/
private $apiSensor = false;

/**
* mapDraggable : boolean
* True/False wether the map is draggable or not
*/
private $mapDraggable = true;

/**
* mapType
* string : can be 'ROADMAP', 'SATELLITE', 'HYBRID' or 'TERRAIN'
* display's either a (road)map, satellite, hybrid or terrain view, (road)map by default
*/
private $mapType = 'ROADMAP';

/**
* zoomLevel
* int : 0 - 19
* set's the initial zoom level of the map (0 is fully zoomed out and 19 is fully zoomed in)
*/
private $zoomLevel = 6;

/**
* showDefaultUI
* True/False whether to show the deault UI controls or not
*/
private $showDefaultUI = true;

/**
* showMapTypeControl
* True/False whether to show map type control or not
*/
private $showMapTypeControl = true;

/**
* mapTypeControlStyle
* string : can be 'HORIZONTAL_BAR', 'DROPDOWN_MENU' or 'DEFAULT'
* Style of the map type control
*/
private $mapTypeControlStyle = 'DEFAULT';

/**
* showNavigationControl
* True/False whether to show navigation control or not
*/
private $showNavigationControl = true;

/**
* navigationControlStyle
* string : can be 'ANDROID', 'DEFAULT', 'SMALL' or 'ZOOM_PAN'
* Style of the navigation control
*/
private $navigationControlStyle = 'DEFAULT';

/**
* showScaleControl
* True/False whether to show scale control or not
*/
private $showScaleControl = true;

/**
* showStreetViewControl
* True/False whether to show StreetView control or not
*/
private $showStreetViewControl = true;

/**
* enableScrollwheelZoom
* True/False whether the scrollwhell zoom is enabled or not
*/
private $enableScrollwheelZoom = true;

/**
* enableDoubleClickZoom
* True/False whether doubleclick zoom zoom is enabled or not
*/
private $enableDoubleClickZoom = true;

/**
* infoWindowBehaviour
* string : can be 'MULTIPLE', 'SINGLE', 'CLOSE_ON_MAPCLICK' or 'SINGLE_CLOSE_ON_MAPCLICK'
* Behavious of InfoWindow overlays
*/
private $infoWindowBehaviour = 'MULTIPLE';

/**
* infoWindowTrigger
* string : can be 'CLICK' OR 'MOUSEOVER'
* Determines if InfoWindow is shown with a click or by mouseover
*/
private $infoWindowTrigger = 'CLICK';

/**
 * maximum longitude of all markers
 *
 * @var float
 */
private $maxLng = -1000000;

/**
 * minimum longitude of all markers
 *
 * @var float
 */
private $minLng = 1000000;

/**
 * max latitude
 *
 * @var float
 */
private $maxLat = -1000000;

/**
 * min latitude
 *
 * @var float
 */
private $minLat = 1000000;

/**
 * map center latitude (horizontal)
 * calculated automatically as markers
 * are added to the map.
 *
 * @var float
 */
private $centerLat = null;

/**
 * map center longitude (vertical)
 * calculated automatically as markers
 * are added to the map.
 *
 * @var float
 */
private $centerLng = null;


/**********
 *** START OF FUNCTION BLOCK
 **********/

/**
* @function     __construct
* @description  constructor
* @param $sensor : boolean
*/
function __construct($sensor = false)
{
    $this->setSensor($sensor);
}

/**
* @function     setSensor
* @param        $sensor : boolean
* @returns      nothing
* @description  Tells the v3 API wether the device has a sensor or not
*/
function setSensor($sensor = false)
{
    $this->apiSensor = $sensor;
}

/**
* @function     setWidth
* @param        $width : int
* @returns      nothing
* @description  Sets the width of the map to be displayed
*/
function setWidth($width)
{
    if ($width <= 0) { $width = $this->mapWidth; }
    $this->mapWidth = $width;
}

/**
* @function     setHeight
* @param        $height : int
* @returns      nothing
* @description  Sets the height of the map to be displayed
*/
function setHeight($height)
{
    if ($height <= 0) { $height = $this->mapHeight; }
    $this->mapHeight = $height;
}

/**
* @function     setBackgroundColor
* @param        $color : string
* @returns      nothing
* @description  Sets the background color of the map
*/
function setBackgroundColor($color = "")
{
    $this->mapBackgroundColor = $color;
}

/**
* @function     setZoomLevel
* @param        $zoom : int (0 - 19)
* @returns      nothing
* @description  Sets the zoom level of the map (0 is fully zoomed out and 19 is fully zoomed in)
*/
function setZoomLevel($zoom)
{
    if (($zoom <= 0) OR ($zoom > 19)) { $zoom = $this->zoomLevel; }
    $this->zoomLevel = $zoom;
}

/**
* @function     setMapType
* @param        $mapType : string (can be 'ROADMAP', 'SATELLITE', 'HYBRID' or 'TERRAIN')
* @returns      nothing
* @description  Sets the type of the map to be displayed, either a (road)map, satellite, hybrid or terrain view; (road)map by default
*/
function setMapType($mapType)
{
    switch ($mapType)
    {
        case 'SATELLITE' :
        case 'HYBRID' :
        case 'TERRAIN' :
            $this->mapType = $mapType;
            break;
        default :
            $this->mapType = 'ROADMAP';
            break;       
    }
}

/**
* @function     setMapDraggable
* @param        $draggable : boolean
* @returns      nothing
* @description  Sets wether the map is draggable or not
*/
function setMapDraggable($draggable = true)
{
    $this->mapDraggable = $draggable;
}

/**
* @function     setInfoWindowBehaviour
* @param        $infoWindowBehaviour : string (can be 'MULTIPLE', 'SINGLE' or 'CLOSE_ON_MAPCLICK')
* @returns      nothing
* @description  Sets the behaviour of InfoWindow overlays, either multiple or single windows are displayed
*/
function setInfoWindowBehaviour($infoWindowBehaviour)
{
    switch ($infoWindowBehaviour)
    {
        case 'MULTIPLE' :
        case 'SINGLE' :
        case 'CLOSE_ON_MAPCLICK' :
        case 'SINGLE_CLOSE_ON_MAPCLICK' :
            $this->infoWindowBehaviour = $infoWindowBehaviour;
            break;
        default :
            $this->infoWindowBehaviour = 'MULTIPLE'; // default behaviour of Google Maps V3
            break;       
    }
}

/**
* @function     setInfoWindowTrigger
* @param        $infoWindowTrigger : string : can be 'CLICK' OR 'ONMOUSEOVER'
* @returns      nothing
* @description  Determines if InfoWindow is shown with a click or by mouseover
*/
function setInfoWindowTrigger($infoWindowTrigger)
{
    switch ($infoWindowTrigger)
    {
        case 'MOUSEOVER' :
            $this->infoWindowTrigger = $infoWindowTrigger;
            break;
        default :
            $this->infoWindowTrigger = 'CLICK';
            break;       
    }
}

/**
* @function     showDefaultUI
* @param        $control : boolean
* @returns      nothing
* @description  Tells the v3 API wether to show the default UI (its behaviour) or not
*/
function showDefaultUI($control = true)
{
    $this->showDefaultUI = $control;
}

/**
* @function     showMapTypeControl
* @param        $control : boolean
* @param        $style : string (can be 'HORIZONTAL_BAR', 'DROPDOWN_MENU' or 'DEFAULT')
* @returns      nothing
* @description  Tells the v3 API wether to show the map type control or not
*/
function showMapTypeControl($control = true, $style )
{
    $this->showMapTypeControl = $control;
   
    switch ( $style )
    {
        case 'HORIZONTAL_BAR' :
        case 'DROPDOWN_MENU' :
            $this->mapTypeControlStyle = $style;
            break;
        default :
            $this->mapTypeControlStyle = 'DEFAULT';
            break;   
    }
}

/**
* @function     showNavigationControl
* @param        $control : boolean
* @param        $style : string (can be 'ANDROID', 'DEFAULT', 'SMALL' or 'ZOOM_PAN')
* @returns      nothing
* @description  Tells the v3 API wether to show the navigation control or not
*/
function showNavigationControl($control = true, $style)
{
    $this->showNavigationControl = $control;
    switch ( $style )
    {
        case 'ANDROID' :
        case 'SMALL' :
        case 'ZOOM_PAN' :
            $this->navigationControlStyle = $style;
            break;
        default :
            $this->navigationTypeControlStyle = 'DEFAULT';
            break;   
    }
}

/**
* @function     showScaleControl
* @param        $control : boolean
* @returns      nothing
* @description  Tells the v3 API wether to show the scale control or not
*/
function showScaleControl($control = true)
{
    $this->showScaleControl = $control;
}

/**
* @function     showStreetViewControl
* @param        $control : boolean
* @returns      nothing
* @description  Tells the v3 API wether to show the StreetView control or not
*/
function showStreetViewControl($control = true)
{
    $this->showStreetViewControl = $control;
}

/**
* @function     setScrollwheelZoom
* @param        $swzoom : boolean
* @returns      nothing
* @description  Sets wether scrollwheel zoom is enabled or not
*/
function setScrollwheelZoom($swzoom = true)
{
    $this->enableScrollwheelZoom = $swzoom;
}

/**
* @function     setDoubleClickZoom
* @param        $dczoom : boolean
* @returns      nothing
* @description  Sets wether doubleclick zoom is enabled or not
*/
function setDoubleclickZoom($dczoom = true)
{
    $this->enableDoubleClickZoom = $dczoom;
}

/**
* @function     printGMapJS
* @returns      nothing
* @description  Adds the necessary Javascript for the Google Map API v3
*               (should be called in between the html <head></head> tags)
*/
function printGMapsJS()
{
    $this->apiSensor ? $_sensor = "true" : $_sensor = "false";
   
    echo "\n<!-- Include Google Maps JS -->";
    echo "\n<script src=\"http://maps.google.com/maps/api/js?sensor=$_sensor\" type=\"text/javascript\"></script>\n";
}

/**
 * @function     adjustCenterCoords
 *
 * @param        $lng the map longitude : string
 * @param        $lat the map latitude  : string
 * @description  adjust map center coordinates by the given lat/lon point
 */
private function adjustCenterCoords($lat, $lng)
{
    if ( (strlen((string)$lat) != 0) AND (strlen((string)$lng) != 0) )
    {
        $this->maxLat = (float) max($lat, $this->maxLat);
        $this->minLat = (float) min($lat, $this->minLat);
        $this->maxLng = (float) max($lng, $this->maxLng);
        $this->minLng = (float) min($lng, $this->minLng);
   
        $this->centerLng = (float) ($this->minLng + $this->maxLng) / 2;
        $this->centerLat = (float) ($this->minLat + $this->maxLat) / 2;
    }
}

/**
* @function     addMarker
* @param        $lat : string (latitude)
*               $lng : string (longitude)
*               $tooltip : string (tooltip text)
*               $info : Message to be displayed in an InfoWindow
*               $iconURL : URL to an icon to be displayed instead of the default icon
*               (see for example http://code.google.com/p/google-maps-icons/)
*               $clickable : boolean (true if the marker should be clickable)
* @description  Add's a Marker to be displayed on the Google Map using latitude/longitude
*/
function addMarker($lat, $lng, $tooltip="", $info="", $iconURL="", $clickable=true)
{
    $count = count($this->mapMarkers);
    $this->mapMarkers[$count]['lat']     = $lat;
    $this->mapMarkers[$count]['lng']     = $lng;
    $this->mapMarkers[$count]['tooltip'] = $tooltip;
    $this->mapMarkers[$count]['info']    = $info;
    $this->mapMarkers[$count]['iconURL'] = $iconURL;
    $this->mapMarkers[$count]['clickable'] = $clickable;
   
    $this->adjustCenterCoords($lat, $lng);
}

/**
* @function     addMarkerByAddress
* @param        $lat : string (latitude)
*               $lng : string (longitude)
*               $tooltip : string (tooltip text)
*               $info : Message to be displayed in an InfoWindow
*               $iconURL : URL to an icon to be displayed instead of the default icon
*               (see for example http://code.google.com/p/google-maps-icons/)
*               $clickable : boolean (true if the marker should be clickable)
*               @description  Add's a Marker to be displayed on the Google Map using latitude/longitude
*/
function addMarkerByAddress($address, $tooltip="", $info="", $iconURL="", $clickable=true)
{
    $geoCoder = new simpleGMapGeocoder();
    $result = array();
   
    if (!is_string($address))
    {
 die("All Addresses must be passed as a string");
    }
   
    $result = $geoCoder->getGeoCoords($address);
   
    if ( $result['status'] == "OK" )
    {
        $count = count($this->mapMarkers);
        $this->mapMarkers[$count]['lat']     = $result['lat'];
        $this->mapMarkers[$count]['lng']     = $result['lng'];
        $this->mapMarkers[$count]['tooltip'] = $tooltip;
        $this->mapMarkers[$count]['info']    = $info;
        $this->mapMarkers[$count]['iconURL'] = $iconURL;
        $this->mapMarkers[$count]['clickable'] = $clickable;       

        $this->adjustCenterCoords($result['lat'], $result['lng']);
    }
}

/**
* @function     addCircle
* @param        $lat : string (latitude)
*               $lng : string (longitude)
*               $rad : string (radius of circle in meters)
*               $info : Message to be displayed in an InfoWindow
*               $options : array (options like stroke color etc. for the circle)
* @description  Add's an circle to be displayed on the Google Map using latitude/longitude and radius
*/
function addCircle($lat, $lng, $rad, $info="", $options=array())
{
    $count = count($this->mapCircles);
    $this->mapCircles[$count]['lat']  = $lat;
    $this->mapCircles[$count]['lng']  = $lng;
    $this->mapCircles[$count]['rad']  = $rad;
    $this->mapCircles[$count]['info'] = $info;
   
    /* set options */
    if ( sizeof($options) != 0 )
    {
        $this->mapCircles[$count]['fillColor']     = $options['fillColor'];
        $this->mapCircles[$count]['fillOpacity']   = $options['fillOpacity'];
        $this->mapCircles[$count]['strokeColor']   = $options['strokeColor'];
        $this->mapCircles[$count]['strokeOpacity'] = $options['strokeOpacity'];
        $this->mapCircles[$count]['strokeWeight']  = $options['strokeWeight'];
       
        if ( $options['clickable'] == "" OR $options['clickable'] == false )
        {
            $this->mapCircles[$count]['clickable'] = false;
        }
        else
        {
            $this->mapCircles[$count]['clickable'] = true;
        }
    }
    $this->adjustCenterCoords($lat, $lng);
}

/**
* @function     addRectangle
* @param        $lat1 : string (latitude sw corner)
*               $lng1 : string (longitude sw corner)
*               $lat2 : string (latitude ne corner)
*               $lng2 : string (longitude ne corner)
*               $info : Message to be displayed in an InfoWindow
*               $options : array (options like stroke color etc. for the rectangle)
* @description  Add's a rectangle to be displayed on the Google Map using latitude/longitude for soutwest and northeast corner
*/
function addRectangle($lat1, $lng1, $lat2, $lng2, $info="", $options=array())
{
    $count = count($this->mapRectangles);
    $this->mapRectangles[$count]['lat1'] = $lat1;
    $this->mapRectangles[$count]['lng1'] = $lng1;
    $this->mapRectangles[$count]['lat2'] = $lat2;
    $this->mapRectangles[$count]['lng2'] = $lng2;
    $this->mapRectangles[$count]['info'] = $info;
   
    /* set options */
    if ( sizeof($options) != 0 )
    {
        $this->mapRectangles[$count]['fillColor']     = $options['fillColor'];
        $this->mapRectangles[$count]['fillOpacity']   = $options['fillOpacity'];
        $this->mapRectangles[$count]['strokeColor']   = $options['strokeColor'];
        $this->mapRectangles[$count]['strokeOpacity'] = $options['strokeOpacity'];
        $this->mapRectangles[$count]['strokeWeight']  = $options['strokeWeight'];
       
        if ( $options['clickable'] == "" OR $options['clickable'] == false )
        {
            $this->mapRectangles[$count]['clickable'] = false;
        }
        else
        {
            $this->mapRectangles[$count]['clickable'] = true;
        }
    }
    $this->adjustCenterCoords($lat1, $lng1);
    $this->adjustCenterCoords($lat2, $lng2);   
}

/**
* @function     calculateDistance
* @param        $lat1 : string (latitude location 1)
*               $lng1: string (longitude location 1)
*               $lat2 : string (latitude location 2)
*               $lng2: string (longitude location 2)
*               $unit : km (killometers), m (miles), n (nautical miles), i (inch)
* @description  calculates distance between two locations in given unit (default kilometers)
*/
function calculateDistance($lat1, $lng1, $lat2, $lng2, $unit="km")
{
    $radius = 6371; // mean radius of the earth in kilometers
    $lat1 = (float)$lat1;
    $lat2 = (float)$lat2;
    $lng1 = (float)$lng1;
    $lng2 = (float)$lng2;
   
   
    // calculation of distance in km using Great Circle Distance Formula
    $dist = $radius *
            acos( sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +
                  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($lng2) - deg2rad($lng1)) );
   
    switch ( strtolower($unit) )
    {
        case 'm' :     // miles
            $dist = $dist / 1.609;
            break;
        case 'n' :     // nautical miles
            $dist = $dist / 1.852;
            break;
        case 'i' :     // inch
            $dist = $dist * 39370;
            break;
    }
   
    return $dist;
}

/**
* @function     showMap
* @description  Displays the Google Map on the page
*/
function showMap($zoomToBounds = true)
{
    $this->showDefaultUI ? $_disableDefaultUI = "false" : $_disableDefaultUI = "true";
    $this->showMapTypeControl ? $_mapTypeControl = "true" : $_mapTypeControl = "false";
    $this->showNavigationControl ? $_navigationControl = "true" : $_navigationControl = "false";
    $this->showScaleControl ? $_scaleControl = "true" : $_scaleControl = "false";
    $this->showStreetViewControl ? $_streetViewControl = "true" : $_streetViewControl = "false";
    $this->mapDraggable ? $_mapDraggable = "true" : $_mapDraggable = "false";
    $this->enableScrollwheelZoom ? $_scrollwheelZoom = "true" : $_scrollwheelZoom = "false";
    $this->enableDoubleClickZoom ? $_disableDoubleClickZoom = "false" : $_disableDoubleClickZoom = "true";

    // just set the infoWindowTrigger to lower case so we can use it direct as a string
    $_infowindowtrigger = strtolower($this->infoWindowTrigger);
   
    // create div for the map canvas
    echo "\n<!-- DIV container for the map -->";
    echo "\n<div id=\"gmap_canvas\" style=\"width: ".$this->mapWidth."px; height: ".$this->mapHeight."px;\">\n</div>\n";
       
    // create JS to display the map
    echo "\n<!-- Display the Google Map -->";
    echo "\n<script type=\"text/javascript\">\n".
         "var currentInfoWindow = null;\n".
         "var bounds = new google.maps.LatLngBounds();\n".
  "var latlng = new google.maps.LatLng(".$this->centerLat.", ".$this->centerLng.");\n".
  "var options = {\n".
  "\tzoom: ".$this->zoomLevel.",\n".
  "\tcenter: latlng,\n".
  "\tmapTypeId: google.maps.MapTypeId.".$this->mapType.",\n";
    if ( $this->mapBackgroundColor != "" ) { echo "backgroundColor: '".$this->mapBackgroundColor."',\n"; }
    echo "\tdisableDefaultUI: ".$_disableDefaultUI.",\n".
         "\tmapTypeControl: ".$_mapTypeControl.",\n".
         "\tmapTypeControlOptions: { style: google.maps.MapTypeControlStyle.".$this->mapTypeControlStyle." },\n".
         "\tnavigationControl: ".$_navigationControl.",\n".
         "\tnavigationControlOptions: { style: google.maps.NavigationControlStyle.".$this->navigationControlStyle." },\n".
         "\tscaleControl: ".$_scaleControl.",\n".
         "\tstreetViewControl: ".$_streetViewControl.",\n".
         "\tdraggable: ".$_mapDraggable.",\n".
         "\tscrollwheel: ".$_scrollwheelZoom.",\n".
         "\tdisableDoubleClickZoom: ".$_disableDoubleClickZoom."\n".
         "};\n\n".
         "function showmap() {\n".
         "\tvar map = new google.maps.Map(document.getElementById('gmap_canvas'), options);\n\n";
   
    // infoWindowBehaviour    
    if ( ($this->infoWindowBehaviour == 'CLOSE_ON_MAPCLICK') OR ($this->infoWindowBehaviour == 'SINGLE_CLOSE_ON_MAPCLICK') )
    {
        echo "\tgoogle.maps.event.addListener(map, 'click', function() { if (currentInfoWindow != null) { currentInfoWindow.close(); } });\n";
    }

    /*
     * Run through the mapMarkers array to display markers on the map
     */
    for ( $count = 0; $count < sizeof($this->mapMarkers); $count++ )
    {
        // place the marker on the map
        echo "\tvar markerLatLng = new google.maps.LatLng(".$this->mapMarkers[$count]['lat'].", ".$this->mapMarkers[$count]['lng'].");\n".
             "\tvar marker$count = new google.maps.Marker({\n".
             "\t position: markerLatLng,\n".
             "\t title: '".$this->mapMarkers[$count]['tooltip']."',\n";
        if ( $this->mapMarkers[$count]['iconURL'] != "" )
        {
             echo "\t icon: '".$this->mapMarkers[$count]['iconURL']."',\n";
        }
        if ( $this->mapMarkers[$count]['clickable'] == false )
        {
             echo "\t clickable: false,\n";
        }
        echo "\t map: map\n".
             "\t});\n";
            
        // add an InfoWindow if there is a text to be displayed
        if ( $this->mapMarkers[$count]['info'] != "")
        {
            // create InfoWindow
            echo "\tvar infowindowM$count = new google.maps.InfoWindow({\n".
                 "\t content: '".$this->mapMarkers[$count]['info']."'\n".
                
                 "\t});\n";
            // add an event to the marker
            echo "\tgoogle.maps.event.addListener (marker$count, '$_infowindowtrigger', function() {\n";
            // infoWindowBehaviour    
            if ( ($this->infoWindowBehaviour == 'SINGLE') OR ($this->infoWindowBehaviour == 'SINGLE_CLOSE_ON_MAPCLICK') )
            {
                echo "\t if (currentInfoWindow != null) { currentInfoWindow.close(); } \n";
            }
            echo "\t infowindowM$count.open(map, marker$count);\n".
                 "\t currentInfoWindow = infowindowM$count;\n".
                 "\t});\n";
        }
        echo "\tbounds.extend(markerLatLng);\n\n";
    }

    /*
     * Run through the mapCircles array to display circles on the map
     */
    for ( $count = 0; $count < sizeof($this->mapCircles); $count++ )
    {
        // place the circle on the map
        echo "\tvar circleLatLng = new google.maps.LatLng(".$this->mapCircles[$count]['lat'].", ".$this->mapCircles[$count]['lng'].");\n".
             "\tvar circle$count = new google.maps.Circle({\n".
             "\t center: circleLatLng,\n".
             "\t radius: ".$this->mapCircles[$count]['rad'].",\n";
        // check if there are options set for the circle    
        if ( $this->mapCircles[$count]['fillColor'] != "" )     { echo "\t fillColor: '".$this->mapCircles[$count]['fillColor']."',\n"; }
        if ( $this->mapCircles[$count]['fillOpacity'] != "" )   { echo "\t fillOpacity: ".$this->mapCircles[$count]['fillOpacity'].",\n"; }
        if ( $this->mapCircles[$count]['strokeColor'] != "" )   { echo "\t strokeColor: '".$this->mapCircles[$count]['strokeColor']."',\n"; }
        if ( $this->mapCircles[$count]['strokeOpacity'] != "" ) { echo "\t strokeOpacity: ".$this->mapCircles[$count]['strokeOpacity'].",\n"; }
        if ( $this->mapCircles[$count]['strokeWeight'] != "" )  { echo "\t strokeWeight: ".$this->mapCircles[$count]['strokeWeight'].",\n"; }
        if ( $this->mapCircles[$count]['clickable'] == false )  { echo "\t clickable: false,\n"; }
        echo "\t map: map\n".
             "\t});\n";
            
        // add an InfoWindow if there is a text to be displayed and circle is clickable
        if ( ($this->mapCircles[$count]['info'] != "") AND ($this->mapCircles[$count]['clickable'] != false) )
        {
            // create InfoWindow
            echo "\tvar infowindowC$count = new google.maps.InfoWindow({\n".
                 "\t content: '".$this->mapCircles[$count]['info']."',\n".
                 "\t position: circleLatLng,\n".
                 "\t});\n";
            // add an event to the marker
            echo "\tgoogle.maps.event.addListener (circle$count, '$_infowindowtrigger', function() {\n";
            // infoWindowBehaviour    
            if ( ($this->infoWindowBehaviour == 'SINGLE') OR ($this->infoWindowBehaviour == 'SINGLE_CLOSE_ON_MAPCLICK') )
            {
                echo "\t if (currentInfoWindow != null) { currentInfoWindow.close(); } \n";
            }
            echo "\t var tmplat1 = circle$count.getCenter().lat()+(circle$count.getBounds().getNorthEast().lat() - circle$count.getCenter().lat())/2;\n";
            echo "\t var tmplng1 = circle$count.getCenter().lng()+(circle$count.getBounds().getNorthEast().lng() - circle$count.getCenter().lng())/2;\n";
            echo "\t var newpos = new google.maps.LatLng(tmplat1, tmplng1);\n";
            echo "\t infowindowC$count.open(map);\n".
                 "\t infowindowC$count.setPosition(newpos);\n".
                 "\t currentInfoWindow = infowindowC$count;\n".
                 "\t});\n";
        }

        echo "\tbounds.extend(circle$count.getBounds().getNorthEast());\n";
        echo "\tbounds.extend(circle$count.getBounds().getSouthWest());\n\n";
    }

    /*
     * Run through the mapRectangles array to display circles on the map
     */
    for ( $count = 0; $count < sizeof($this->mapRectangles); $count++ )
    {
        // place the rectangle on the map
        echo "\tvar rectangleSW = new google.maps.LatLng(".$this->mapRectangles[$count]['lat1'].", ".$this->mapRectangles[$count]['lng1'].");\n".
             "\tvar rectangleNE = new google.maps.LatLng(".$this->mapRectangles[$count]['lat2'].", ".$this->mapRectangles[$count]['lng2'].");\n".
             "\tvar rectangleBounds = new google.maps.LatLngBounds(rectangleSW,rectangleNE);\n".
             "\tvar rectangle$count = new google.maps.Rectangle({\n".
             "\t bounds: rectangleBounds,\n";
        // check if there are options set for the rectangle    
        if ( $this->mapRectangles[$count]['fillColor'] != "" )     { echo "\t fillColor: '".$this->mapRectangles[$count]['fillColor']."',\n"; }
        if ( $this->mapRectangles[$count]['fillOpacity'] != "" )   { echo "\t fillOpacity: ".$this->mapRectangles[$count]['fillOpacity'].",\n"; }
        if ( $this->mapRectangles[$count]['strokeColor'] != "" )   { echo "\t strokeColor: '".$this->mapRectangles[$count]['strokeColor']."',\n"; }
        if ( $this->mapRectangles[$count]['strokeOpacity'] != "" ) { echo "\t strokeOpacity: ".$this->mapRectangles[$count]['strokeOpacity'].",\n"; }
        if ( $this->mapRectangles[$count]['strokeWeight'] != "" )  { echo "\t strokeWeight: ".$this->mapRectangles[$count]['strokeWeight'].",\n"; }
        if ( $this->mapRectangles[$count]['clickable'] == false )  { echo "\t clickable: false,\n"; }
        echo "\t map: map\n".
             "\t});\n";
            
        // add an InfoWindow if there is a text to be displayed and rectangle is clickable
        if ( ($this->mapRectangles[$count]['info'] != "") AND ($this->mapRectangles[$count]['clickable'] != false) )
        {
            // create InfoWindow
            echo "\tvar infowindowR$count = new google.maps.InfoWindow({\n".
                 "\t content: '".$this->mapRectangles[$count]['info']."',\n".
                 "\t position: rectangleNE,\n".
                 "\t});\n";
            // add an event to the marker
            echo "\tgoogle.maps.event.addListener (rectangle$count, '$_infowindowtrigger', function() {\n";
            // infoWindowBehaviour    
            if ( ($this->infoWindowBehaviour == 'SINGLE') OR ($this->infoWindowBehaviour == 'SINGLE_CLOSE_ON_MAPCLICK') )
            {
                echo "\t if (currentInfoWindow != null) { currentInfoWindow.close(); } \n";
            }
            echo "\t infowindowR$count.open(map);\n".
                 "\t currentInfoWindow = infowindowR$count;\n".
                 "\t});\n";
        }

        echo "\tbounds.extend(rectangleNE);\n";
        echo "\tbounds.extend(rectangleSW);\n\n";
    }

    if ( $zoomToBounds )
    {
        echo "\tmap.fitBounds(bounds);\n";
    }
    echo "}\n\n".
         "window.onload = showmap;\n".
  "</script>\n";
}

}

class simpleGMapGeocoder {
   
/**
* @function     getGeoCoords
* @param        $address : string
* @returns      -
* @description  Gets GeoCoords by calling the Google Maps geoencoding API
*/
function getGeoCoords($address)
{
    $coords = array();
   
    /*
      OBSOLETE, now using utf8_encode
     
      // replace special characters (eg. German "Umlaute")
      $address = str_replace("?", "ae", $address);
      $address = str_replace("?", "oe", $address);
      $address = str_replace("?", "ue", $address);
      $address = str_replace("?", "Ae", $address);
      $address = str_replace("?", "Oe", $address);
      $address = str_replace("?", "Ue", $address);
      $address = str_replace("?", "ss", $address);
    */
   
    $address = utf8_encode($address);
   
    // call geoencoding api with param json for output
    $geoCodeURL = "http://maps.google.com/maps/api/geocode/json?address=".
                  urlencode($address)."&sensor=false";
   
    $result = json_decode(file_get_contents($geoCodeURL), true);
               
    $coords['status'] = $result["status"];
    $coords['lat'] = $result["results"][0]["geometry"]["location"]["lat"];
    $coords['lng'] = $result["results"][0]["geometry"]["location"]["lng"];
   
    return $coords;
}

/**
* WORK IN PROGRESS...
*
* @function     reverseGeoCode
* @param        $lat : string
* @param        $lng : string
* @returns      -
* @description  Gets Address for the given LatLng by calling the Google Maps geoencoding API
*/
function reverseGeoCode($lat,$lng)
{
    $address = array();
   
    // call geoencoding api with param json for output
    $geoCodeURL = "http://maps.google.com/maps/api/geocode/json?address=$lat,$lng&sensor=false";
   
    $result = json_decode(file_get_contents($geoCodeURL), true);
               
    $address['status'] = $result["status"];
   
    echo $geoCodeURL."<br />";
    print_r($result);
   
    return $address;
}

/**
* @function     getOSMGeoCoords
* @param        $address : string
* @returns      -
* @description  Gets GeoCoords by calling the OpenStreetMap geoencoding API
*/
function getOSMGeoCoords($address)
{
    $coords = array();
       
    $address = utf8_encode($address);
   
    // call OSM geoencoding api
    // limit to one result (limit=1) without address details (addressdetails=0)
    // output in JSON
    $geoCodeURL = "http://nominatim.openstreetmap.org/search?format=json&limit=1&addressdetails=0&q=".
                  urlencode($address);
   
    $result = json_decode(file_get_contents($geoCodeURL), true);
   
    $coords['lat'] = $result[0]["lat"];
    $coords['lng'] = $result[0]["lon"];

    return $coords;
}

}

//以下为应用实例

$map = new simpleGMapAPI();
$geo = new simpleGMapGeocoder();

$map->setWidth(600);
$map->setHeight(600);
$map->setBackgroundColor('#d0d0d0');
$map->setMapDraggable(true);
$map->setDoubleclickZoom(false);
$map->setScrollwheelZoom(true);

$map->showDefaultUI(false);
$map->showMapTypeControl(true, 'DROPDOWN_MENU');
$map->showNavigationControl(true, 'DEFAULT');
$map->showScaleControl(true);
$map->showStreetViewControl(true);

$map->setZoomLevel(14); // not really needed because showMap is called in this demo with auto zoom
$map->setInfoWindowBehaviour('SINGLE_CLOSE_ON_MAPCLICK');
$map->setInfoWindowTrigger('CLICK');

$map->addMarkerByAddress("Ravensberger Park 1 , Bielefeld", "Ravensberger Spinnerei", "Ravensberger Spinnerei", "http://google-maps-icons.googlecode.com/files/museum-historical.png");
$map->addMarkerByAddress("Universit鋞sstra遝 25, Bielefeld", "Universit鋞 Bielefeld", "<a href=\"http://www.uni-bielefeld.de\" target=\"_blank\">http://www.uni-bielefeld.de</a>", "http://google-maps-icons.googlecode.com/files/university.png");
$map->addMarker(52.0149436, 8.5275128, "Sparrenburg Bielefeld", "Sparrenburg, 33602 Bielefeld, Deutschland<br /><img src=\"http://www.bielefeld.de/ftp/bilder/sehenswuerdigkeiten/sehenswuerdigkeiten/sparrenburg-bielefeld-435.gif\"", "http://google-maps-icons.googlecode.com/files/museum-archeological.png");

$opts = array('fillColor'=>'#0000dd', 'fillOpacity'=>0.2, 'strokeColor'=>'#000000', 'strokeOpacity'=>1, 'strokeWeight'=>2, 'clickable'=>true);
$map->addCircle(52.0149436, 8.5275128, 1500, "1,5km Umgebung um die Sparrenburg", $opts);

$opts = array('fillColor'=>'#00dd00', 'fillOpacity'=>0.2, 'strokeColor'=>'#003300', 'strokeOpacity'=>1, 'strokeWeight'=>2, 'clickable'=>true);
$map->addRectangle(52.0338, 8.487, 52.0414, 8.502, "Campus University Bielefeld", $opts);

echo "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"\n'>http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
echo "<html xmlns=\"\n'>http://www.w3.org/1999/xhtml\">\n";
echo "<head>\n";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\" />\n";
echo "<title>simpleGMapAPI test</title>";

$map->printGMapsJS();

echo "</head>\n";
echo "\n\n<body>\n\n";

// showMap with auto zoom enabled
$map->showMap(true);

echo "</body>\n";
echo "</html>\n";

?

  评论这张
 
阅读(1429)| 评论(1)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017