我可以给你一行代码,但需要很多解释。因此,我准备为您提供完整的解决方案。干得好。首先添加这些xaml代码。
<my:Map Margin="12,0,12,0" Name="MapControl" LogoVisibility="Collapsed" ScaleVisibility="Visible" CopyrightVisibility="Collapsed" ZoomBarVisibility="Visible">
<toolkit:GestureService.GestureListener>
<toolkit:GestureListener Tap="GestureListener_Tap"/>
</toolkit:GestureService.GestureListener>
<my:Pushpin Name="Pushpin"/>
</my:Map>
然后在你的 .cs 文件中,非常小心地添加这些代码。
private void GestureListener_Tap(object sender, GestureEventArgs e)
{
try
{
var point = new Point(e.GetPosition(MapControl).X, e.GetPosition(MapControl).Y);
var location = MapControl.ViewportPointToLocation(point);
Pushpin.Location = location;
Pushpin.Content = "loading...";
var request = new ReverseGeocodeRequest()
{
Location = location,
Credentials = new Credentials()
{
ApplicationId = "YourAPP_ID"
}
};
// prepare the service
GeocodeServiceClient service = new GeocodeServiceClient(new BasicHttpBinding(), new EndpointAddress("http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc"));
service.ReverseGeocodeCompleted += service_ReverseGeocodeCompleted;
service.ReverseGeocodeAsync(request);
}
catch (Exception)
{
MessageBox.Show("Couldn't communicate with server");
}
}
void service_ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
{
if (e.Result.Results.Count > 0)
Pushpin.Content = e.Result.Results[0].Address.FormattedAddress;
else
Pushpin.Content = "Invalid";
}
在这两者之间,首先通过在BingMapsPortal 中注册您的应用程序为您的应用程序创建唯一的应用程序 ID,并在代码中使用该 AppId。
然后,在解决方案资源管理器中右键单击参考并选择添加服务参考并粘贴以下链接并选择确定。
http://dev.virtualearth.net/webservices/v1/geocodeservice/geocodeservice.svc.
这会在您的应用程序中添加服务引用,最终它可以工作。快乐编码。