从地址获取GPS坐标的代码(VB/VB.Net/VBA/VBScript) [英] Code to Get GPS Coordinates from Address (VB/VB.Net/VBA/VBScript)

查看:109
本文介绍了从地址获取GPS坐标的代码(VB/VB.Net/VBA/VBScript)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信Google API可以让您获取给定地址的坐标.但是,我发现的大多数(或我应该说全部)示例都不适合vb.通常,这是一些让我感到困惑的javascript示例.

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.

这里有一些我使用地理编码服务的代码.这很好用.只是我想直接自己查询Google地图.

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

推荐答案

这应该可以完成.您需要通过工具">"Excel中的引用"添加对MSXML6库(Microsoft XML,v6.0)的引用

This should do the job. You need to add a reference to the MSXML6 library (Microsoft XML, v6.0) via Tools > References in 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

您的示例的唯一真正区别是:

The only real differences to your example are:

  • 更改查询的URL和参数以使用Google API
  • 将响应作为XML文档进行处理,并使用 XPath 提取所需的结果
  • changing the URL and parameters of the query to use the Google API
  • treating the response as an XML document and using XPath to extract the required results

很显然,我的代码中没有任何错误处理,但是它应该使您了解使用API​​的必要条件.您肯定想自己查阅 API文档以了解有关速率限制等信息.

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

这篇关于从地址获取GPS坐标的代码(VB/VB.Net/VBA/VBScript)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