一、新建一个命名为SearchLocatin的 Windows Phone 应用程序。如下:
二、为应用程序添加GeocodeService的Web服务引用,添加流程如下:
1、右键单击
2、选择添加服务引用,就会出现添加服务引用的对话框。如下:
3、在地址输入框,输入,然后单击“前往”。就会出现如下对话框:
4、把命名空间ServiceReference重命名为SearchService,最后点击确认,这样“为应用程序添加GeocodeService的Web服务引用”就完成?如下:
5、请注意,(4)的“?”其实序添加GeocodeService的Web服务引用还没完成!因为地点检索和反向地理坐标检索都只支持客户端异步调用,添加Web服务引用后,客户端配置文件()会自动生成的配置有两个服务端点的地址配置,所以需要人为将“basicHttp”服务端点的地址配置删除,否则会出现异常。“basicHttp”删除前和删除后,如下:
(1)、“basicHttp”删除前:
<configuration>
<system.serviceModel> <bindings> <basicHttpBinding> <binding name="BasicHttpBinding_IGeocodeService" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"> <security mode="None" /> </binding> </basicHttpBinding> <customBinding> <binding name="CustomBinding_IGeocodeService"> <binaryMessageEncoding /> <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> </binding> </customBinding> </bindings> <client> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService" contract="SearchService.IGeocodeService" name="BasicHttpBinding_IGeocodeService" /> <endpoint address="" binding="customBinding" bindingConfiguration="CustomBinding_IGeocodeService" contract="SearchService.IGeocodeService" name="CustomBinding_IGeocodeService" /> </client> </system.serviceModel></configuration>(2)、“basicHttp”删除后:
<configuration>
<system.serviceModel> <bindings> <customBinding> <binding name="CustomBinding_IGeocodeService"> <binaryMessageEncoding /> <httpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" /> </binding> </customBinding> </bindings> <client> <endpoint address="" binding="customBinding" bindingConfiguration="CustomBinding_IGeocodeService" contract="SearchService.IGeocodeService" name="CustomBinding_IGeocodeService" /> </client> </system.serviceModel></configuration>6、这样就真正完成了添加GeocodeService的Web服务引用了!
三、为Mainpage.xaml文件添加一个Map控件、一个Button按钮、一个TextBlock输入框和一个PushPin图钉。Mainpage.xaml文件如下:
<!--ContentPanel - 在此处放置其他内容-->
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"> <my:Map Height="480" HorizontalAlignment="Left" Margin="27,99,0,0" Name="map1" VerticalAlignment="Top" Width="407"> <my:Pushpin Name="pushpinLocation" Background="Red" Content="A"/> </my:Map> <TextBox Height="72" HorizontalAlignment="Left" Margin="27,6,0,0" Name="textBox1" VerticalAlignment="Top" Width="257" GotFocus="textBox1_GotFocus"/> <Button Content="Search" Height="72" HorizontalAlignment="Right" Margin="0,6,22,0" Name="btnSearch" VerticalAlignment="Top" Width="166" Click="btnSearch_Click" /> </Grid>四、为Mainpage.xaml。.cs文件如下:
public partial class MainPage : PhoneApplicationPage
{
// 构造函数
public MainPage()
{
InitializeComponent();
}
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
this.textBox1.Text = "";
this.pushpinLocation.Visibility = Visibility.Collapsed;
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
this.pushpinLocation.Visibility = Visibility.Visible;
this.MakeSearchRequest(this.textBox1.Text.Trim());
}
private void MakeSearchRequest(string keyWord)
{
try
{
GeocodeServiceClient client = new GeocodeServiceClient();
client.GeocodeCompleted += new EventHandler<GeocodeCompletedEventArgs>(client_GeocodeCompleted);
GeocodeRequest request = new GeocodeRequest();
request.Credentials = new Credentials();
request.Credentials.ApplicationId = "Ass18leJLz5UXGqXw1XqS3iJee4o-UwFsYOeDQUjyXC25UPYb4hYmsek0KpguMEK";
request.Query = keyWord;
client.GeocodeAsync(request);
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(() => MessageBox.Show(ex.Message));
}
}
void client_GeocodeCompleted(object sender, GeocodeCompletedEventArgs e)
{
try
{
GeocodeResponse response = e.Result;
double latitude = response.Results[0].Locations[0].Latitude;
double longitude = response.Results[0].Locations[0].Longitude;
this.map1.Center = new GeoCoordinate(latitude, longitude);
this.pushpinLocation.Location = new GeoCoordinate(latitude, longitude);
this.map1.ZoomLevel = 10;
}
catch (Exception ex)
{
Dispatcher.BeginInvoke(() => MessageBox.Show(ex.Message));
}
}
}
五、运行结果如下:
特别声明:本文借鉴。
本人是第一次记录自己的学习心得和发现,不足之处,大叔们多多包涵!