MapLocationFinderResult无位置结果,调用Locations(0)触发越界异常求助
解决UWP地理编码示例越界异常及PDB加载问题
一、越界异常排查与修复
- 完善状态判断与空集合校验
你的代码仅判断了Success状态,但即使状态返回成功,Locations集合仍可能为空。先扩展状态输出并增加集合非空判断,定位问题:
Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim addressToGeocode As String = "Microsoft" Dim queryHint As New BasicGeoposition With {.Latitude = 47.643, .Longitude = -122.131} Dim hintPoint As New Geopoint(queryHint) Dim result As MapLocationFinderResult = Await MapLocationFinder.FindLocationsAsync(addressToGeocode, hintPoint, 3) ' 先输出状态,排查是否真的成功 Label1.Text = $"Status: {result.Status.ToString()}" If result.Status = MapLocationFinderStatus.Success Then ' 先判断集合是否为空,避免越界 If result.Locations.Count > 0 Then Label1.Text = $"result = ({result.Locations(0).Point.Position.Latitude},{result.Locations(0).Point.Position.Longitude})" Else Label1.Text = "成功返回但无位置数据" End If Else Label1.Text = $"地理编码失败:{result.Status.ToString()}" End If End Sub
- 空结果常见原因及修复
- 网络权限缺失:地理编码需联网,在
Package.appxmanifest的Capabilities标签中勾选Internet (Client)权限。 - 未配置Azure Maps密钥:UWP地图服务需绑定有效密钥,步骤如下:
- 在Azure门户创建Maps资源并获取密钥;
- 在应用的
App.xaml.vb中添加密钥注册:Protected Overrides Async Sub OnLaunched(e As LaunchActivatedEventArgs) Await MapService.ServiceTokenManager.RegisterMapServiceTokenAsync("你的Azure Maps密钥") ' 其他初始化代码 End Sub
- 查询词过于宽泛:将
"Microsoft"替换为更具体的地址,比如"Microsoft Redmond, WA"。
二、MapGeocoder.pdb加载错误处理
该错误与越界异常无关,PDB是原生代码的调试符号文件,不影响应用运行。处理方式:
- 打开Visual Studio的
工具→选项→调试→符号,取消勾选Microsoft符号服务器,避免自动加载原生符号;若需调试原生代码,勾选后等待符号下载完成即可。 - 无需调试原生代码时,直接忽略该提示。
内容的提问来源于stack exchange,提问作者user17611369
相关产品推荐
相关产品推荐

