0

我对 OOP PHP 很陌生,我写了一个类来从谷歌的地图 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 回答