Introduction

Tablets and smartphones are ideal are ideal for travelers. This lesson will look at using a device's Location capabilities to determine the current location (longitude and latitude). Map services, such as Microsoft Bing, Google Maps, or MapQuest can be used to display the current location on a map or to provide other locations provided by an address. Routing services between points on a map also can be determined. Much of the work is done for you by accessing methods of the map services API (Application Programming Interface), and thus you can provide amazing mapping capability with little code.

Lesson Objectives
By the end of this lesson, you will be able to:
  1. Calculate the current positional longitude and latitude using the device's Location capability.
  2. Map the current location on a Bing Map object.
  3. Display specified locations on a Bing Map given a provided address or landmark name.
  4. Plot a course on a map to show driving directions between two specified points.
 

Learning Sequence
Required Readings
Read the following:
  • Using the Geolocation API
  • Using Bing Maps API to Display a Location
  • Mapping a Specific Location or Landmark
  • Mapping a Driving Route Between Two Points
Resources
View the following:
  • Review the "Bing Maps Platform" on Microsoft's Bing website for information and to get started with Bing Maps.
Assignments
Complete the following:
  • Quiz 9
  • Assignment 9A (Take Me Out to the Ballgame)
 

Instruction

(Note: Due to the formatting limitations of this document, some code snippets presented here may include statements split on more than one line when in the code editor they should not be. E.g. literal strings (red text enclosed in quotes) must occur on one line. Make the appropriate corrections in the Visual Studio text editor.)

The Location Provider of a Windows 8 device provides an app with location data. Like many tablets, Microsoft Surface tablets do not have a Global Position System (GPS) chip in them; instead, they use Wi-Fi triangulation and IP address information to determine location. Windows 8 devices from other manufacturers may have a GPS chip, and there are ways of providing more accurate GPS location data. Options include using USB dongles or tethering to a smartphone or GPS device coupled with supported drivers for the tablet. Windows 8 will use the most accurate location data it can obtain based on service availabilities.

Microsoft's Bing mapping solution provides a developer with easy-to-use, impressive mapping capabilities. Specific locations can be pinpointed with latitude and longitude measurements. Geocoding provides accurate location data for address or landmark lookup. Routing services are part of the Bing API (Application Programming Interface) and allow for automated driving instructions and course plotting between two or more points on a map.

Tablets offer other sensors that can be tapped into, providing environment awareness. Data from the compass sensor would likely be useful in a hiking app. The accelerometer provides gravitational effect data across x, y, and Z axes and the inclinometer provides data on the pitch, roll, and yaw of the device. This would be necessary in creating a game where a user must roll a virtual marble through an obstacle course by moving the device. Data from the light sensor can be used to extend the battery life of the device by reducing the brightness of the display in low-light settings.

Using the Geolocation API

Estimated current latitude and longitude data is provided by Windows 8 geolocation API (Application Programming Interface). The location data is derived from the most accurate hardware and software capabilities present. This might be Wi-Fi triangulation, IP address information, or a Global Position System (GPS) chip if available.

Review "Using the Geolocation API" section in the Lesson 9 Guide for detailed information and examples.

Using Bing Maps API to Display a Location

Microsoft offers a Bing Maps API for C# and VB developers. A Bing Maps account is necessary to obtain a usage key. The cost of the license depends on the project. Review the "Bing Maps Platform" on Microsoft's Bing website for information and to get started. A 90-day trial is available as well as an educational license. At the time of this writing, licenses for Windows Store apps were also free.

Review "Using Bing Maps API to Display a Location" section in the Lesson 9 Guide for detailed information and examples.

Mapping a Specific Location or Landmark

The GeocodeRequestOptions object of the Bing.Maps.Search API can be used to determine the longitude and latitude of a street address or place name in conjunction with a SearchManager Object. The GeocodeAsync method of the SearchManager is deployed with the GeocodeRequestOptions object as its lone parameter.

Review "Mapping a Specific Location or Landmark" section in the Lesson 9 Guide for detailed information and examples.

Mapping a Driving Route Between Two Points

The Directions API of Bing Maps is used to plot two points on the map and draw the driving path between the points. The Directions API of Bing Maps also provides step-by-step directions to navigate from the first address/landmark to the second. In this example, the user will enter two points and will then click a button to display the driving path on the map and the step-by-step directions in a TextBox. The Directions API can also determine the total distance and an estimated time of travel.

Review "Mapping a Driving Route Between Two Points" section in the Lesson 9 Guide for detailed information and examples.

Practice

  1. Create an app that tells your story with links to locations on a map. See the tip below on how to create hyperlinks in a RichTextBox.

Figure 1 – Practice Project 1. Hyperlinks of places in a person's biography are mapped.

You can use an Internet search engine Google to find the latitude and longitude of specific locations as needed. For instance, you can search "Parkland Hospital Dallas latitude" to find the coordinates.


Figure 2 – Internet Search engines are a great place to find the latitude and longitude coordinates of a landmark. Remember that if the latitude is south or the longitude is west, the coordinate is expressed in code as a negative value.

TIP: A RichTextBlock can contain hyperlinked text. Consider the following example.

XAML Code


<Page

x:Class="_09_hyperlink_demo__CS_.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:local="using:_09_hyperlink_demo__CS_"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

mc:Ignorable="d">


<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">


<RichTextBlock FontSize="22" Margin="65,100,111,543" IsTapEnabled="True"

IsTextSelectionEnabled="False">

<Paragraph Foreground="White" FontFamily="Segoe UI Light">

<Run>Four score and seven years ago,</Run>

<InlineUIContainer>

<TextBlock Margin="5,0,5,0" Tapped="Link1">

<Underline><Run Text="our fathers" Foreground="Magenta"/></Underline>

</TextBlock>

</InlineUIContainer>

<Run>brought forth on this continent, a new nation, conceived in</Run>

<InlineUIContainer >

<TextBlock Margin="5,0,5,0" Tapped="Link2">

<Underline><Run Text="Liberty" Foreground="Magenta"/></Underline>

</TextBlock>

</InlineUIContainer>

<Run>, and dedicated to the proposition that all men are created equal.</Run>

</Paragraph>

</RichTextBlock>

<TextBlock HorizontalAlignment="Left" Height="30" Margin="65,251,0,0"

Foreground="Yellow" FontSize="16" FontFamily="Segoe UI Light"

TextWrapping="Wrap" Text="Additional information / Explanation"

VerticalAlignment="Top" Width="349"/>

<TextBlock x:Name="txtMoreInfo" HorizontalAlignment="Left" Foreground="LightGray"

FontSize="16" FontFamily="Segoe UI Light" Height="141" Margin="65,275,0,0"

VerticalAlignment="Top" Width="1174"/>

</Grid>

</Page>


The highlighted <InlineUIContainer> blocks provide the hyperlinked text and respond to a Tapped event. Also note that the IsTapEnabled property of the parent RichTextBlock is set to true. This enables the links to be active and to respond to the tap or click. The IsTextSelectionEnabled property also is set to false so the mouse pointer does not turn into a selection I-beam pointer if using a mouse. A tap or click on a link displays more information in the lower TextBlock named txtMoreInfo, as seen in the code snippets for C# or VB below:


Figure 3 – Tapping or clicking on the 'Score' hyperlink displays additional explanatory information in the TextBlock below.

C# Code Snippet From MainPage.xaml.cs


. . .

private void Link1(object sender, TappedRoutedEventArgs e)

{

txtMoreInfo.Text = "SCORE \nA score is an amount of twenty. " +

"Thus 'Four score and seven' is 87. The Gettysburg Address " +

"was delivered in 1863, or 87 years after the Constitution " +

"was adopted in 1776.";

}


private void Link2(object sender, TappedRoutedEventArgs e)

{

txtMoreInfo.Text =

"OUR FATHERS \nLincoln was referring to writers of the Constitution.";

}


private void Link3(object sender, TappedRoutedEventArgs e)

{

txtMoreInfo.Text = "LIBERTY \nThe pilgrims came here to find religious freedom" +

" and freedom from the oppressions of the monarchy.";

}


VB Code Snippet From MainPage.xaml.vb


Private Sub Link1(sender As Object, e As TappedRoutedEventArgs)

txtMoreInfo.Text = "SCORE \nA score is an amount of twenty. " & _

"Thus 'Four score and seven' is 87. The Gettysburg Address " & _

"was delivered in 1863, or 87 years after the Constitution " & _

"was adopted in 1776."

End Sub


Private Sub Link2(sender As Object, e As TappedRoutedEventArgs)

txtMoreInfo.Text = _

"OUR FATHERS \nLincoln was referring to writers of the Constitution."

End Sub


Private Sub Link3(sender As Object, e As TappedRoutedEventArgs)

txtMoreInfo.Text = "LIBERTY \nThe pilgrims came here to find religious freedom" & _

" and freedom from the oppressions of the monarchy."

End Sub


  1. Write an app that maps directions from your home to five of your favorite hangouts: school, restaurant, bowling alley, golf course, theatre, mall, favorite store, and so on. Use geocoding to find the location coordinates for a given address. Provide an option to map directions using your current location instead of your home.

Figure 4 – Practice Project 2 involves mapping five unique locations from the student's home or their current location.

Assignments

Complete the following:

Summary

Any location on the globe can be identified by its latitude and longitude. These coordinates can be used to map any location in a map object, such as provided by MapQuest or Bing. The Location service of the device can be employed to identify the current location. Microsoft's Bing Maps provide methods for geocoding (finding the latitude and longitude of a specified address), mapping the driving route between two points, and providing step-by-step directions for navigating between the two points.

To obtain the current location, the Location capability must be enabled in the package.appxmanifest. To use Bing Maps, a platform must be referenced in the Configuration Manager of the project and the Bing Maps API must be downloaded, installed, and referenced in the project. A Bing developer key must be obtained and referenced in the Credentials property of the Bing map object in the XAML code after including a reference to the Bing Maps namespace. The appropriate using (C#) or Imports (VB) directive statements also must be included in the code-behind to access the Bing API methods and object.


Course Information

Additional Attributions

Creative Commons License

This work is created by the National Information Security and Geospatial Technologies Consortium (NISGTC), and except where otherwise noted, is licensed under the Creative Commons Attribution 3.0 Unported License