javascript 如何通过javascript从变量中的json google map api获取地理编码纬度和经度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29298260/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 10:20:54  来源:igfitidea点击:

How to get geocode latitude and longitude from json google map api in variables by javascript?

javascriptjsongoogle-mapsgoogle-maps-api-3

提问by sphakrrokr Subhrojit

JSON GEO LOCATION https://maps.googleapis.com/maps/api/geocode/json?address=KOLKATA&key=API_KEY

JSON 地理位置 https://maps.googleapis.com/maps/api/geocode/json?address=KOLKATA&key=API_KEY

{
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "Kolkata",
                   "short_name" : "Kolkata",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Kolkata",
                   "short_name" : "Kolkata",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "West Bengal",
                   "short_name" : "WB",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "India",
                   "short_name" : "IN",
                   "types" : [ "country", "political" ]
                }
             ],
             "formatted_address" : "Kolkata, West Bengal, India",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 23.0078201,
                      "lng" : 88.5428696
                   },
                   "southwest" : {
                      "lat" : 22.3436288,
                      "lng" : 88.19430439999999
                   }
                },
                "location" : {
                   "lat" : 22.572646,
                   "lng" : 88.36389500000001
                },
                "location_type" : "APPROXIMATE",
                "viewport" : {
                   "northeast" : {
                      "lat" : 23.0078201,
                      "lng" : 88.5428696
                   },
                   "southwest" : {
                      "lat" : 22.3436288,
                      "lng" : 88.19430439999999
                   }
                }
             },
             "place_id" : "ChIJZ_YISduC-DkRvCxsj-Yw40M",
             "types" : [ "locality", "political" ]
          }
       ],
       "status" : "OK"
    }

JS

JS

$(function() {
    $("#home_city_select").change(function() {
        //alert( $('option:selected', this).text() );
        var city = document.getElementById("home_city_select").value;
        var ltd;
        var lng;

        var jsonLtdLng="https://maps.googleapis.com/maps/api/geocode/json?address="+city+"&key=API_KEY";
        //alert(JSON.stringify(jsonLtdLng));
        //alert($.getJSON(jsonLtdLng));
        //alert($.getJSON(jsonLtdLng.lat));
        //alert($.getJSON(jsonLtdLng.results.geometry.location.lat));
        //alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));
        //alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat()));
        alert(jsonLtdLng.results[0].geometry.location.lat());
    });
});

Testing in IBM Worklight.

在 IBM Worklight 中进行测试。

But I cannot get the latitude and longitude

但我无法获得经纬度

alert(JSON.stringify(jsonLtdLng));//returns full address

alert(JSON.stringify(jsonLtdLng));//返回完整地址

alert($.getJSON(jsonLtdLng));//returns object Object

alert($.getJSON(jsonLtdLng));//返回对象Object

alert($.getJSON(jsonLtdLng.lat));//returns nothing

alert($.getJSON(jsonLtdLng.lat));//不返回任何内容

alert($.getJSON(jsonLtdLng.results.geometry.location.lat));//returns nothing

alert($.getJSON(jsonLtdLng.results.geometry.location.lat));//不返回任何内容

alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));//returns nothing

alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat));//不返回任何内容

alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat()));//returns nothing

alert($.getJSON(jsonLtdLng.results[0].geometry.location.lat()));//不返回任何内容

alert(jsonLtdLng.results[0].geometry.location.lat());//returns nothing

alert(jsonLtdLng.results[0].geometry.location.lat());//不返回任何内容

回答by jmgross

Your call to $.getJSONwas not totally correct

你的电话$.getJSON不完全正确

Try this :

试试这个 :

$.getJSON(jsonLtdLng, function (data) {
  console.log(data.results[0].geometry.location.lat);
});

回答by alfmonc

I think you can use console.log(), alert()or document.write()as long as the information is deserialized, I am using JSON.parse()for deserializing, eval()is what people used before. Deserializingmeans to recover the data encoded in json so Javascript can use it as an object. In fact, if you use write.document(someJson) you will see [object object] meaning you have all your data there ready to be used, but you need to JSON.parse()it first. JSON.stringnify()is the opposite of what you want to do, because that function is for serializethe data and then transmit it, then the receptor needs to use JSON.parse()to deserialized it and use it in Javascript. I like to use write.document()in order to see the information, but console log allows you to see what is inside of the object easier. Here, some code example:

我觉得你可以用console.log()alert()或者document.write()只要是反序列化的信息,我是JSON.parse()用来反序列化的,eval()以前是人家用的。反序列化意味着恢复以 json 编码的数据,以便 Javascript 可以将其用作对象。事实上,如果你使用 write.document(someJson) 你会看到 [object object] 意味着你已经准备好使用你的所有数据,但你需要先使用JSON.parse()它。 JSON.stringnify()与您想要做的相反,因为该功能用于序列化数据然后传输它,然后接收器需要使用JSON.parse()它来反序列化它并在Javascript中使用它。我喜欢用write.document()以便查看信息,但控制台日志可以让您更轻松地查看对象内部的内容。在这里,一些代码示例:

// apiData would be the variable with the API information
var externalData = JSON.parse(apiData);

// will output [object object]     
document.write(externalData);

// will output  “     “
document.write('      ');

// will output the latitude
document.write(externalData.results[0].geometry.location.lat);

// will output in the console, you need to type externalData in the console log 
console.log(externalData);

I hope this helps.

我希望这有帮助。