我相信Google API允许您获取给定地址的坐标。但是,我发现的大多数(或者我应该说全部)示例都不适用于vb。通常这是一些让我感到困惑的javascript示例。
以下是我使用地理编码服务的一些代码。这非常有效。只是我想直接查询谷歌地图。
Public Function fgGetLatAndLongUsingAddress(sAddress As String) As String
'This function works best with a complete address including the zip code
Dim sResponseText As String, sReturn As String
sReturn = "none"
Dim objHttp As Object, sQuery As String
sQuery = "http://rpc.geocoder.us/service/csv?address=" & Replace(sAddress, " ", "+")
Set objHttp = CreateObject("Msxml2.ServerXMLHTTP")
objHttp.Open "GET", sQuery, False
objHttp.send
sResponseText = objHttp.ResponseText
gsLastLatLongResponseText = sResponseText
Set objHttp = Nothing
If Len(sResponseText) > 0 Then
If InStr(sResponseText, "Bad Request") > 0 Then
'Do Nothing
ElseIf InStr(sResponseText, "couldn't find this address") > 0 Then
'Do Nothing
Else
If InStr(sResponseText, vbCrLf) > 0 Then
'We got more than one result
End If
If InStr(sResponseText, ",") > 0 Then
Dim aryInfo() As String
aryInfo = Split(sResponseText, ",")
sReturn = aryInfo(0) & "," & aryInfo(1)
End If
End If
End If
fgGetLatAndLongUsingAddress = sReturn
End Function
答案 0 :(得分:4)
这应该可以胜任。您需要通过工具>添加对MSXML6库(Microsoft XML,v6.0)的引用。 Excel中的参考资料
Option Explicit
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
' Use the empty string to indicate failure
getGoogleMapsGeocode = ""
Set xhrRequest = New XMLHTTP60
sQuery = "http://maps.googleapis.com/maps/api/geocode/xml?sensor=false&address="
sQuery = sQuery & Replace(sAddr, " ", "+")
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 documentation以了解速率限制等
答案 1 :(得分:0)
感谢您的代码!它很棒。感谢您对速率限制的提升。这就是谷歌所说的:
使用Google地理编码API每天的查询限制为2,500个地理定位请求。 (Google Maps API Premier的用户每天最多可执行100,000次请求。)此限制的使用是为了防止滥用和/或重新使用地理编码API,此限制可能会在将来更改,恕不另行通知。此外,我们强制执行请求速率限制以防止滥用服务。如果超过24小时限制或以其他方式滥用服务,Geocoding API可能会暂时停止为您工作。如果您继续超出此限制,则可能会阻止您访问地理编码API。 注意:地理编码API只能与Google地图结合使用;禁止在地图上显示地理编码结果。有关允许使用的完整详细信息,请参阅Maps API服务条款许可限制。