2015-06-27 174 views
1

在JSON地理編碼GetResponse中,Microsoft提供的代碼使用函數返回結果。但是,我想使用該函數以外的結果,但之後我無法從函數內訪問數據。也許代碼可以更清楚地說明這一點:函數內的訪問變量

Dim geocodeRequest As New Uri(String.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key)) 
    Dim latlong_adress As BingMapsRESTService.Common.JSON.Location = Nothing 

    GetResponse(geocodeRequest, Function(x) 
            MsgBox(x.ResourceSets(0).Resources.Length & " result(s) found.") 
            latlong_adress = x.ResourceSets(0).Resources(0) 
            'Correct results: 
            MsgBox(latlong_adress.Confidence) 
            MsgBox(latlong_adress.EntityType) 
            MsgBox(latlong_adress.Point.Coordinates(0) & ", " & latlong_adress.Point.Coordinates(1)) 
            Return 0 
           End Function) 
    'Empty: --> is nothing 
    If latlong_adress IsNot Nothing Then 
     MsgBox(latlong_adress.Confidence) 
     MsgBox(latlong_adress.EntityType) 
     MsgBox(latlong_adress.Point.Coordinates(0) & ", " & latlong_adress.Point.Coordinates(1)) 
    End If 

如何在響應後訪問響應中的數據?

回答

0

我解決了我的問題,雖然我不太確定答案的正確性。我在理解問題時遇到的問題與GetResponse操作的異步類型有關。

我增加了一個事件如下:

Public Event NewGeocode(ByVal LocationData As BingMapsRESTService.Common.JSON.Location, ByVal successful As Boolean) 
Private Sub geocodeLoop(Optional LocationData As BingMapsRESTService.Common.JSON.Location = Nothing, Optional successfull As Boolean = False) Handles Me.NewGeocode 
    If LocationData Is Nothing Then 
     geocodeAddress() 
    Else 
     If successfull = True Then 
      'Correct results: 
      MsgBox(LocationData.Confidence) 
      MsgBox(LocationData.EntityType) 
      MsgBox(LocationData.Point.Coordinates(0) & ", " & LocationData.Point.Coordinates(1)) 
      geocodeAddress() 
     Else 
      MsgBox("Unsuccessfull") 
     End If 
    End If 
End Sub 

Private Sub geocodeAddress() 
    Dim key As String = ... 
    Dim query As String = "1 Microsoft Way, Redmond, WA" 

    Dim geocodeRequest As New Uri(String.Format("http://dev.virtualearth.net/REST/v1/Locations?q={0}&key={1}", query, key)) 
    Dim latlong_adress As BingMapsRESTService.Common.JSON.Location = Nothing 
    GetResponse(geocodeRequest, Function(x) 

            If Not x.ResourceSets(0).Resources.Length = 0 Then 
             RaiseEvent NewGeocode(x.ResourceSets(0).Resources(0), True) 
            Else 
             RaiseEvent NewGeocode(Nothing, False) 
            End If 
            Return 0 
           End Function) 
End Sub 
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click 
    geocodeLoop() 
End Sub