저는 OOP PHP를 처음 사용하고 있으며 Google의지도 API에서 Lat/Long을 얻기 위해 클래스를 작성했습니다. 이것은 잘 작동하기 때문에 다소 부적절합니다. 내가 클래스 객체 생성 할 때객체에서 반환 된 배열 값을 변수 PHP로 가져옵니다.
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']));
을 내가 배열을 얻으려면 아래와 같이 클래스에서
은 내가 LAT/LONG/주소 값의 배열을 반환 수업에서 돌아온 것, 이것이 내가 붙어있는 곳입니다. 나는 시도했다 :$var = print_r($obj->findCoord($row['address']), true);
그러나 이것은 내가 원하지 않는 문자열로 배열을 반환합니다. 누구든지 도와 줄 수 있습니까?
감사합니다.
$ var = $ obj-> findCoord ($ row [ 'address']); print_r ($ var, true); ? – Hasina
예, 여전히 배열이 아닌 문자열을 반환합니다. –