Excel VBA中的Google API地理编码返回空白

问题描述 投票:0回答:1

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

Google启动了新的结算流程,但代码无效。

输入:地址 输出:Lat,Long

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密钥和服务器密钥有什么区别?

enter image description here

excel vba api geocoding api-key
1个回答
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地理编码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月12日'最后更新时间:09/08/2018'电子邮件:[email protected]'网站:https://www.myengineeringworld.net'---------------- -------------------------------------------------- -----------------------------------

'Declaring the necessary variables.

'最后2个变量在末尾使用30,对应于VBA中的“Microsoft XML,v3.0”库(msxml3.dll)。如果您使用它的任何其他版本(例如v6.0),则将这些变量分别声明为XMLHTTP60和DOMDocument60。 Dim ApiKey作为字符串Dim请求为新XMLHTTP30 Dim结果为新DOMDocument30 Dim 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 .........”

© www.soinside.com 2019 - 2024. All rights reserved.