从地址获取 GPS 坐标的代码 (VB/VB.Net/VBA/VBScript)
我相信 Google API 允许您获取给定地址的坐标。然而,我发现的大多数(或者我应该说全部)示例都不适用于 vb。通常是一些 javascript 示例让我感到困惑。
这是我使用地理编码服务的一些代码。这很好用。只是我想自己直接查询Google Maps。
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
I believe the Google API allows you to get coordinates for a given address. However, most (or should I say all) of the examples I've found aren't for vb. Usually it's some javascript example that just leaves me confused.
Here's some code I have that uses a geocoding service. This works great. It's just that I want to query Google Maps directly myself.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以完成工作。您需要通过“工具”>“添加对 MSXML6 库(Microsoft XML,v6.0)的引用” Excel 中的引用
与您的示例的唯一真正区别是:
显然,我的代码中没有任何错误处理,但它应该让您了解使用 API 需要什么。您绝对需要亲自查阅API 文档以了解速率限制等信息
This should do the job. You need to add a reference to the MSXML6 library (Microsoft XML, v6.0) via Tools > References in Excel
The only real differences to your example are:
Obviously there's no error handling whatsoever in my code but it should give you an idea of what you need to use the API. You definitely want to consult the API documentation yourself to find out about rate limiting etc
感谢您的代码!效果很好。感谢您对速率限制的提醒。 Google 的声明如下:
使用 Google Geocoding API 的查询限制为每天 2,500 个地理定位请求。 (Google Maps API Premier 的用户每天最多可以执行 100,000 个请求。)实施此限制是为了防止滥用和/或重新利用地理编码 API,并且此限制将来可能会更改,恕不另行通知。此外,我们还实施请求率限制以防止滥用服务。如果您超出 24 小时限制或以其他方式滥用该服务,地理编码 API 可能会暂时停止为您工作。如果您继续超出此限制,您对地理编码 API 的访问可能会被阻止。
注意:Geocoding API 只能与 Google 地图结合使用;禁止对结果进行地理编码而不将其显示在地图上。有关允许使用的完整详细信息,请参阅 Maps API 服务条款许可限制。
Thanks for that code! It works great. Thanks for the heads up on the rate limits. Here's what Google has to say:
Use of the Google Geocoding API is subject to a query limit of 2,500 geolocation requests per day. (User of Google Maps API Premier may perform up to 100,000 requests per day.) This limit is enforced to prevent abuse and/or repurposing of the Geocoding API, and this limit may be changed in the future without notice. Additionally, we enforce a request rate limit to prevent abuse of the service. If you exceed the 24-hour limit or otherwise abuse the service, the Geocoding API may stop working for you temporarily. If you continue to exceed this limit, your access to the Geocoding API may be blocked.
Note: the Geocoding API may only be used in conjunction with a Google map; geocoding results without displaying them on a map is prohibited. For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions.