我是OOP PHP的新手,我写了一个类来从谷歌的地图API获得Lat / Long。这有点无关紧要,因为它工作正常。
从类中我返回一个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);
然而,这会将数组作为我不想要的字符串返回。有人可以帮忙吗?
感谢。
答案 0 :(得分: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;
?>