Это вывод JSON: я хочу получить только значение formatted_address & параметра long_name & короткое имя. Я ссылался на вопросы, которые люди уже задавали, но я не понял их правильно.
ссылается на:
получить город из результатов геокодера?
{
"results" : [
{
"address_components" : [
{
"long_name" : "Jalan Sungai Chua",
"short_name" : "B11",
"types" : [ "route" ]
},
{
"long_name" : "Sungai Chua",
"short_name" : "Sungai Chua",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Selangor",
"short_name" : "Selangor",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Malaysia",
"short_name" : "MY",
"types" : [ "country", "political" ]
},
{
"long_name" : "43000",
"short_name" : "43000",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Jalan Sungai Chua, Sungai Chua, 43000 Kajang, Selangor, Malaysia",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 2.9932816,
"lng" : 101.787477
},
"southwest" : {
"lat" : 2.9932814,
"lng" : 101.7873144
}
},
"location" : {
"lat" : 2.9932815,
"lng" : 101.7873957
},
"location_type" : "GEOMETRIC_CENTER",
"viewport" : {
"northeast" : {
"lat" : 2.994630480291502,
"lng" : 101.7887446802915
},
"southwest" : {
"lat" : 2.991932519708498,
"lng" : 101.7860467197085
}
}
},
"place_id" : "ChIJcSMkCHjLzTERU6wXGzYy14I",
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Petronas Jalan Enggang",
"short_name" : "Petronas Jalan Enggang",
"types" : [ "point_of_interest", "establishment" ]
},
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Selangor",
"short_name" : "Selangor",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Malaysia",
"short_name" : "MY",
"types" : [ "country", "political" ]
},
{
"long_name" : "43000",
"short_name" : "43000",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Petronas Jalan Enggang, Kajang, 43000 Kajang, Selangor, Malaysia",
"geometry" : {
"location" : {
"lat" : 2.994144,
"lng" : 101.78781
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 2.995492980291502,
"lng" : 101.7891589802915
},
"southwest" : {
"lat" : 2.992795019708498,
"lng" : 101.7864610197085
}
}
},
"place_id" : "ChIJN5uCE3jLzTERpHUSvJFj5To",
"types" : [
"bus_station",
"transit_station",
"point_of_interest",
"establishment"]
},
{
"address_components" : [
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "administrative_area_level_4", "political" ]
},
{
"long_name" : "Hulu Langat",
"short_name" : "Hulu Langat",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Selangor",
"short_name" : "Selangor",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Malaysia",
"short_name" : "MY",
"types" : [ "country", "political" ]
},
{
"long_name" : "43000",
"short_name" : "43000",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kajang, 43000 Kajang, Selangor, Malaysia",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 2.9980711,
"lng" : 101.79819
},
"southwest" : {
"lat" : 2.986342,
"lng" : 101.7843368
}
},
"location" : {
"lat" : 2.9919972,
"lng" : 101.7908387
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 2.9980711,
"lng" : 101.79819
},
"southwest" : {
"lat" : 2.986342,
"lng" : 101.7843368
}
}
},
"place_id" : "ChIJsfCmp3jLzTERJuiZjvHEHSA",
"types" : [ "sublocality_level_1", "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Kajang",
"short_name" : "Kajang",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Hulu Langat",
"short_name" : "Hulu Langat",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Selangor",
"short_name" : "Selangor",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Malaysia",
"short_name" : "MY",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Kajang, Selangor, Malaysia",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 3.069515,
"lng" : 101.8336388
},
"southwest" : {
"lat" : 2.8684469,
"lng" : 101.69742
}
},
"location" : {
"lat" : 2.993518,
"lng" : 101.7874058
},
Моя функция PHP, чтобы получить обратное геокодирование для заданного широты и долготы
public function get_location()
{
$this->location = $param;
$location = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=2.993518,101.7874058&sensor=true');
return $location;
}
Я попытался разобрать так:
$search = new search();
$data = $search->get_location();//caries the JSON data
$return['json']= json_encode($data);
$data = json_decode($return['json'], true);
Но как настроить таргетинг на длинное имя, форматированный адрес и так далее?
В следующем примере показано, как анализировать и получать доступ Сервис геокодирования результат long_name
& short_name
свойства в PHP:
$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?latlng=2.993518,101.7874058&sensor=true');
$data = json_decode($json, true);
foreach ($data['results'] as $item) {
if( !empty( $item['address_components'] ) ){
$longName = $item['address_components'][0]['long_name'];
$shortName = $item['address_components'][0]['short_name'];
print_r('Long Name:' . $longName);
print_r(' Short Name:' . $shortName);
}
}
Во-первых, вы можете прочитать все о типах результатов геокодера здесь -> https://developers.google.com/maps/documentation/geocoding/intro
Я бы преобразовал JSON в stdClass
для удобства :
$location = json_decode($location);
Затем проанализируйте это так:
$geoResults = [];
foreach($location->results as $result){
$geoResult = [];
foreach ($result->address_components as $address) {
if ($address->types[0] == 'country') {
$geoResult['country'] = $address->long_name;
}
if ($address->types[0] == 'administrative_area_level_1') {
$geoResult['state'] = $address->long_name;
}
if ($address->types[0] == 'administrative_area_level_2') {
$geoResult['county'] = $address->long_name;
}
if ($address->types[0] == 'locality') {
$geoResult['city'] = $address->long_name;
}
if ($address->types[0] == 'postal_code') {
$geoResult['postal_code'] = $address->long_name;
}
if ($address->types[0] == 'route') {
$geoResult['route'] = $address->long_name;
}
}
$geoResults[] = $geoResult;
}
Теперь у вас есть $geoResults
массив с именованными свойствами:
Array
(
[0] => Array
(
[route] => Jalan Sungai Chua
[city] => Kajang
[state] => Selangor
[country] => Malaysia
[postal_code] => 43000
)
[1] => Array
(
[city] => Kajang
[state] => Selangor
[country] => Malaysia
[postal_code] => 43000
)
[2] => Array
(
[city] => Kajang
[state] => Selangor
[country] => Malaysia
[postal_code] => 43000
)
[3] => Array
(
[city] => Kajang
[state] => Selangor
[country] => Malaysia
)
[4] => Array
(
[state] => Selangor
[country] => Malaysia
)
[5] => Array
(
[postal_code] => 43000
[state] => Selangor
[country] => Malaysia
)
[6] => Array
(
[state] => Selangor
[country] => Malaysia
)
[7] => Array
(
[state] => Selangor
[country] => Malaysia
)
[8] => Array
(
[country] => Malaysia
)
)
Вы можете перебрать все найденные города следующим образом:
foreach($geoResults as $result) {
echo isset($result['city']) ? $result['city'] : 'N/A';
echo '<br>';
}