希望有人能指出我正确的方向。我在 VBA 中有一个从 Excel 调用的函数,用于调用从一个邮政编码到另一个邮政编码的距离。这工作正常,直到现在。
只需为荷兰计算距离,但不能将查询字符串限制为一个国家/地区。现在我有一个邮政编码 2151 KD,我在我的查询字符串中使用它作为 from 或 to 邮政编码。喜欢:
(我删除了数字和字母之间的空格)。
当我将它粘贴到浏览器中时,这工作正常。它显示了正确的位置和距离。(目的地或起点)。
此查询字符串是使用此 VBA 函数在代码中生成的:
Function GetDistance(fromZip As String, toZip As String) As Integer
Dim Uri As String: Uri = "http://maps.googleapis.com/maps/api/distancematrix/json?origins={0}&destinations={1}&mode=car&sensor=false"
Dim xmlHttp As Object: Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
Dim Json As Object
Dim distance As Double: distance = 0
Uri = Replace(Uri, "{0}", fromZip)
Uri = Replace(Uri, "{1}", toZip)
With xmlHttp
.Open "GET", Uri, False
.setRequestHeader "Content-Type", "text/xml"
.send
End With
'Debug.Print (Uri)
Set Json = JsonConverter.ParseJson(xmlHttp.ResponseText)
'Top-level status OK
If Json("status") = "OK" Then
'Elementstatus
If Json("rows")(1)("elements")(1)("status") = "OK" Then
distance = CLng(Json("rows")(1)("elements")(1)("distance")("value")) / 1000
Else
distance = -2
End If
Else
distance = -1
End If
If fromZip = "1251KD" Or toZip = "1251KD" Then
Debug.Print (xmlHttp.ResponseText)
Debug.Print (Uri)
End If
If IsObject(xmlHttp) Then
Set xmlHttp = Nothing
End If
If IsObject(Json) Then
Set Json = Nothing
End If
GetDistance = Round(distance, 0)
结束功能
但是,结果是不同的。公元 1251 年现在是美国的一个地点。并且没有距离计算。
从 VBA 代码调用时的结果:
{
"destination_addresses" : [ "1211 LW Hilversum, Netherlands" ],
"origin_addresses" : [ "NE 1251, Osceola, MO 64776, USA" ],
"rows" : [
{
"elements" : [
{
"status" : "ZERO_RESULTS"
}
]
}
],
"status" : "OK"
}
直接从浏览器调用时的结果:
{
destination_addresses: [
"1211 LW Hilversum, Nederland"
],
origin_addresses: [
"1251 KD Laren, Nederland"
],
rows: [
{
elements: [
{
distance: {
text: "6,9 km",
value: 6878
},
duration: {
text: "14 min.",
value: 810
},
status: "OK"
}
]
}
],
status: "OK"
}
到目前为止,我还无法弄清楚如何解决这个问题。当我将查询字符串粘贴到浏览器的地址栏中时,查询字符串正在工作,但是从 VBA 调用时,结果无效。到目前为止,仅针对 1251 KD 这个特定的邮政编码。
有人有线索吗?