Home > php教程 > php手册 > php google api 接口程序

php google api 接口程序

WBOY
Release: 2016-05-25 16:38:37
Original
934 people have browsed it

常用的google地图开发参数

phproogle::apiKey

.这是谷歌的用户API密钥,这也可以设置使用时,一个新的实例是instanciated构造.

phproogle::addGoogleMapControl() phproogle::addGoogleMapControl()

此方法设置,如缩放控制控制器的谷歌地图,地图控制等此方法可以多次调用设置地图多个控件,Options are:选项有:

* GLargeMapControl GLargeMapControl

* GSmallMapControl GSmallMapControl

* GSmallZoomControl GSmallZoomControl

* GScaleControl GScaleControl

* GMapTypeControl GMapTypeControl

* GHierarchicalMapTypeControl GHierarchicalMapTypeControl

* GOverviewMapControl GOverviewMapControl

addGoogleMapControl ( "GSmallMapControl" ); ?> 

phproogle::mapType phproogle::mapType

This sets the map type to one of four options:这将设置地图类型的四个选项之一:

* normal正常

* satellite卫星

* hybrid混合

* physical物理

mapType = "normal" ; ?> 

phproogle::mapCenter phproogle::mapCenter

T这将设置分区范围内的地图中心,该值是一个包含的纬度和经度的地图中心的顺序.

$map->mapCenter = array(-33.862828, 151.216974); 1 $map->mapCenter = array(-33.862828, 151.216974); 1 

phproogle::mapZoom phproogle::mapZoom

这将设置地图缩放级别。 零值是最广泛的缩放级别,并显示整个世界。 虽然19日是最高变焦,将显示建筑物。 Each zoom level doubles the zoom.每个缩放级别的两倍变焦。 12.默认值为12。 

phproogle::addMarker() phproogle::addMarker()

This function is used to create and place markers upon the map.这个函数用于创建和地点后,地图标记。 the addMarker method takes three args.在addMarker argS的方法有三个。

* float $latitude浮动$纬度

* float $longitude浮动$经度

* string $html字符串$的HTML

.经度和纬度都是数字值和HTML可以是任何HTML或文本将在标记球囊内。 

phproogle::addMarkerAddress() phproogle::addMarkerAddress()

此方法类似于phproogle::addMarker()和地方在地图上的标记。 然而,而不是接受经度和纬度来放置标志,这种方法需要两个值。

* string $address字符串$地址

* string $html字符串$的HTML

参数的地址,如"二街宫人至法国巴黎简单的地址"。  balloon.在HTML参数再次,任何文本或HTML放置在信息气球。 

phproogle::mapDivID phproogle::mapDivID

当设置,这将设置组ID的映射居住于默认值是"地图" 

phproogle::mapWidth phproogle::mapWidth

顾名思义,这将设置div的宽度该地图居住于默认宽度为350像素。 

phproogle::mapHeight phproogle::mapHeight

这将设置分区的高度该地图居住于默认值是300像素。 

phproogle::googleJS() phproogle::googleJS()

此方法返回的JavaScript字符串,用于在文件头,显示与谷歌API密钥连接字符串最频繁。 

phproogle::drawMap() phproogle::drawMap()

此方法返回的JavaScript制作完成的地图本身.

