0

私はGoogleMapsAPIを使用してアドレスの緯度と経度を取得していますが、奇妙なこと#に、文字列に記号が含まれていると、Googleマップによって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)

ポンド記号を削除すると、すべてが正しく機能します。ハッシュ記号に関するGoogleの問題は何ですか?

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パラメータの一部としてではなくアンカーとして処理し、応答がxmlではなくjsonになるため、outputパラメータは無視されます。

于 2013-01-18T11:14:29.660 に答える