2015-09-05 2 views
0

У меня есть этот JSON от Google Maps API:Долгота и широта от Google Maps Api JSON?

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Jakarta", 
       "short_name" : "Jakarta", 
       "types" : [ "colloquial_area", "locality", "political" ] 
      }, 
      { 
       "long_name" : "Special Capital Region of Jakarta", 
       "short_name" : "Special Capital Region of Jakarta", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "Indonesia", 
       "short_name" : "ID", 
       "types" : [ "country", "political" ] 
      } 
     ], 
     "formatted_address" : "Jakarta, Special Capital Region of Jakarta, Indonesia", 
     "geometry" : { 
      "bounds" : { 
       "northeast" : { 
        "lat" : -6.0886599, 
        "lng" : 106.972825 
       }, 
       "southwest" : { 
        "lat" : -6.3708331, 
        "lng" : 106.686211 
       } 
      }, 
      "location" : { 
       "lat" : -6.2087634, 
       "lng" : 106.845599 
      }, 
      "location_type" : "APPROXIMATE", 
      "viewport" : { 
       "northeast" : { 
        "lat" : -6.0886599, 
        "lng" : 106.972825 
       }, 
       "southwest" : { 
        "lat" : -6.3708331, 
        "lng" : 106.686211 
       } 
      } 
     }, 
     "place_id" : "ChIJnUvjRenzaS4RoobX2g-_cVM", 
     "types" : [ "colloquial_area", "locality", "political" ] 
     } 
    ], 
    "status" : "OK" 
} 

мой вопрос, как получить эту часть из этого JSON:

"location" : { 
    "lat" : -6.2087634, 
    "lng" : 106.845599 
}, 

так, что я могу использовать его в качестве PHP переменной, как это:

$lat = -6.2087634; 
$lng = 106.845599; 

и вот мой запрос JSON в Google Maps API:

$url = 'https://maps.googleapis.com/maps/api/geocode/json?address=indonesia&key=MYKEY'; 
$content = file_get_contents($url); 
$json = json_decode($content, true); 

спасибо.

ответ

3

Access это, сделайте проверку первым:

if(isset($json['results'][0])) { 
    $lat = $json['results'][0]['geometry']['location']['lat']; //-6.2087634 
    $lng = $json['results'][0]['geometry']['location']['lng']; //106.845599 
} 
+0

это работает, братан ... спасибо большого –

+0

Вы Wellcome –

Смежные вопросы