当前位置: 代码迷 >> JavaScript >> 运行时使用JavaScript的Google API Geocode错误
  详细解决方案

运行时使用JavaScript的Google API Geocode错误

热度:107   发布时间:2023-06-07 13:43:12.0

我正在尝试在控制台日志上打印出一个地址。 但是,当我运行代码时,最终会收到一条错误消息,指出

{
   "error_message" : "Invalid request. Missing the 'address', 'components', 'latlng' or 'place_id' parameter.",
   "results" : [],
   "status" : "INVALID_REQUEST"
}

我使用一个称为request的npm模块,试图将地址简单地打印到日志上。 我尝试了其他地址,并继续遇到相同的问题。 我到底在这里想念什么?

const request = require('request');

    request({
      url: 'https://maps.googleapis.com/maps/api/geocode/json?address%20=%201301%20lombard%20street%20philadelphia',
      //json = true
    }, (error, response, body) => {
      console.log(body);
    });

根据链接给出的第一个示例,格式似乎是正确的

删除链接中地址后的%20(空格):)

我使用其他Google Maps API,因此请尝试为您提供帮助。 请使用“地址= 1301%20lombard%20street%20philadelphia”格式的目标地址

https://maps.googleapis.com/maps/api/geocode/json?address=1301%20lombard%20street%20philadelphia&key=PASTE_YOUR_API_KEY

您将需要API密钥。 前往: https://developers.google.com/maps/documentation/javascript/get-api-key : https://developers.google.com/maps/documentation/javascript/get-api-key

您的答复将是:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "1301",
               "short_name" : "1301",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Lombard Street",
               "short_name" : "Lombard St",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Washington Square West",
               "short_name" : "Washington Square West",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "long_name" : "Philadelphia",
               "short_name" : "Philadelphia",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Philadelphia County",
               "short_name" : "Philadelphia County",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Pennsylvania",
               "short_name" : "PA",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "19147",
               "short_name" : "19147",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "1003",
               "short_name" : "1003",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "1301 Lombard St, Philadelphia, PA 19147, USA",
         "geometry" : {
            "location" : {
               "lat" : 39.9444071,
               "lng" : -75.16317189999999
            },
            "location_type" : "RANGE_INTERPOLATED",
            "viewport" : {
               "northeast" : {
                  "lat" : 39.9457560802915,
                  "lng" : -75.16182291970848
               },
               "southwest" : {
                  "lat" : 39.9430581197085,
                  "lng" : -75.16452088029151
               }
            }
         },
         "place_id" : "EiwxMzAxIExvbWJhcmQgU3QsIFBoaWxhZGVscGhpYSwgUEEgMTkxNDcsIFVTQSIbEhkKFAoSCU38VoEkxsaJEedji1ij51aUEJUK",
         "types" : [ "street_address" ]
      }
   ],
   "status" : "OK"
}
  相关解决方案