<?php
class phproogleMap {
    /*
     * @The google api key
    */
    private $apiKey;
    /*
     * @the map zoom level
    */
    private $mapZoom = 8;
    /*
     * @The width of the map div
    */
    private $mapWidth = 350;
    /*
     * @The height of the map div
    */
    private $mapHeight = 300;
    /*
     * @The map center
    */
    private $mapCenter;
    /*
     * The array of map types
    */
    private $googleMapTypes;
    /*
     * @The map type
    */
    private $mapType = &#39;normal&#39;;
    /*
     * @The array of marker points
    */
    private $markerPoints = array();
    /*
     * @The array of marker addresses
    */
    private $markerAddresses = array();
    /*
     * @The maps controls
    */
    private $googleMapControls = array();
    /*
     * @The ID of the map div
    */
    private $mapDivID;
    /*
     * The constructor
     *
     * @param apiKey
     *
     * @access public
     *
     * @return void
     *
    */
    public function __construct($apiKey = null) {
        $this->apiKey = is_null($apiKey) ? &#39;&#39; : $apiKey;
        /*** set the map types ***/
        $this->setGoogleMapTypes();
    }
    /*
     *
     * @setter
     *
     * @access public
     *
    */
    public function __set($name, $value) {
        switch ($name) {
            case &#39;apiKey&#39;:
                if (!is_string($value)) {
                    throw new Exception($name, $value, &#39;string&#39;);
                }
                $this->$name = $value;
                break;
            case &#39;mapZoom&#39;:
                if (filter_var($value, FILTER_VALIDATE_INT, array(
                    "options" => array(
                        "min_range" => 0,
                        "max_range" => 19
                    )
                )) == false) {
                    throw new Exception(" $name is out of range");
                }
                $this->$name = $value;
                break;
            case &#39;mapWidth&#39;:
                if (filter_var($value, FILTER_VALIDATE_INT, array(
                    "options" => array(
                        "min_range" => 100,
                        "max_range" => 900
                    )
                )) == false) {
                    throw new Exception(" $name is out of range for");
                }
                $this->$name = $value;
                break;
            case &#39;mapHeight&#39;:
                if (filter_var($value, FILTER_VALIDATE_INT, array(
                    "options" => array(
                        "min_range" => 100,
                        "max_range" => 900
                    )
                )) == false) {
                    throw new Exception(" $name is out of range for");
                }
                $this->$name = $value;
                break;
            case &#39;mapType&#39;:
                if (!array_key_exists($value, $this->googleMapTypes)) {
                    throw new Exception(" $name is not a valid map type");
                }
                $this->$name = $value;
                break;
            case &#39;mapDivID&#39;:
                if (!is_string($value)) {
                    throw new Exception(" $name is not a valid ID");
                }
                $this->$name = $value;
                break;
            case &#39;mapCenter&#39;:
                if (!is_array($value)) {
                    throw new Exception(" $name is not a valid array");
                }
                $this->$name = $value;
                break;
            default:
                throw new Exception("Invalid Parameter $name ");
        }
    }
    /*
     *
     * @getter
     *
     * @access public
     *
    */
    public function __get($name) {
        switch ($name) {
            case &#39;apiKey&#39;:
                return $this->apiKey;
                break;
            case &#39;mapZoom&#39;:
                return $this->mapZoom;
                break;
            case &#39;mapWidth&#39;:
                return $this->mapWidth;
                break;
            case &#39;mapHeight&#39;:
                return $this->mapHeight;
                break;
            case &#39;mapType&#39;:
                return $this->mapType;
                break;
            case &#39;mapDivID&#39;:
                return $this->mapDivID;
                break;
            case &#39;mapCenter&#39;;
            return $this->mapCenter;
            break;
    }
    /*** if we are here, throw an excepton ***/
    throw new Exception(" $name is invalid");
}
/*
 *
 * @isset
 *
 * @access public
 *
*/
public function __isset($name) {
    switch ($name) {
        case &#39;apiKey&#39;:
            $this->apiKey = $name;
            break;
        case &#39;mapZoom&#39;:
            $this->mapZoom = $name;
            break;
        case &#39;mapWidth&#39;:
            $this->mapWidth = $name;
            break;
        case &#39;mapHeight&#39;:
            $this->mapHeight = $name;
            break;
        case &#39;mapType&#39;:
            $this->mapType = $name;
            break;
        case &#39;mapDivID&#39;:
            $this->mapDivID = $name;
            break;
        case &#39;mapCenter&#39;;
        $this->mapCenter = $name;
        break;
    default:
        return false;
}
}
/*
 *
 * @Set the map types
 *
 * @access private
 *
 * @return void
 *
*/
private function setGoogleMapTypes() {
    $this->googleMapTypes = array(
        &#39;physical&#39; => &#39;G_PHYSICAL_MAP&#39;,
        &#39;normal&#39; => &#39;G_NORMAL_MAP&#39;,
        &#39;satellite&#39; => &#39;G_SATELLITE_MAP&#39;,
        &#39;hybrid&#39; => &#39;G_HYBRID_MAP&#39;
    );
}
/*
 *
 * @add to the array of google maps controls
 *
 * @access public
 *
 * @return void
 *
*/
public function addGoogleMapControl($control) {
    $n = sizeof($this->googleMapControls);
    $this->googleMapControls[] = $control;
}
/*
 *
 * @get pinpoint marker by address
 *
 * @access public
 *
 * @param string $address
 *
 * @param string $html
 *
 * @return void
 *
*/
public function addMarkerAddress($address, $html) {
    $s = sizeof($this->markerAddresses);
    $this->markerAddresses[$s][&#39;address&#39;] = $address;
    $this->markerAddresses[$s][&#39;html&#39;] = $html;
}
/*
 *
 * @get pinpoint mark by latitude or longitude
 *
 * @access public
 *
 * @param string $lat
 *
 * @param string $long
 *
 * @param string $html
 *
 * @return void
 *
*/
public function addMarker($lat, $long, $html) {
    $pointer = sizeof($this->markerPoints);
    $this->markerPoints[$pointer][&#39;lat&#39;] = $lat;
    $this->markerPoints[$pointer][&#39;long&#39;] = $long;
    $this->markerPoints[$pointer][&#39;html&#39;] = $html;
}
/*
 *
 * @The javascript for google to connect
 *
 * @access public
 *
 * @return string
 *
*/
public function googleJS() {
    return &#39;<script src="http://maps.google.com/maps?file=api&v=2&key=&#39; . $this->apiKey . &#39;" type="text/javascript"></script>&#39; . " ";
}
private function noJavascript() {
    return &#39;<noscript><b>JavaScript must be enabled in order for you to use Google Maps.</b> 
    However, it seems JavaScript is either disabled or not supported by your browser. 
    To view Google Maps, enable JavaScript by changing your browser options, and then 
    try again. 
    </noscript>&#39;;
}
public function drawMap() {
    $js = &#39;<div id="&#39; . $this->mapDivID . &#39;" style="width: &#39; . $this->mapWidth . &#39;px; height: &#39; . $this->mapHeight . &#39;px"></div>&#39;;
    $js.= $this->noJavascript();
    $js.= &#39; 
    <script type="text/javascript"> 
    //<![CDATA[ 
    if (GBrowserIsCompatible()) { 
    geocoder = new GClientGeocoder(); 
    function createMarker(point,html) { 
    var marker = new GMarker(point); 
    GEvent.addListener(marker, "click", function() { 
    marker.openInfoWindowHtml(html); 
    }); 
    return marker; 
    } 
    // Display the map, with some controls and set the initial location 
    var map = new GMap2(document.getElementById("&#39; . $this->mapDivID . &#39;"));&#39; . " ";
    /*** set the map controls here ***/
    if (sizeof($this->googleMapControls) > 0) {
        foreach ($this->googleMapControls as $control) {
            $js.= &#39;map.addControl(new &#39; . $control . &#39;());&#39; . " ";
        }
    }
    /*** set the map center, zooom, and type ***/
    list($lat, $long) = $this->mapCenter;
    $js.= &#39;map.setCenter(new GLatLng(&#39; . $lat . &#39;,&#39; . $long . &#39;), &#39; . $this->mapZoom . &#39;, &#39; . $this->googleMapTypes[$this->mapType] . &#39;);&#39; . " ";
    if (sizeof($this->markerAddresses) > 0) {
        foreach ($this->markerAddresses as $add) {
            $base_url = "http://maps.google.com/maps/geo?output=xml" . "&key=" . $this->apiKey;
            $request_url = file_get_contents($base_url . "&q=" . urlencode($add[&#39;address&#39;]));
            $xml = simplexml_load_string($request_url);
            $status = $xml->Response->Status->code;
            $point = $xml->Response->Placemark->Point->coordinates;
            list($long, $lat, $d) = explode(&#39;,&#39;, $point);
            $js.= &#39;var point = new GLatLng(&#39; . $lat . &#39;,&#39; . $long . &#39;);&#39; . " ";
            $js.= "var marker = createMarker(point,&#39;" . $add[&#39;html&#39;] . "&#39;)" . " ";
            $js.= &#39;map.addOverlay(marker);&#39; . " ";
        }
    }
    /*** set the markers here ***/
    foreach ($this->markerPoints as $data) {
        $js.= &#39;var point = new GLatLng(&#39; . $data[&#39;lat&#39;] . &#39;,&#39; . $data[&#39;long&#39;] . &#39;);&#39; . " ";
        $js.= "var marker = createMarker(point,&#39;" . $data[&#39;html&#39;] . "&#39;)" . " ";
        $js.= &#39;map.addOverlay(marker);&#39; . " ";
    }
    $js.= &#39; 
    GMap.prototype.centerAndZoomOnBounds = function(bounds) { 
    // make 10% bigger so all markers show completely 
    var span = new GSize((bounds.maxX - bounds.minX) * 1.1, (bounds.maxY - bounds.minY)*1.1); 
    var center = new GPoint(bounds.minX + span.width / 2., bounds.minY + span.height / 2.); 
    var newZoom = this.spec.getLowestZoomLevel(center, span, this.viewSize); 
    if (this.getZoomLevel() != newZoom) { 
    this.centerAndZoom(center, newZoom); 
    } else { 
    this.recenterOrPanToLatLng(center); 
    } 
    } 
    // display a warning if the browser was not compatible 
    } else { 
    alert("Sorry, the Google Maps API is not compatible with this browser"); 
    } 
    //]]> 
    </script>&#39;;
    return $js;
}
} /*** end of class ***/
try {
    /*** a new phproogle instance ***/
    $map = new phproogleMap();
    /*** the google api key ***/
    $map->apiKey = &#39;YOUR_GOOGLE_API_KEY&#39;;
    /*** zoom is 0 - 19 ***/
    $map->mapZoom = 14;
    /*** the map width ***/
    $map->mapWidth = 350;
    /*** the map height ***/
    $map->mapHeight = 300;
    /*** set the map type ***/
    $map->mapType = &#39;normal&#39;;
    /*** set the map center ***/
    $map->mapCenter = array(-33.862828,
        151.216974
    );
    /*** add some markers with latitude and longitude  ***/
    $map->addMarker(-33.858362, 151.214876, &#39;<h2>Sydney Opera House</h2><p>For those with culture</p>&#39;);
    $map->addMarker(-33.862828, 151.216974, &#39;<h3>Royal Botanic Gardens</h2><a href="http://phpro.org">A link here</a>&#39;);
    /*** add some controls ***/
    $map->addGoogleMapControl(&#39;GMapTypeControl&#39;);
    $map->addGoogleMapControl(&#39;GSmallMapControl&#39;);
    $map->addGoogleMapControl(&#39;GOverviewMapControl&#39;);
    /*** add some marker addresses ***/
    $map->addMarkerAddress(&#39;2 Pitt St Sydney NSW Australia&#39;, &#39;<h2>Head Office</h2>&#39;);
    $map->addMarkerAddress(&#39;122 Pitt St Sydney NSW Australia&#39;, &#39;<h2>The Factory</h2>&#39;);
    /*** set the map div id ***/
    $map->mapDivID = &#39;map&#39;;
}
catch(Exception $e) {
    echo $e->getMessage();
}
    ?>
    <html> 
    <head> 
    <?php echo $map -> googleJS ();  
    </head> 
    <body> 
    <?php echo $map -> drawMap ();  
    </body> 
    </html>
Copy after login


教程链接:

随意转载~但请保留教程地址★

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template