如何将地址框文本转换为geo long lat。在wp7

时间:2013-05-17 15:07:22

标签: windows windows-phone-7 bing-maps

我想将地址框文本转换为地理坐标,以便将引脚放在地图上。这是我的地图的xaml代码。

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <!--TitlePanel contains the name of the application and page title-->
    <!--ContentPanel - place additional content here-->

        <my:Map Height="683" HorizontalAlignment="Left" CredentialsProvider="bingmapcode" Margin="0,0,0,13" Name="map1" VerticalAlignment="Top" Width="460"/>

</Grid>

我的地址保存在字符串类型变量中。如何将其转换为坐标。

1 个答案:

答案 0 :(得分:2)

这是我从MSDN回来的一些代码。添加地址文本框,然后使用“查找”按钮搜索地图。

<TextBox Height="23" Name="txt_address" Width="84" />
<Button Content="Find" Height="23" Margin="0 0 5 0"  Name="btn_Find"  Width="64" Click="btn_Find_Click"   />

按钮点击事件;

private void btn_Find_Click(object sender, RoutedEventArgs e)
{
FindAndDisplayNearbyPOI(Geocode(txt_address.Text));
}

   public XmlDocument Geocode(string addressQuery)
    {
        string BingMapsKey = "You bing key";
        string geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + addressQuery + "?o=xml&key=" + BingMapsKey;

        XmlDocument geocodeResponse = GetXmlResponse(geocodeRequest);

        return geocodeResponse;
    }

    private XmlDocument GetXmlResponse(string requestURL)
    {
        HttpWebRequest request = WebRequest.Create(requestURL) as HttpWebRequest;
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
                throw new Exception(String.Format("Server error (HTTP {0}: {1}).",
                    response.StatusCode,
                    response.StatusDescription));
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(response.GetResponseStream());

            return xmlDoc;
        }
    }

private Microsoft.Maps.MapControl.WPF.Location FindAndDisplayNearbyPOI(XmlDocument xmlDoc)
        {
            Microsoft.Maps.MapControl.WPF.Location pushpin = new Microsoft.Maps.MapControl.WPF.Location();
            // Create namespace manager
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
            nsmgr.AddNamespace("rest", "http://schemas.microsoft.com/search/local/ws/rest/v1");

            // Get all locations in the response and then extract the coordinates for the top location
            XmlNodeList locationElements = xmlDoc.SelectNodes("//rest:Location", nsmgr);

            if (locationElements.Count == 0)
            {
                //ErrorMessage.Visibility = Visibility.Visible;
                //ErrorMessage.Content = "The location you entered could not be geocoded.";
            }
            else
            {
                // Get the geocode points that are used for display (UsageType=Display)
                XmlNodeList displayGeocodePoints = locationElements[0].SelectNodes(".//rest:GeocodePoint/rest:UsageType[.='Display']/parent::node()", nsmgr);
                string latitude = displayGeocodePoints[0].SelectSingleNode(".//rest:Latitude", nsmgr).InnerText;
                string longitude = displayGeocodePoints[0].SelectSingleNode(".//rest:Longitude", nsmgr).InnerText;

                pushpin = AddPushpinToMap(Convert.ToDouble(latitude), Convert.ToDouble(longitude), "Cust");

                map1.Center = new Microsoft.Maps.MapControl.WPF.Location(Convert.ToDouble(latitude), Convert.ToDouble(longitude));
                map1.ZoomLevel = 12;
            }

            return pushpin;
        }

private Microsoft.Maps.MapControl.WPF.Location AddPushpinToMap(double latitude, double longitude, string pinLabel)
{

    Microsoft.Maps.MapControl.WPF.Location location = new Microsoft.Maps.MapControl.WPF.Location(latitude, longitude);
    Microsoft.Maps.MapControl.WPF.Pushpin pushpin = new Microsoft.Maps.MapControl.WPF.Pushpin();
    pushpin.Content = pinLabel;
    pushpin.Location = location;


    map1.Children.Add(pushpin);

    return pushpin.Location;
}