2014-06-09 108 views
0

我找到了類將Google地址轉換爲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/lng給變量,但我無法將對象轉換爲字符串。我怎樣才能做到這一點?

$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 echo $ res [0] - > location-> lat work?」 – Veedrac

回答

3

要獲得 'LAT' 和 '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對象'公共屬性? –

+0

馬克M是對的。 'stdClass'具有所有公共動態屬性。第二組電話是您想要的正確電話。 –