2014-06-09 5 views
0

Google API를 사용하여 주소를 GPS 경도/위도로 변환하는 클래스를 찾았습니다.PHP에서 객체 배열의 단일 요소를 어떻게 인쇄 할 수 있습니까?

모든 배열을 인쇄 할 수 있습니다 : print_r($geo->adress(my address).

Array 
(
    [0] => stdClass Object 
     (
      [location] => stdClass Object 
       (
        [lat] => 53.1243946 
        [lng] => 18.001958 
       ) 

      [city] => Bydgoszcz 
      [street] => MarszaĹka Focha 
      [number] => 4 
      [code] => 85-070 
     ) 

) 

을 보여줍니다하지만 단일 요소를 인쇄 할 수 없습니다. 변수에 위치 -> 위도/경도를 추가해야하지만 객체를 문자열로 변환 할 수 없습니다. 내가 어떻게 할 수 있니?

$lat = implode(",", $geo->adress('Focha 4, Bydgoszcz'); // doesn't work at all. 

<?php 

echo "<pre>"; 
$geo = new GeoGoogle; 
print_r($geo->adress('Focha 4, Bydgoszcz')); 
echo"</pre>"; 



final class GeoGoogle { 

    // zwróć listę lokacji pasujących do podanego adresu 
    function adress($adress) { 
    return $this->parse(
     $this->prepare(
     'http://maps.googleapis.com/maps/api/geocode/json?address=' 
     .urlencode($adress) 
     .'&sensor=false' 
    ) 
    ); 
    } 

    // zwróć listę lokacji pasujących do podanej długości i szerokości 
    function latlng($lat,$lng) { 
    return $this->parse(
     $this->prepare(
     'http://maps.googleapis.com/maps/api/geocode/json?latlng=' 
     .$lat .','. $lng 
     .'&sensor=false' 
    ) 
    ); 
    } 

    /* pomocnicze */ 

    private function prepare($uri) { 
    return json_decode(file_get_contents($uri)); 
    } 

    private function parse($data) { 
    if($data->status == 'OK') { 
     $results = array(); 
     foreach($data->results as $res) { // przetwórz wyniki 
     $result = array(
      'location' => null, 
      'city' => null, 
      'street' => null, 
      'number' => null, 
      'code' => null 
     ); 
     // pobierz współrzędne 
     if(isset($res->geometry)) { 
      if(isset($res->geometry->location)) { 
      $result['location'] = $res->geometry->location; 
      } 
     } 
     // pobierz dane 
     foreach($res->address_components as $component) { 
      foreach($component->types as $type) { 
      switch($type) { 
       case 'street_number': 
       $result['number'] = $component->long_name; 
       break; 
       case 'route': 
       $result['street'] = $component->long_name; 
       break; 
       case 'postal_code': 
       $result['code'] = $component->long_name; 
       break; 
       case 'sublocality': 
       $result['city'] = $component->long_name; 
       break; 
       case 'administrative_area_level_3': 
       if(is_null($result['city'])) { 
        $result['city'] = $component->long_name; 
       } 
       break; 
      } 
      } 
     } 
     if(!is_null($result['city'])) { 
      $results[] = (object) $result; 
     } 
     } 
     return $results; 
    } 
    return array(); 
    } 
} 
+0

당신은 ** ** 여기에'implode'을 사용할 수 없습니다 액세스하려고하면 개체입니다. –

+0

user3712744는 "echo $ res [0] -> location-> lat work"를 알고 싶어합니다. – Veedrac

답변

3

를 얻으려면 개체에서 '위도'와 'LNG'값이 시도 :

$lat = $object->location['lat']; 
$lng = $object->location['lng']; 
편집

See demo

: 가까운 데이터에서 찾고, 더 도움이 될 수 있습니다.

$lat = $geo[0]->location['lat']; 
$lng = $geo[0]->location['lng']; 

See demo 2

편집 : 관해서는 코멘트에 언급 된 오류로 Cannot use object of type GeoGoogle as array

이 시도 : 값이 때문에

$my_geo = $geo->adress(my address); 

$lat = $my_geo[0]->location->lat; 
$lng = $my_geo[0]->location->lng; 
+0

이 방법은 효과가있을 수 있지만 속성이 공개인지 또는 '__get' 메서드가 정의되어 있는지를 알아야합니다. –

+1

@BrettSantore 내가 틀렸다면 고쳐 주겠다.하지만 기본적으로'StdClass object' public의 속성이 아닌가? –

+0

마크 M이 맞습니다. 'stdClass'는 모든 public dynamic 속성을 가지고 있습니다. 두 번째 호출 집합이 올바른 호출입니다. –

관련 문제