博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WP7 BingMap 通过关键字搜索地理位置
阅读量:5127 次
发布时间:2019-06-13

本文共 4173 字,大约阅读时间需要 13 分钟。

 

一、新建一个命名为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));  

           }

         }

    }

五、运行结果如下:

特别声明:本文借鉴。

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

 

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

你可能感兴趣的文章
ACM-ICPC 2018 world final A题 Catch the Plane
查看>>
那些年,那些书
查看>>
面向对象六大基本原则的理解
查看>>
注解小结
查看>>
java代码编译与C/C++代码编译的区别
查看>>
Bitmap 算法
查看>>
转载 C#文件中GetCommandLineArgs()
查看>>
list control控件的一些操作
查看>>
精读《useEffect 完全指南》
查看>>
SNF快速开发平台MVC-EasyQuery-拖拽生成SQL脚本
查看>>
DrawerLayout实现双向侧滑
查看>>
CentOS下同步时间并写入CMOS
查看>>
NLog简单使用
查看>>
MySQL入门很简单-触发器
查看>>
LVM快照(snapshot)备份
查看>>
Struts2 - 与 Servlet 耦合的访问方式访问web资源
查看>>
绝望的第四周作业
查看>>
一月流水账
查看>>
数论四大定理
查看>>
npm 常用指令
查看>>