简体   繁体   English

来自Google Maps Api JSON的经度和纬度?

[英]Longitude and Latitude from Google Maps Api JSON?

I have this JSON from 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"
}

my question is, how to get this part from that JSON : 我的问题是,如何从JSON中获取此部分:

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

so that I can use it as PHP variable like this : 这样我就可以像这样使用它作为PHP变量:

$lat = -6.2087634;
$lng = 106.845599;

and here's my JSON request to Google Maps API : 这是我对Google Maps API的JSON请求:

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

thank you. 谢谢。

Access it, do checks first: 访问它,首先检查:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用PHP JSON对象从Google Maps API获取5个附近地点的经度和纬度? - How to fetch Latitude and Longitude of 5 Nearby Places from Google Maps API using PHP JSON object? 从mysql检索经度,并将其传递给Google Maps API - Retrieving latitude and longitude from mysql, passing it to google maps api 解析谷歌地图API的纬度和经度 - Parsing Google Maps API for Latitude and Longitude 如何从谷歌地图方向API获取经度和纬度数组 - How to get array of latitude and longitude array from google maps direction api 如何使用谷歌地图 api 将邮政编码转换为地理位置(纬度和经度)? - How to convert postcode to Geolocation (latitude & longitude) using google maps api? 检查纬度和经度的公式在没有Google Maps Api的区域内 - Formula to Check Latitude & Longitude is within Area without Google Maps Api 如何从Wordpress中的Google Maps ACF字段中删除经纬度? - How to remove latitude and longitude from Google Maps ACF field in Wordpress? 在Google地图中设置纬度和经度-未定义结果 - Setting Latitude and Longitude in Google Maps - undefined results 使用纬度和经度在谷歌地图中搜索附近 - Near by search in google maps using latitude and longitude 使用纬度和经度显示谷歌地图 - DISPLAY Google maps using Latitude & Longitude
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM