2013-04-04 1 views
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 

Хорошо, так что это отлично работает в производстве, qa и localhost, пока мы не переместим его на Azure VM. С VM мы можем использовать браузер, чтобы перейти на URL-адрес https://maps.google.com/maps/api/geocode/xml?sensor=false&address=. но когда другая страница вызывает функцию getgeocode, результаты всегда пустые, что означает, что вызов api остался каким-то образом.Невозможно вызвать google api из Azure virtual machine

Я не считаю, что это ограничение ключа домена, потому что: a) im не использует ключ в этом вызове и b) я устанавливаю ключ google go для любого домена, чтобы проверить его. EDIT: Я попытался использовать другой сервис с тем же результатом. Он работает в dev и на локальных машинах, но не на Azure VM. То, что я не понимаю, так это то, как доступ к REST-авию доступен через браузер, но при вызове из кода ничего не возвращает. Любые идеи?

ответ

0

Явное создание веб-клиента и загрузка его в документ 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 
Смежные вопросы