Windows 8 Consumer Preview and Visual Studio 11 Beta – Quick tip: using location services (Part 8/11)

February 29, 2012

no comments

In this short post I will show how to use location services in you app. Working with location services requires declaring Location capability in application manifest:


The location services provides access to location functionality, such as cell triangulations, WiFi (through IP address), and GPS. Also great many modern devices supports resolving location in some way from mentioned before, application must handle the case where location services cannot resolve the location or user has disabled location services from the Control Panel.

The location service accessed through Windows.Devices.Geolocation namespace. Geolocator class responsible for resolving the location and (like sensors) supports two work modes: polling and events.

Using events based model requires subscribing to PositionChanged event to get notifications once position changes and optionally StatusChanged event which raised when the ability of the Geolocator to provide updated location changes. Also Geolocator class supports specifying DesiredAccuracy, MovementThreshold and ReportInterval to support various accuracy VS speed requirements of specific application.

Disclaimer: Since my app demo application (Valentine Love Catcher) not really need to use those services, I will show relatively simple scenario – polling.

Sample app will be using location (if available) when sharing information and only in case proximity connection is not established yet. App initializes location services during general localizations:

private async void InitializeLocationServices()
    //Initialize geolocator object
    Geolocator geoLocator = new Geolocator();

    if (null != geoLocator)
            //Try resolve the current location
            position = await geoLocator.GetGeopositionAsync();
        catch (Exception)
            //Nothing to do - no GPS signal or some timeout occured...

When application sharing data, if position already resolved the values will be used in message transferred to Share Target application:

if (null != position)
    string city = position.CivicAddress.City;
    string country = position.CivicAddress.Country;
    string state = position.CivicAddress.State;
    string zip = position.CivicAddress.PostalCode;

    string msg = "I am located in " + country;

    if (city.Length > 0)
        msg += ", city of " + city;

    if (state.Length > 0)
        msg += ", " + state;

    if (zip.Length > 0)
        msg += " near zip code " + zip;

Pretty simple…

That’s it for now.


Stay tuned for next post,


Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>