BingMapsTask in Mango

Mango From Scratch

It turns out that launching Bing Maps, even with Search terms and setting the zoom level Donuts is absurdly easy, using the new BingMaps task.  I used it to search for donuts in the Boston area (and came close to crashing it, it found so many!)  

The BingMaps task is a launcher; once you start it you do not return a value to the calling program.  That said, it works like all launchers (instantiate it, set its values, call Show()) and thus is easy and convenient.

To see how this works, create a new Phone application and in MainPage.xaml create 4 rows and 2 columns.  Here’s the Xaml I used to create the rows and columns,

<Grid
  x:Name="ContentPanel"
  Grid.Row="1"
  Margin="12,0,12,0">
  <Grid.RowDefinitions>
      <RowDefinition
          Height="1*" />
      <RowDefinition
          Height="1*" />
      <RowDefinition
          Height="1*" />
      <RowDefinition
          Height="1*" />
      <RowDefinition
          Height="4*" />
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
      <ColumnDefinition
          Width="1*" />
      <ColumnDefinition
          Width="1*" />
      <ColumnDefinition
          Width="1 *" />
  </Grid.ColumnDefinitions>

The left column is for the three prompts, the right for the TextBoxes that will gather the user’s choices

<TextBlock
 Text="Coordinates (empty for here
 Grid.Row="0"
  TextWrapping="Wrap"
 VerticalAlignment="Center"/>
 <TextBox
     Name="Long"
     VerticalAlignment="Center"
     Text="42.21"
     Grid.Column="1" />
 <TextBox
     Name="Lat"
     VerticalAlignment="Center"
     Text="-71.03"
     Grid.Column="2" />

 <TextBlock
     Text="Search Term"
     VerticalAlignment="Center"
     Grid.Row="1" />
 <TextBox
     Name="Search"
     Grid.Row="1"
     Grid.Column="1"
     VerticalAlignment="Center"
     Text="Coffee"
     Grid.ColumnSpan="2" />

 <TextBlock
     Text="Zoom Level"
     VerticalAlignment="Center"
     Grid.Row="2" />
 <TextBox
     Name="Zoom"
     Grid.Row="2"
     Grid.Column="1"
     VerticalAlignment="Center"
     Text="2"
     Grid.ColumnSpan="2"/>


 

Finally, place a button in the fourth row

<Button
    Content="Display"
    Name="Display"
    Grid.Row="3"
    Grid.Column="1"
    VerticalAlignment="Center"
    HorizontalAlignment="Left" />

 

All the action happens in the event handler for the button,

 public MainPage()
 {
     InitializeComponent();
     Display.Click += ( o, e ) =>
     {
        // all the action goes here
     };
 }

 

The goal is to grab the values from the various text boxes and to use them to initialize a BingMapsTask instance.  Because we use a GeoCoordinate, howevrer, you’ll need to add a reference to System.Device and a couple using statements,

using Microsoft.Phone.Tasks;
using System.Device.Location;

 

You can now parse the values given for longitude and latitude into doubles; you’ll do the same for the value given for Zoom, and initialize the bingMapsTask.  Like all tasks, you actually launch it by calling Show().

public MainPage()
{
  InitializeComponent();
  Display.Click += ( o, e ) =>
  {
     var longitude = Double.Parse(Long.Text);
     var latitude = Double.Parse(Lat.Text);
     var bingMapsTask = new BingMapsTask()
     {
        Center = new GeoCoordinate( longitude, latitude ),
        SearchTerm = Search.Text,
        ZoomLevel = Double.Parse( Zoom.Text )
     };
     bingMapsTask.Show();
  };
}

 

Note that to get the results I show above, I changed the search term to donuts and the zoom level to 20.

About Jesse Liberty

Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET will be released early in 2025. Liberty is a Senior SW Engineer for CNH and he was a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a Microsoft MVP.
This entry was posted in Essentials, Mango, Mini-Tutorial, WindowsPhone and tagged . Bookmark the permalink.

One Response to BingMapsTask in Mango

Comments are closed.