2015-06-27 4 views
1

Внутри GetSponse JSON Geocode, код, предоставленный 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 
Смежные вопросы