Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用GoogleGeocodeAPI从地址中获取纬度和经度?_Javascript_Google Geocoding Api - Fatal编程技术网

Javascript 如何使用GoogleGeocodeAPI从地址中获取纬度和经度?

Javascript 如何使用GoogleGeocodeAPI从地址中获取纬度和经度?,javascript,google-geocoding-api,Javascript,Google Geocoding Api,创建一个天气应用程序来学习JavaScript。当页面基于纬度和经度加载时,我已经设法从OpenWeatherMap API获取计算机位置的天气,然后我有一个下拉框,使用google places API自动填充,然后我有一个submit按钮,我想将地点的名称转换为纬度和经度,以便从OpenWeatherMap获取新地点的天气。我不知道该从结果中得出什么结论 第一:JSON数据: "results" : [ { "address_components" : [

创建一个天气应用程序来学习JavaScript。当页面基于纬度和经度加载时,我已经设法从OpenWeatherMap API获取计算机位置的天气,然后我有一个下拉框,使用google places API自动填充,然后我有一个submit按钮,我想将地点的名称转换为纬度和经度,以便从OpenWeatherMap获取新地点的天气。我不知道该从结果中得出什么结论

第一:JSON数据:

   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Tbilisi",
               "short_name" : "Tbilisi",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Didi digomi",
               "short_name" : "Didi digomi",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Tbilisi",
               "short_name" : "Tbilisi",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Georgia",
               "short_name" : "GE",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Tbilisi, Georgia",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.8438937,
                  "lng" : 45.0176811
               },
               "southwest" : {
                  "lat" : 41.6210248,
                  "lng" : 44.6600246
               }
            },
            "location" : {
               "lat" : 41.7151377,
               "lng" : 44.827096
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.8438937,
                  "lng" : 45.0176811
               },
               "southwest" : {
                  "lat" : 41.6210248,
                  "lng" : 44.6600246
               }
            }
         },
         "place_id" : "ChIJa2JP5tcMREARo25X4u2E0GE",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
} 

然后是我的代码,我可以很好地获取状态,但我不知道如何更改它以获取纬度和经度:


var request = new XMLHttpRequest();

                request.open('GET',link,true);
                request.onload = function(){
                var obj2 = JSON.parse(this.response);
                if (request.status >= 200 && request.status < 400) {

                var newLat = (obj2.status);
                document.write(obj2.status);
               // var newLng = (obj2.results.formatted_address);
                alert(newLat);


var request=new XMLHttpRequest();
打开('GET',link,true);
request.onload=函数(){
var obj2=JSON.parse(this.response);
如果(request.status>=200&&request.status<400){
var newLat=(obj2.status);
文件写入(obj2.状态);
//var newLng=(obj2.results.formatted_address);
警报(newLat);
这会导致一个“OK”警报,文档上写着“OK”,但当我试图更改它以提取纬度和经度数据时,它会崩溃


请帮助。

在我看来,您想从
obj2.results[0].geometry.location.lat
obj2.results[0].geometry.location.lng中提取lat/lng是的,太完美了,谢谢。您能解释一下为什么需要[0]?当然。JSON的
results
属性是一个数组。在您提供的示例中,它恰好只包含一个项目,但即使有多个项目,我假设Google会将最佳匹配放在数组的第一位,您可以通过其索引访问该数组。javascript和大多数语言中数组的第一个项目位于索引0处,因此[0]在我的示例中。如果您想要数组中的第二个项目,请放入[1],第三个项目将是[2],以此类推。