在JSON Geocode 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 :(得分: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