WP7 BingMap 通过关键字搜索地理位置

本文介绍如何在Windows Phone应用中实现地理位置搜索功能。主要内容包括创建应用、添加GeocodeService Web服务引用、配置客户端、UI设计及代码实现等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

一、新建一个命名为SearchLocatin的 Windows Phone 应用程序。如下:

 

二、为应用程序添加GeocodeService的Web服务引用,添加流程如下:

 

1、右键单击 

2、选择添加服务引用,就会出现添加服务引用的对话框。如下:

 

 

3、在地址输入框,输入http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc,然后单击“前往”。就会出现如下对话框:

 

 

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="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
                contract="SearchService.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/binaryHttp"
                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="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/binaryHttp"
                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));  

           }

         }

    }

五、运行结果如下:

特别声明:本文借鉴http://www.cnblogs.com/beniao/archive/2010/02/08/1665291.html

本人是第一次记录自己的学习心得和发现,不足之处,大叔们多多包涵!

 

转载于:https://www.cnblogs.com/meng-xiaoxiao/archive/2012/06/19/2555006.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值