Google API地理编码在运行时使用JavaScript错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Google API地理编码在运行时使用JavaScript错误相关的知识,希望对你有一定的参考价值。
我试图在控制台日志上打印出一个地址。但是,当我运行代码时,我最终得到一个错误说明
{
"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);
});
根据链接https://www.npmjs.com/package/request中给出的第一个示例,格式似乎是正确的
答案
删除链接中地址后的%20(空格):)
另一答案
我使用其他Google Maps API,所以试着帮助你。请使用'address = 1301%20 lombard%20street%20 philadelphia'格式的目标地址
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
你的回答是:
{
"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"
}
以上是关于Google API地理编码在运行时使用JavaScript错误的主要内容,如果未能解决你的问题,请参考以下文章
使 JavaScript Google API 地理编码发生在鼠标点击而不是页面加载上