使用jQuery从Google Geocoding API读取JSON数据

原因

我正在尝试了解Google Maps Geocoding,以及如何读取它发送回的JSON数据。这是Google寄回的邮件:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1600",
               "short_name" : "1600",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Amphitheatre Pkwy",
               "short_name" : "Amphitheatre Pkwy",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Mountain View",
               "short_name" : "Mountain View",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Santa Clara County",
               "short_name" : "Santa Clara County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "California",
               "short_name" : "CA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "94043",
               "short_name" : "94043",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.4224764,
               "lng" : -122.0842499
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.4238253802915,
                  "lng" : -122.0829009197085
               },
               "southwest" : {
                  "lat" : 37.4211274197085,
                  "lng" : -122.0855988802915
               }
            }
         },
         "place_id" : "ChIJ2eUgeAK6j4ARbn5u_wAGqWA",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}

我收到以下代码的响应:

$("#submit").click(function(event) {
  var address = encodeURIComponent($("#location").val());

  $.ajax({
    type: "GET",
    url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false&key=" + API_KEY,
    dataType: "json",
    success: processJSON
  });

  function processJSON(json) {
    // Do stuff here
  }
});

问题是我不知道该放什么东西processJSON我在这里读教程:https://www.sitepoint.com/ajaxjquery-getjson-simple-example/并在文件.getJSON()http://api.jquery.com/jquery.getjson/但是,我在了解这两个网站时遇到了麻烦。

如果我想获取postal_code,我将如何读取数据?我尝试了这个:

function processJSON(json) {
    // Do stuff here
    alert("Postal Code:" + json.address_components[6].long_name);
  }

我试了一下getJSON(),以及后success

$.getJSON(url, function(json) {
  alert("Postal Code:" + json.address_components[6].long_name);
 });

但是,它们都不会发出任何警报。我究竟做错了什么?提前致谢!

LF00

在ajax之后添加成功功能

$("#submit").click(function(event) {
  var address = encodeURIComponent($("#location").val());

  $.ajax({
    type: "GET",
    url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false&key=" + API_KEY,
    dataType: "json",
    success: processJSON
  }).success(function(data){
    processJSON(data);
  }

  function processJSON(json) {
    // Do stuff here
    console.log(json);
    alert("Postal Code:" + json.results[0].address_components[6].long_name);
  }
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Google Geocoding Without API Key

来自分类Dev

Google Maps Geocoding的API密钥

来自分类Dev

使用Google Geocoding API进行VBA编程

来自分类Dev

使用 jQuery 根据来自 Google Geocoding API JSON 响应的类型获取值

来自分类Dev

Google Maps Geocoding API中的CORS错误

来自分类Dev

如何使用Mockito / Powermock模拟Google的Geocoding API请求?

来自分类Dev

Google 地图 API/Geocoding Uncaught ReferenceError: google is not defined

来自分类Dev

Google Geocoding API返回错误的语言作为响应

来自分类Dev

每秒Google Maps Geocoding API的配额限制与文档不匹配

来自分类Dev

Google Geocoding API不再每次都显示地区

来自分类Dev

C#中的Google Geocoding Json解析问题

来自分类Dev

C#中的Google Geocoding Json解析问题

来自分类Dev

使用JSON.NET和NuGet Googlemapsapi库进行GeoCoding

来自分类Dev

使用Google Maps JavaScript API v3和Geocoding API映射多个位置

来自分类Dev

使用Java从Android Studio中的Google Geocoding API获取经度和纬度值

来自分类Dev

使用jQuery读取Google API JSON

来自分类Dev

Google Geocoding API仅适用于一个字地址

来自分类Dev

Google Geocoding API未返回旧金山国际机场进行SFO查询

来自分类Dev

为什么Google Geocoding API没有密钥就可以工作?

来自分类Dev

Google Geocoding API-返回任何基于邮政编码的有效地址

来自分类Dev

将许多请求发送到Google Maps Geocoding API的正确方法是什么?

来自分类Dev

通过复杂的Google Geocoding JSON对象中的键获取值-Java-Android

来自分类Dev

如何通过URL从Google Geocoding json响应中提取特定元素?

来自分类Dev

如何在JavaScript中使用Mapbox Geocoding API反向对点进行地理编码?

来自分类Dev

Google Geocoding stopped to work for Crimea, Ukraine

来自分类Dev

Google Maps Geocoding不起作用

来自分类Dev

jQuery读取JSON数据

来自分类Dev

jQuery:从 json 读取数据?

来自分类Dev

如何使用Google Geocoding API获取反向地理编码的两个坐标,即一个坐标是源坐标和目标坐标

Related 相关文章

  1. 1

    Google Geocoding Without API Key

  2. 2

    Google Maps Geocoding的API密钥

  3. 3

    使用Google Geocoding API进行VBA编程

  4. 4

    使用 jQuery 根据来自 Google Geocoding API JSON 响应的类型获取值

  5. 5

    Google Maps Geocoding API中的CORS错误

  6. 6

    如何使用Mockito / Powermock模拟Google的Geocoding API请求?

  7. 7

    Google 地图 API/Geocoding Uncaught ReferenceError: google is not defined

  8. 8

    Google Geocoding API返回错误的语言作为响应

  9. 9

    每秒Google Maps Geocoding API的配额限制与文档不匹配

  10. 10

    Google Geocoding API不再每次都显示地区

  11. 11

    C#中的Google Geocoding Json解析问题

  12. 12

    C#中的Google Geocoding Json解析问题

  13. 13

    使用JSON.NET和NuGet Googlemapsapi库进行GeoCoding

  14. 14

    使用Google Maps JavaScript API v3和Geocoding API映射多个位置

  15. 15

    使用Java从Android Studio中的Google Geocoding API获取经度和纬度值

  16. 16

    使用jQuery读取Google API JSON

  17. 17

    Google Geocoding API仅适用于一个字地址

  18. 18

    Google Geocoding API未返回旧金山国际机场进行SFO查询

  19. 19

    为什么Google Geocoding API没有密钥就可以工作?

  20. 20

    Google Geocoding API-返回任何基于邮政编码的有效地址

  21. 21

    将许多请求发送到Google Maps Geocoding API的正确方法是什么?

  22. 22

    通过复杂的Google Geocoding JSON对象中的键获取值-Java-Android

  23. 23

    如何通过URL从Google Geocoding json响应中提取特定元素?

  24. 24

    如何在JavaScript中使用Mapbox Geocoding API反向对点进行地理编码?

  25. 25

    Google Geocoding stopped to work for Crimea, Ukraine

  26. 26

    Google Maps Geocoding不起作用

  27. 27

    jQuery读取JSON数据

  28. 28

    jQuery:从 json 读取数据?

  29. 29

    如何使用Google Geocoding API获取反向地理编码的两个坐标,即一个坐标是源坐标和目标坐标

热门标签

归档