2013-06-07 2 views
0

저는 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); 

그러나 이것은 내가 원하지 않는 문자열로 배열을 반환합니다. 누구든지 도와 줄 수 있습니까?

감사합니다.

+0

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

+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; 
?> 
+0

PHP 알림 : 정의되지 않은 속성 : latLngFinder :: $ lat $ lat 변수를 선언해야한다고 생각하십니까? 나는 그 일을하는 법을 모른다. –

+0

hold on 나는 vars를 되 돌리는 것을 잊어 버렸다. 2 분 – Dave

+0

ok 나는 지금 오류를 얻지 못했지만 아무 것도 화면에 표시되지 않는다. –

관련 문제