Gaudreau95
Gaudreau95

Reputation: 139

WPF Bing Maps latitude longitude to Textbox

I'm using a WPF Bing map within my application and its currently set up to center at a certain latitude longitude, I was wondering if instead of having a set latitude longitude that instead I could display a text box and search button where the user could enter the latitude longitude of there chosen location. If so what code should I add?

Xaml

 Title="MainWindow" Height="320.149" Width="475.746">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="0*"/>
        <ColumnDefinition/>
    </Grid.ColumnDefinitions>
    <m:Map CredentialsProvider="My Key" Mode="AerialWithLabels"
           Center="51.521347, -0.138983" ZoomLevel="16"  x:Name="BingMap" Grid.ColumnSpan="2">
        <TextBox x:Name="txtwhere" HorizontalAlignment="Left" Height="35" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="126" Margin="315,10,0,0"  />
        <Button x:Name="Btnsearch" Content="Search" HorizontalAlignment="Left" Height="28" VerticalAlignment="Top" Width="112" Margin="316,57,0,0" Click="Btnsearch_Click"/>

VB

Partial Public Class MainWindow
Inherits Window

Public Sub New()
    InitializeComponent()
    BingMap.Focus()
    'Set map to Aerial Mode with labels
    BingMap.Mode = New AerialMode(True)
End Sub

Upvotes: 1

Views: 832

Answers (1)

Lapious
Lapious

Reputation: 76

You should have more-or less figured it out since you set the "Center" in the Xaml. Just tested it in C# then converted to VB. Let me know if this is what you were looking for (Btnsearch_Click handling Click event of your Btnsearch):

Private Sub Btnsearch_Click(sender As Object, e As RoutedEventArgs)
    Dim latitude As Double = Double.Parse(txtwhere.Text.Substring(0, txtwhere.Text.IndexOf(","C)))
    Dim longitude As Double = Double.Parse(txtwhere.Text.Substring(txtwhere.Text.IndexOf(","C) + 1))

    BingMap.Center = New Microsoft.Maps.MapControl.WPF.Location(latitude, longitude) 
End Sub

Of course this assumes the textbox values are comma separated and valid. it is better to write validation to avoid errors.

Upvotes: 1

Related Questions