0

我正在使用 GoogleMaps API 来获取地址的经纬度,但奇怪的#是,当字符串中存在符号时,Google Maps 会导致 PHP 内存不足......

编码:

//some attempted addresses: "1234 Memory Lane Suite #1", "4321 Test Dr #4", "#" 
$this->address = htmlentities($_POST['address']);
    $googlemaps = new GoogleMaps(GOOGLE_MAPS_API_KEY);
    $coordinates = $googlemaps->getCoordinates(
        'UNITED STATES, ' . 
        $this->state . ', ' . 
        $this->city . ', ' .
        $this->address
    );

错误:

Allowed memory size of 268435456 bytes exhausted (tried to allocate 19574456 bytes)

当我去掉英镑符号时,一切正常。只是有点好奇谷歌的哈希符号有什么问题?

这是API的源代码,很短:

class GoogleMaps {

    /**
    * The Google Maps API key holder
    * @var string 
    */
    private $mapApiKey;

    /**
    * Class Constructor
    */
    public function __construct() {
    }

    /**
    * Reads an URL to a string
    * @param string $url The URL to read from
    * @return string The URL content
    */
    private function getURL($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $tmp = curl_exec($ch);
        curl_close($ch);
        if ($tmp != false){
            return $tmp;
        }
    }

    /**
    * Get Latitude/Longitude/Altitude based on an address
    * @param string $address The address for converting into coordinates
    * @return array An array containing Latitude/Longitude/Altitude data
    */
    public function getCoordinates($address){
        $address = str_replace(' ','+',$address);
        $url = 'http://maps.google.com/maps/geo?q=' . $address . '&output=xml';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $requestCode = $xml->Response->Status->code;
            if ($requestCode == 200){
                //all is ok
                $coords = $xml->Response->Placemark->Point->coordinates;
                $coords = explode(',',$coords);
                if (count($coords) > 1){
                    if (count($coords) == 3){
                        return array('lat' => $coords[1], 'long' => $coords[0], 'alt' => $coords[2]);
                    } else {
                        return array('lat' => $coords[1], 'long' => $coords[0], 'alt' => 0);
                    }
                }
            }
        }
        //return default data
        return array('lat' => 0, 'long' => 0, 'alt' => 0);
    }


}; //end class`
4

1 回答 1

3

使用 编码地址urlencode(),否则输出参数将被忽略,因为 google 将 # 作为锚点处理,而不是 q 参数的一部分,并且响应将是 json 而不是 xml

于 2013-01-18T11:14:29.660 回答