【问题标题】:Google API geocode in Excel VBA returns blankExcel VBA 中的 Google API 地理编码返回空白
【发布时间】:2019-02-01 23:25:50
【问题描述】:

我为地址地理编码创建了一个 Excel VBA 函数。去年有效。

Google 开始了新的结算流程,但代码不起作用。

输入:地址
输出:纬度、经度

Function getGoogleMapsGeocode(sAddr As String) As String

Dim xhrRequest As XMLHTTP60
Dim sQuery As String
Dim domResponse As DOMDocument60
Dim ixnStatus As IXMLDOMNode
Dim ixnLat As IXMLDOMNode
Dim ixnLng As IXMLDOMNode

getGoogleMapsGeocode = ""

Set xhrRequest = New XMLHTTP60
sQuery = "https://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
sQuery = sQuery & Replace(sAddr, " ", "+") & "&key=MY-API-KEY"

xhrRequest.Open "GET", sQuery, False
xhrRequest.send

Set domResponse = New DOMDocument60
domResponse.LoadXML xhrRequest.responseText
Set ixnStatus = domResponse.SelectSingleNode("//status")

If (ixnStatus.Text <> "OK") Then
    Exit Function
End If

Set ixnLat = domResponse.SelectSingleNode("/GeocodeResponse/result/geometry/location/lat")
Set ixnLng = domResponse.SelectSingleNode("/GeocodeResponse/result/geometry/location/lng")

getGoogleMapsGeocode = ixnLat.Text & ", " & ixnLng.Text

End Function

我通过更改一个旧的服务器密钥让它工作,但这带来了另一个问题。

我最近注册了两个不起作用的 API 密钥。但是,旧的服务器密钥有效。 API key 和 server key 有什么区别?

【问题讨论】:

  • "代码不再工作。"不是对您的确切问题的非常有用的描述。当你运行它时会发生什么?
  • 您的代码在此处可以正常工作,具有正确的地址和 api-key。可能你没有用谷歌正确设置你的 api 密钥;或者你的地址搞砸了。但是,正如@TimWilliams 所写,您的问题描述对于故障排除没有用处。
  • 你试过 Postman 看看它是否有效?

标签: excel vba api geocoding api-key


【解决方案1】:

Geocoding VBA Excel 2010

感谢我的兄弟 Edu 的帮助!

选项显式

函数 GetCoordinates(Address As String) As String

'This function returns the latitude and longitude of a given address using the Google Geocoding API.

'函数使用“最简单”形式的Google Geocoding API(只发送地址参数), '因此,不使用边界、语言、区域和组件等可选参数。 '如果有多个结果(例如两个城市名称相同),函数 '返回第一次出现,所以在输入地址时要小心(提示:使用城市名称和 '邮政编码(如果有)。

'NOTE: As Google points out, the use of the Google Geocoding API is subject to a limit of 40,000

' 每月的请求,因此请注意不要超过此限制。欲了解更多信息检查: 'https://cloud.google.com/maps-platform/pricing/sheet

'In order to use this function you must enable the XML, v3.0 library from VBA editor:

'转到工具 -> 参考 -> 检查 Microsoft XML,v3.0。 '如果您没有 v3.0,请使用它的任何其他版本(例如 v6.0)。

'2018 Update: In order to use this function you will now need a valid API key.

'查看下一个链接,该链接将指导您如何获取免费的 API 密钥: 'https://www.myengineeringworld.net/2018/02/how-to-get-free-google-api-key.html

'2018 Update 2 (July): The EncodeURL function was added to avoid problems with special characters.

'这是来自希腊、塞尔维亚、德国和其他国家/地区的地址的常见问题。

'Written By:    Christos Samaras

'日期:2014 年 12 月 6 日 '最后更新时间:2018 年 9 月 8 日 '电子邮件:xristos.samaras@gmail.com '网站:https://www.myengineeringworld.net '---------------------------------------------------- -------------------------------------------------- --

'Declaring the necessary variables.

'前2个变量使用30结尾,对应“Microsoft XML, v3.0”库 '在 VBA (msxml3.dll) 中。如果您使用它的任何其他版本(例如 v6.0),则声明这些变量 '分别为 XMLHTTP60 和 DOMDocument60。 将 ApiKey 调暗为字符串 将请求变暗为新的 XMLHTTP30 暗淡的结果作为新的 DOMDocument30 将 StatusNode 作为 IXMLDOMNode,将 LatitudeNode 作为 IXMLDOMNode,将 LongitudeNode 作为 IXMLDOMNode 调暗

'Set your API key in this variable. Check this link for more info:

'https://www.myengineeringworld.net/2018/02/how-to-get-free-google-api-key.html '****************************************************** ****************************************************** ************* ApiKey = "您的 API 密钥在此处!" '例如:ApiKey = "lxI800lklv3sdf3v5F6........."

【讨论】:

    猜你喜欢
    • 2019-01-15
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 2018-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多