0
Public Class Geocode
Public Structure GeocodeResult
    Public Latitude As String
    Public Longitude As String
    Public Result As String
End Structure


Public Shared Function GetGeocode(ByVal Address As String) As GeocodeResult
    Dim strLat As String = ""
    Dim strLon As String = ""
    Dim strResult As String = ""
    Dim oXmlDoc As Object
    GetGeocode.Latitude = ""
    GetGeocode.Longitude = ""
    GetGeocode.Result = ""
    Try
        Dim baseURL As String = "https://maps.google.com/maps/api/geocode/xml?sensor=false&address=" & Address
        baseURL = Replace(baseURL, " ", "+")
        oXmlDoc = CreateObject("Microsoft.XMLDOM")
        With oXmlDoc
            .Async = False
            If .Load(baseURL) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then
                GetGeocode.Result = .selectSingleNode("GeocodeResponse/status").Text
                If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then
                    GetGeocode.Latitude = .selectSingleNode("//location/lat").Text
                    GetGeocode.Longitude = .selectSingleNode("//location/lng").Text
                    Return GetGeocode
                End If
            End If
        End With
        oXmlDoc = Nothing
        Return GetGeocode
        Exit Function
    Catch ex As Exception
        Throw (ex)
    End Try
    Return GetGeocode
End Function
End Class

好的,在我们将它移到 Azure VM 之前,这在生产、质量保证和 localhost 中都可以正常工作。我们可以从 VM 使用浏览器访问https://maps.google.com/maps/api/geocode/xml?sensor=false&address= URL。但是当另一个页面调用 getgeocode 函数时,结果总是空白,这意味着其余的 api 调用以某种方式失败。

我不认为它是域密钥限制,因为 a)我在此调用中没有使用密钥,b)我将我的 google api 密钥设置为任何域来测试它。编辑:我尝试过使用另一个具有相同结果的服务。它适用于开发人员和本地计算机,但不适用于 Azure VM。我不明白的是如何通过浏览器访问 REST api,但从代码调用时什么也不返回。有任何想法吗?

4

1 回答 1

0

显式创建 Web 客户端并将其加载到 xml 文档中为我解决了这个问题。

Imports Microsoft.VisualBasic
Imports System.Xml
Imports System.Linq
Imports System.Xml.Linq
Imports System.Net
Imports System.IO

Public Class Geocode
    Public Structure GeocodeResult
        Public Latitude As String
        Public Longitude As String
        Public Result As String
    End Structure


    Public Shared Function GetGeocode(ByVal Address As String) As GeocodeResult
        Dim oXmlDoc As Object
        GetGeocode.Latitude = ""
        GetGeocode.Longitude = ""
        GetGeocode.Result = ""
        Try
            Dim baseURL As String = "https://maps.google.com/maps/api/geocode/xml?sensor=false&address=" & Address
            baseURL = Replace(baseURL, " ", "+")
            Using WC As New WebClient()
                oXmlDoc = CreateObject("Microsoft.XMLDOM")
                With oXmlDoc
                    .Async = False
                    If .Load(WC.DownloadData(baseURL)) And Not .selectSingleNode("GeocodeResponse/status") Is Nothing Then
                        GetGeocode.Result = .selectSingleNode("GeocodeResponse/status").Text
                        If Not .selectSingleNode("GeocodeResponse/result") Is Nothing Then
                            GetGeocode.Latitude = .selectSingleNode("//location/lat").Text
                            GetGeocode.Longitude = .selectSingleNode("//location/lng").Text
                            Return GetGeocode
                        End If
                    End If
                End With
                oXmlDoc = Nothing
            End Using

            Return GetGeocode
            Exit Function
        Catch ex As Exception
            Throw (ex)
        End Try
        Return GetGeocode
    End Function
End Class
于 2013-04-05T18:29:02.713 回答