Google API 距离矩阵返回错误 json 导致 VBA
Google API distancematrix returning wrong json result in VBA
希望有人能指出我正确的方向。我在 VBA 中有一个从 Excel 调用的函数,用于调用从一个邮政编码到另一个邮政编码的距离。到目前为止,一切正常。
只需为荷兰计算距离,但无法将查询字符串限制为国家/地区。现在我有一个邮政编码 2151 KD,我在我的查询字符串中使用它作为发件人或收件人的邮政编码。喜欢:
(我去掉了数字和字母之间的空格)。
当我将它粘贴到浏览器中时,效果很好。它显示正确的位置和距离。 (目的地或起点)。
此查询字符串是使用此 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。
有人知道吗?
抱歉,我无法重现您的错误 - 代码工作正常。
但是如果您只需要来自荷兰的结果,为什么不将该国家/地区添加到查询字符串中呢?
对于您的代码,这意味着
Uri = Replace(Uri, "{0}", fromZip & ",Netherlands")
Uri = Replace(Uri, "{1}", toZip & ",Netherlands")
希望有人能指出我正确的方向。我在 VBA 中有一个从 Excel 调用的函数,用于调用从一个邮政编码到另一个邮政编码的距离。到目前为止,一切正常。
只需为荷兰计算距离,但无法将查询字符串限制为国家/地区。现在我有一个邮政编码 2151 KD,我在我的查询字符串中使用它作为发件人或收件人的邮政编码。喜欢:
(我去掉了数字和字母之间的空格)。
当我将它粘贴到浏览器中时,效果很好。它显示正确的位置和距离。 (目的地或起点)。
此查询字符串是使用此 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。
有人知道吗?
抱歉,我无法重现您的错误 - 代码工作正常。
但是如果您只需要来自荷兰的结果,为什么不将该国家/地区添加到查询字符串中呢?
对于您的代码,这意味着
Uri = Replace(Uri, "{0}", fromZip & ",Netherlands")
Uri = Replace(Uri, "{1}", toZip & ",Netherlands")