如何在wp7 Bing地图服务中搜索位置?

时间:2012-10-04 09:26:15

标签: c# windows-phone-7 xaml

我从http://blog.jerrynixon.com/2011/10/bing-maps-geocoding.html

复制了一段代码

以小方式编辑。但它不起作用。

Xaml代码

<phone:PhoneApplicationPage 

x:Class="MapLocationSearch1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"


FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"

SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True" xmlns:my="clr-

namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps">



<!--LayoutRoot is the root grid where all page content is placed-->

<Grid x:Name="LayoutRoot" Background="Transparent">

    <my:Map Height="526" HorizontalAlignment="Left" Name="map1" VerticalAlignment="Top" Width="480" Hold="map1_Hold" CredentialsProvider="Key"/>

    <TextBox Height="72" HorizontalAlignment="Left" Margin="0,532,0,0" Name="txtSearch" Text="" VerticalAlignment="Top" Width="460" />

    <Button Content="Search" Height="72" HorizontalAlignment="Left" Margin="151,624,0,0" Name="btnSearch" VerticalAlignment="Top" Width="160" Click="btnSearch_Click" />
</Grid>


<!--Sample code showing usage of ApplicationBar-->
<!--<phone:PhoneApplicationPage.ApplicationBar>

    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button1.png" Text="Button 1"/>
        <shell:ApplicationBarIconButton IconUri="/Images/appbar_button2.png" Text="Button 2"/>
        <shell:ApplicationBar.MenuItems>
            <shell:ApplicationBarMenuItem Text="MenuItem 1"/>
            <shell:ApplicationBarMenuItem Text="MenuItem 2"/>
        </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>

    </phone:PhoneApplicationPage.ApplicationBar>-->

</phone:PhoneApplicationPage>

运行时代码

using System;    
using System.Collections.Generic;
using System.Linq;    
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using MapLocationSearch1.GeocodeService;
using Microsoft.Phone.Shell;
using System.Diagnostics;
using System.Windows.Controls.Primitives;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.Device.Location;
using Microsoft.Phone.Controls.Maps;

namespace MapLocationSearch1

{
    public partial class MainPage : PhoneApplicationPage

    {


    public MainPage()

    {
        InitializeComponent();


    }

    private void map1_Hold(object sender, GestureEventArgs e)

    {
    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)

    {
        SetPushpin();   
    }


    public static void GeoCode(string address, Action<GeocodeResult, Exception> callback)

    {
        try

        {
            if (string.IsNullOrWhiteSpace(address))
                throw new ArgumentException("address");
            //Build a Request
            var _Credentials = new MapLocationSearch1.GeocodeService.Credentials { ApplicationId = "key" };
            var _Filters = new ObservableCollection<FilterBase>

            {
                new ConfidenceFilter {MinimumConfidence = Confidence.Medium}
            };
            var _Request = new GeocodeService.GeocodeRequest

            {
                Credentials = _Credentials,
                Query = address,
                Options = new GeocodeOptions{Filters = _Filters}
            };

            // Take out the data
            var _Service= new GeocodeServiceClient("BasicHttpBinding_IgeocodeService");
            _Service.GeocodeCompleted+=(s,e)=>

                {
                    callback(e.Result.Results.FirstOrDefault(),e.Error);
                };
            _Service.GeocodeAsync(_Request);
        }
        catch (Exception ex)

        {
            callback(null,ex);
        }
    }
    public void SetPushpin()

    {
        var _Address = txtSearch.Text;
        MainPage.GeoCode(_Address, (r, e) =>

        {
            Dispatcher.BeginInvoke(() =>

            {
                if (r == null || !r.Locations.Any())

                {
                    MessageBox.Show("Location Was not found");
                    return;
                }
                var _Pushpin = new Pushpin

                {
                    Location = new GeoCoordinate

                    {
                        Latitude = r.Locations[0].Latitude,
                        Longitude = r.Locations[0].Longitude
                    },
                    Content = _Address
                };
                map1.Children.Clear();
                map1.Children.Add(_Pushpin);
                map1.Center = _Pushpin.Location;
                map1.ZoomLevel = 10;

            });
        });
    }

需要建议请...

它没有显示我认为的任何地理编码..它没有将GeoCode结果返回到图钉方法。

每个搜索字符串只返回一个null。

0 个答案:

没有答案