json — получить API Google Maps в стеке переполнения

Я никогда не был уверен в API.
Мне удалось получить API на веб-странице с помощью jQuery, но теперь мне нужно декодировать местоположение lat и lng ТОЛЬКО из следующего json (https://maps.googleapis.com/maps/api/geocode/json?address=via%20pascal%2033/2%2042123%20reggio%20emilia&ключ =) с использованием PHP и извлечения двух данных в виде переменных:

{
"results" : [
{
"address_components" : [
{
"long_name" : "2",
"short_name" : "2",
"types" : [ "street_number" ]
},
{
"long_name" : "Via Blaise Pascal",
"short_name" : "Via Blaise Pascal",
"types" : [ "route" ]
},
{
"long_name" : "Reggio Emilia",
"short_name" : "Reggio Emilia",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Reggio nell'Emilia",
"short_name" : "Reggio nell'Emilia",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Provincia di Reggio Emilia",
"short_name" : "RE",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Emilia-Romagna",
"short_name" : "Emilia-Romagna",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italia",
"short_name" : "IT",
"types" : [ "country", "political" ]
},
{
"long_name" : "42123",
"short_name" : "42123",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Via Blaise Pascal, 2, 42123 Reggio Emilia RE,
Italia",
"geometry" : {
"location" : {
"lat" : 44.6666003,
"lng" : 10.5960504
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 44.6679492802915,
"lng" : 10.5973993802915
},
"southwest" : {
"lat" : 44.6652513197085,
"lng" : 10.5947014197085
}
}
},
"partial_match" : true,
"place_id" : "ChIJWaiGeqUcgEcR9l6qwloEWgo",
"types" : [ "street_address" ]
}
],
"status" : "OK"}

В конце концов, мне нужно показать пользователю:

«Широта $ широта, а долгота $ лнг»

Спасибо

-2

Решение

Попробуйте этот код, если вам нужен только последний результат.

$myJsonData = file_get_contents("https://maps.googleapis.com/maps/api/geocode/json?address=via%20pascal%2033/2%2042123%20reggio%20emilia&key=");
$myArrayData = json_decode($myJsonData, 1);
foreach($myArrayData['results'] as $myItem){
$lat = $myItem['geometry']['location']['lat'];
$lng = $myItem['geometry']['location']['lng'];
}
printf("Latitude is %s and longitude is %s", $lat, $lng);
-1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector