在WP7,WPF,VS2010上使用Bing地图的凭据无效

时间:2012-02-06 20:07:23

标签: wpf visual-studio-2010 windows-phone-7 silverlight-4.0 bing-maps

我从http://www.bingmapsportal.com获得了一个密钥,并且我已将以下代码添加到我的项目中,如在整个网络上所示。

在.xaml文件中:

my:Map Height="320" HorizontalAlignment="Stretch" Name="map1" VerticalAlignment="Top" CredentialsProvider="fa0bb238-62bb-41b9-a1e6-459a5e9564a6"/>

(密钥经过轻微编辑以避免滥用)

在.xaml.cs文件中:

map1.CredentialsProvider = new ApplicationIdCredentialsProvider("fa0bb238-62bb-41b9-a1e6-459a5e9564a6");
GeocodeRequest gReq = new GeocodeRequest();
GeocodeServiceClient gSrvc = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

gReq.Credentials = new Credentials();
gReq.Credentials.ApplicationId = "fa0bb238-62bb-41b9-a1e6-459a5e9564a6".ToUpper();

gReq.Query = address;

FilterBase[] filters = new FilterBase[2];
filters[0] = new ConfidenceFilter() { MinimumConfidence = Confidence.High };

GeocodeOptions gOpt = new GeocodeOptions();
gOpt.Filters = filters;
gReq.Options = gOpt;

gSrvc.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(gSrvc_GeocodeCompleted);
gSrvc.GeocodeAsync(gReq);

但我无法让它工作,我在地图本身上收到无效的凭证消息,以及服务器对GeocodeRequest的响应的无效凭证异常。

我访问过大约20个论坛主题(包括WP7 Bing Maps 'Invalid Credentials' Error),我似乎已经完成了他们所谈论的所有内容或已经发布的解决方案。

还有其他想法吗?

1 个答案:

答案 0 :(得分:2)

以下是我在WP7中初始化GeocodeRequest的方法:

GeocodeService.GeocodeRequest request = new GeocodeService.GeocodeRequest
{
    Culture = CultureInfo.CurrentUICulture.ToString(),
    Credentials = new GeocodeService.Credentials { ApplicationId = applicationId },
    UserProfile = new GeocodeService.UserProfile { DeviceType = GeocodeService.DeviceType.Mobile },
    Options = new GeocodeService.GeocodeOptions { Count = 1 },
    Query = address,
};

正如您所看到的,我没有对ApplicationId字符串执行ToUpper(),但我正在设置UserProfile(以及Culture)属性。也许UserProfile.DeviceType = Mobile设置必须与Bing Map API密钥的类型相对应,这也是移动设备。

也许这在某种程度上有所帮助。