簡體   English   中英

如何通過JavaScript從JSON Google Map API中獲取地理編碼緯度和經度?

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

JSON GEO位置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

$(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());
    });
});

在IBM Worklight中進行測試。

但是我無法獲得經度和緯度

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

alert($。getJSON(jsonLtdLng)); //返回對象Object

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()); //不返回任何內容

您對$.getJSON調用並不完全正確

嘗試這個 :

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

我認為您可以使用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);

我希望這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM