Instructions

Objectives

  • Find out the geographic position of a mobile device using two different methods.
  • Convert a partial location name into a geographic position.
  • Seamlessly use several methods to capture a location.

Prerequisites

Step 1 - Find Me?

Create a new mobile web form and paste the following ASP.NET code into the page.

<mob:Location ID="Location1" runat="server" Mode="Client" HighAccuracy="true" AutoPostBack="True" OnLocationFound="LocationFound">
</mob:Location>
<mob:Label ID="Label1" runat="server"></mob:Label>

The following C# code should be pasted into the pages class file.

protected void LocationFound(object sender, LocationFoundEventArgs e)
{
    Label1.Text = e.IsAvailable ? e.LocationInfo.Position.ToDisplay() : "Not Found";
}

Before this page can be tested we need to configure the emulator to support location services and provide a simulated geographic position. The simplest way to do this with the Android emulator is to use the Telnet command interface to send geographic position fixes to the virtual device. If the Telnet client has not already been installed on the development machine now is a good time to add the feature.

If you have already Telnet Client installed you may skip this paragraph. Telnet comes pre-installed on all versions of Windows but isn't enabled by default. On Windows Server 2008 this option can be found on 'Control Panel > Turn Windows features on or off > Add features' and then scrolling to Telent Client. Other Windows release have very a similar procedure. Your computer may need to be restarted before finishing.

After you have started the Android emulator and a virtual machine is running open a command prompt and type telnet localhost 5554.

Type geo fix -0.969 51.455 in the Telnet session once it's started. Note that on a Telnet prompt you have to type exactly what you want to do. Backspaces invalidate the command.

You should see a screen similar to the following providing confirmation that the emulated position has been accepted.



The Emulator now has a fixed position, in this case the railway station in the middle of Reading in the UK. The first parameter after geo fix is longitude and the second latitude. Both are expressed as decimal values.

Open the page using the Android emulator and after a few seconds you should see a screen similar to the following.



When the page is loaded the mobile device is queried for it's location. The latitude and longitude of the emulator are returned and the control will automatically postback the result to the server firing the
LocationFound event.

Different mobile devices use different techniques to determine location. The location control supports all common methods and developers using the location control do not need to worry about the differences between devices. Some mobile devices will not support location and in situations where location support can not be guaranteed some of the others described in this tutorial should be considered.

Step 2 - Finding Somewhere Else

Sometimes an application doesn't just need to know where the mobile device is, it needs to ask the user for other locations they might be interested in. For example; a Store Finder application may be asked to locate stores near the users current position, or at another location the user intends to travel to. The Location control supports this scenario via the Mode property.

Change the ASP.NET code from Step 1 so that the Mode property of the location control is set to GeoCode. The code should appear as follows.

<mob:Location ID="Location1" runat="server" Mode="GeoCode" HighAccuracy="true" AutoPostBack="True"
    OnLocationFound="LocationFound">
</mob:Location>

The Mode property controls the methods that are used to obtain a location. When set to GeoCode the control will ask the user to enter a location into a text box. Pressing the Find Location button initiates a request to Google's GeoCode API to determine the latitude and longitude of the location. If more than one possible location exists the user will be asked to select from a drop down list. Once a location has been chosen the LocationFound event will be fired.

Opening the page with the Android Emulator and entering Reading into the TextBox will result in the following series of screens.

Step 3 - MaxMind

Sometimes an application doesn't need to know the precise position of a mobile device. The country or the state the device is in will be good enough. The previous two methods either require features not present on all mobile devices, or require user input.

Alter the ASP.NET code from
Step 2 to set the Mode property to MaxMind. The code should be similar to the following.

<mob:Location ID="Location1" runat="server" Mode="MaxMind" HighAccuracy="true" AutoPostBack="True"
    OnLocationFound="LocationFound">
</mob:Location>

Now open the web page in the Android Emulator. A page similar to the following screen shot should appear.


What's happening? When in MaxMind mode a small local database mapping IP address ranges to geographic locations is consulted to determine the position of the mobile device base on it's public IP address. As this database is provided by MaxMind Inc we've named the mode MaxMind.

When accessing the web page within a development environment the IP address of the emulator will be within a private address range and can not possibly be included in the IP address database. In order to develop and test mobile web applications which use MaxMind mode the mobile device must use a public IP address. When accessed from the Android Emulator connected to the public internet a screen similar to the one seen in Step 1 will be displayed. Remember the location accuracy will be very poor compared to the value returned in Step 1.

When the web page is displayed on an Android Emulator with a public IP address a screen similar to the following will appear.



MaxMind sell commercial databases that are more accurate than the free version downloaded when 51Degrees.mobi is installed. See the MaxMind web site for more details.

If MaxMind is used within a mobile web application the IP address database should be updated monthly to ensure recent data is being used.

Step 4 - Multiple Methods - Server Side Location

Changing the Mode property of the Location Control to Server will use both the MaxMind and GeoCode methods to return a location. These methods are both implemented entirely on the web server and are used together to provide flexibility to the end user. The user will be able to select the location found by MaxMind in a single click, or if they'd rather use the TextBox and Find Location button to enter their own location they can.

Two other properties need to be set when using
Server mode. DisplayLocation needs to be set to true to enable the location found by MaxMind to be available for the user to select if they don't wish to use the GeoCode option. If you'd rather the location found from MaxMind was displayed as a name rather than a latitude and longitude the RequestAddress property should be set to true.

Change the ASP.NET code from
Step 3 to the following.

<mob:Location ID="Location1" runat="server" Mode="Server" DisplayLocation="true" RequestAddress="true" AutoPostBack="True"
    OnLocationFound="LocationFound">
</mob:Location>

When accessed from the Android Emulator with a public IP address a screen similar to the following will be presented.

 

If the public IP address does not return a location then a message information the end user the location can not be found will appear.

The RequestAddress property is used to convert a location to a more familiar place name or point of interest. If RequestAddress were set to false the latitude and longitude of the location would be displayed which may not mean anything to the end user.

Step 5 - Multiple Methods - Precise Locations

In Step 4 two location methods were used that will work on any mobile device as they're implemented entirely using server side technology. If a very accuracy location is required for the mobile web application Precise mode can be used to combine the Client and GeoCode methods.

Change the Mode property in Step 4 to Precise. Your ASP.NET location control code should appears as follows.

<mob:Location ID="Location1" runat="server" Mode="Precise" HighAccuracy="true" DisplayLocation="True" RequestAddress="true" AutoPostBack="True"
    OnLocationFound="LocationFound">
</mob:Location>
<mob:Label ID="Label1" runat="server"></mob:Label>

 The user interface present will appear almost identically to that seen in
Step 4. However the location available for single click selection will have been provided by the mobile device's location services. If the page is displayed on the Android Emulator a screen similar to the following should appear. 



No location has been found through location services because the geo fix command has not been issued via the telnet session. See Step 1. This is why a message "Sorry. We're unable to locate you at the moment." is displayed. If the end user should not be aware of option to select the current location if the location service fails to return a location set the DisplayLocation property of the control to false.

Step 6 - Multiple Methods - Everything

One final mode is available which combines all the methods discussed in this tutorial. All mode is very similar to Precise but will use a MaxMind location if the location services of the mobile device fail to return a location. This will at least provide the end user a location for single click selection if they're happy with the location provided.

Licensing

Google maps APIs are used with the Location control when Mode property is set to GeoCode, Precise or All or RequestAddress is set to true. MaxMind's GeoLiteCity database is used by the Location control in when the Mode property is set to All or Server. These services are subject to separate terms of use. See the following link:
Purchase of a 51Degrees.mobi - .Net Mobile Framework does not include a licence to use these services. In many situations Google and MaxMind provide the necessary services to use the Location control without charge.

Conclusion

The Location control provides a very simple method of capturing a location using a variety of complex methods. A single event is fired when a location is found irrespective of the method or source. Mobile web applications can make use of location to simplify or enhance existing processes. For example; explicitly finding points of interest or providing nearby and therefore most likely points of interest towards the top of a list.

Tutorials

NumberSorted By Number In Ascending OrderTutorialSummary
1InstallationInstall 51Degrees - .Net Mobile Framework and confirm the installation is successful.
2EmulatorsConfigure IIS to work with mobile device emulators improving development productivity.
3Hello WorldAdd a mobile label control to a mobile page and understand major differences compared to standard ASP.NET.
4Mobile ProfileUse a simple database to gain additional insight into the mobile device performance, optimise view state and persist data.
5Clever ImagesOptimise images to consume the bare minimum amount of data for the display on the requesting mobile device without compromising image quality.
6Entering TextMake it easier for your users to enter text by remembering previous values and providing common options.
7DatesCaptures dates quickly and easily using all the capabilities of the requesting mobile device.
8LocationFind out where the requesting mobile device is or get location information from users.
9MapsDisplay maps from Google or Bing, move around and show multiple markers.
10Data Lists and PagersDisplay lists of items and page between them.
11Styling Mobile ControlsStyle mobile web applications using different common characteristics of mobile devices such as screen size or input method.
12DeploymentDeploy the Framework onto a production server and configure IIS and SQL.