0

私は OOP PHP を初めて使用し、Google のマップ API から緯度/経度を取得するクラスを作成しました。これは問題なく動作するため、無関係です。

クラスから、以下に示すように、LAT/LONG/ADDRESS 値の配列を返します。

    class latLngFinder {

    /**
    * Creates cURL connection to Google API v3
    * @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/Formatted Address based on an address
    * @param string $address The address for finding coordinates
    * @return array An array containing Latitude/Longitude/Formatted Address
    */
    public function findCoord($address){
        $address = str_replace(' ', '+', $address);
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $status = $xml->status;

            // Use this to debug returned XML
            //print_r($xml);

            if ($status == 'OK'){
                // Find Latitude/Longitude/Formatted Address in XML
                $lat = array( (string) $xml->result->geometry->location->lat);
                $lng = array( (string) $xml->result->geometry->location->lng);
                $formatted_addr = array( (string) $xml->result->formatted_address);

            }
        }
        // Return data
        return array('formatted_address' =>  $formatted_addr[0], 'lat' => $lat[0], 'lng' => $lng[0]);

    }


};

クラスオブジェクトを作成すると:

 // Include class
    require_once(dirname(__FILE__) . '/class.latLngFinder.php');

    // Initialise the latLngFinder class
    $obj = new latLngFinder();

    print_r($obj->findCoord($row['address']));

クラスから返された配列を取得したいのですが、ここで行き詰まっています。私は使用してみました:

$var = print_r($obj->findCoord($row['address']), true);

ただし、これは配列を文字列として返しますが、これは望ましくありません。誰でも助けることができますか?

ありがとう。

4

1 に答える 1

0
<?php
class latLngFinder {
    public $output;
    public $lat;
    public $lng;
    public $formatted_address;

    public function __construct($inputaddress)
    {
        $this->address = $inputaddress;
        $this->output = $this->findCoord();
    }

    /**
    * Creates cURL connection to Google API v3
    * @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/Formatted Address based on an address
    * @param string $address The address for finding coordinates
    * @return array An array containing Latitude/Longitude/Formatted Address
    */
    private function findCoord(){
        $address = str_replace(' ', '+', $this->address);
        $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $address . '&sensor=false';
        $data = $this->getURL($url);
        if ($data){
            $xml = new SimpleXMLElement($data);
            $status = $xml->status;

            // Use this to debug returned XML
            //print_r($xml);

            if ($status == 'OK'){
                // Find Latitude/Longitude/Formatted Address in XML
                $lat = $xml->result->geometry->location->lat;
                $lng = $xml->result->geometry->location->lng;
                $formatted_addr = $xml->result->formatted_address;
                $this->lat = $lat;
                $this->lng = $lng;
                $this->formatted_address = $formatted_addr;
            }
        }
    }
}

// Initialise the latLngFinder class
$obj = new latLngFinder($row['address']);
echo $obj->lat;
echo "<br />";
echo $obj->lng;
echo "<br />";
echo $obj->formatted_address;
?>
于 2013-06-07T09:12:26.353 に答える