locked
Display GPS position of user using c++

    Question

  • Hi

    I am trying to get the GPS position of the user in my Windows Store app using Bing Maps. I have found several examples online, but they all seem to be for C#. I can see that there are a lot of similarities between the two, but I am stuck several places, especiallywith the event handling where C# uses "geolocator.positionChanged += geolocator_PositionChanged;" like in the example on bing "getting-started-with-bing-maps-windows-store-apps-native" (can't paste links here atm).

    Is there a good and simple guide for how to do this (like in the article above) using C++? I should mention that I am pretty new to C++, so it would be great if the examples were easy to understand.

    Thanks for any help

    Friday, July 05, 2013 10:39 AM

Answers

  • Here are additional steps for using Bing Maps:

    1. Add reference to Bing Maps in Solution Explorer by right clicking on Project then References... then Add New Reference

    2. In XAML, add namespace reference: xmlns:bm="using:Bing.Maps"

    3. Add the Map to the XAML

    <bm:Map x:Name="map" Grid.Row="1" Credentials="Your bing maps key goes here"/>

    4. The .h

    	private:
    		Windows::Devices::Geolocation::Geolocator^ geolocator;
    		Windows::Foundation::EventRegistrationToken positionToken; 
    		void OnPositionChanged(Windows::Devices::Geolocation::Geolocator^ sender, Windows::Devices::Geolocation::PositionChangedEventArgs^ e); 
    
    		Bing::Maps::Pushpin^ GpsPushpin;

    5. Add some namespace usings in the .cpp

    using namespace Windows::Devices::Geolocation;
    using namespace Windows::UI::Core; 
    using namespace Bing::Maps;

    6. The .cpp initialization

    MainPage::MainPage()
    {
    	InitializeComponent();
    	geolocator = ref new Geolocator();
    	positionToken = geolocator->PositionChanged::add(
    		ref new TypedEventHandler<Geolocator^, PositionChangedEventArgs^>(
    		this, &MainPage::OnPositionChanged)); 
    
    	//Create and add pushpin
    	GpsPushpin = ref new Pushpin();
    
    	map->Children->Append(GpsPushpin);
    
    
    }

    7. The event handler:

    void MainPage::OnPositionChanged(Geolocator^ sender, PositionChangedEventArgs^ e) 
    { 
    	// We need to dispatch to the UI thread to display the output 
    	Dispatcher->RunAsync( 
    		CoreDispatcherPriority::Normal, 
    		ref new DispatchedHandler( 
    		[this, e]() 
    	{ 
    		auto coordinate = e->Position->Coordinate; 
    		Latitude->Text = coordinate->Latitude.ToString(); 
    		Longitude->Text = coordinate->Longitude.ToString();
    
    		auto location = ref new Location(coordinate->Latitude, coordinate->Longitude);
    		//Update the position of the GPS pushpin
    		MapLayer::SetPosition(GpsPushpin, location);
    
    		//Make GPS pushpin visible
    		GpsPushpin->Visibility = Windows::UI::Xaml::Visibility::Visible;
    
    		//Update the map view to the current GPS location
    		map->SetView(location, 17);
    
    	}, 
    		CallbackContext::Any 
    		) 
    		); 
    } 



    • Proposed as answer by jrboddie Friday, July 05, 2013 1:02 PM
    • Edited by jrboddie Friday, July 05, 2013 1:08 PM typo
    • Marked as answer by audi91 Monday, July 08, 2013 8:39 AM
    Friday, July 05, 2013 1:01 PM

All replies

  • There is a Geolocation sample in the gallery.

    But the simple version is as follows:

    1. Declare Location capability in the Package Manifest

    2. Add members to the .h file

    	private:
    		Windows::Devices::Geolocation::Geolocator^ geolocator;
    		Windows::Foundation::EventRegistrationToken positionToken; 
    		void OnPositionChanged(Windows::Devices::Geolocation::Geolocator^ sender, Windows::Devices::Geolocation::PositionChangedEventArgs^ e); 

    3. For readability, add the following namespaces to your .cpp

    using namespace Windows::Devices::Geolocation;
    using namespace Windows::UI::Core; 

    4. Create an instance of your geolocator and hook up an event handler:

    MainPage::MainPage()
    {
    	InitializeComponent();
    	geolocator = ref new Geolocator();
    	positionToken = geolocator->PositionChanged::add(
    		ref new TypedEventHandler<Geolocator^, PositionChangedEventArgs^>(
    		this, &MainPage::OnPositionChanged)); 
    }

    5. Add the event handler:

    void MainPage::OnPositionChanged(Geolocator^ sender, PositionChangedEventArgs^ e) 
    { 
        // We need to dispatch to the UI thread to display the output 
        Dispatcher->RunAsync( 
            CoreDispatcherPriority::Normal, 
            ref new DispatchedHandler( 
                [this, e]() 
                { 
                    auto coordinate = e->Position->Coordinate; 
                    Latitude->Text = coordinate->Latitude.ToString(); 
                    Longitude->Text = coordinate->Longitude.ToString();
                }, 
                CallbackContext::Any 
                ) 
            ); 
    } 

    (Where Latitude and Longitude here are TextBlocks in the XAML.)

    • Proposed as answer by jrboddie Friday, July 05, 2013 2:23 PM
    Friday, July 05, 2013 12:11 PM
  • Here are additional steps for using Bing Maps:

    1. Add reference to Bing Maps in Solution Explorer by right clicking on Project then References... then Add New Reference

    2. In XAML, add namespace reference: xmlns:bm="using:Bing.Maps"

    3. Add the Map to the XAML

    <bm:Map x:Name="map" Grid.Row="1" Credentials="Your bing maps key goes here"/>

    4. The .h

    	private:
    		Windows::Devices::Geolocation::Geolocator^ geolocator;
    		Windows::Foundation::EventRegistrationToken positionToken; 
    		void OnPositionChanged(Windows::Devices::Geolocation::Geolocator^ sender, Windows::Devices::Geolocation::PositionChangedEventArgs^ e); 
    
    		Bing::Maps::Pushpin^ GpsPushpin;

    5. Add some namespace usings in the .cpp

    using namespace Windows::Devices::Geolocation;
    using namespace Windows::UI::Core; 
    using namespace Bing::Maps;

    6. The .cpp initialization

    MainPage::MainPage()
    {
    	InitializeComponent();
    	geolocator = ref new Geolocator();
    	positionToken = geolocator->PositionChanged::add(
    		ref new TypedEventHandler<Geolocator^, PositionChangedEventArgs^>(
    		this, &MainPage::OnPositionChanged)); 
    
    	//Create and add pushpin
    	GpsPushpin = ref new Pushpin();
    
    	map->Children->Append(GpsPushpin);
    
    
    }

    7. The event handler:

    void MainPage::OnPositionChanged(Geolocator^ sender, PositionChangedEventArgs^ e) 
    { 
    	// We need to dispatch to the UI thread to display the output 
    	Dispatcher->RunAsync( 
    		CoreDispatcherPriority::Normal, 
    		ref new DispatchedHandler( 
    		[this, e]() 
    	{ 
    		auto coordinate = e->Position->Coordinate; 
    		Latitude->Text = coordinate->Latitude.ToString(); 
    		Longitude->Text = coordinate->Longitude.ToString();
    
    		auto location = ref new Location(coordinate->Latitude, coordinate->Longitude);
    		//Update the position of the GPS pushpin
    		MapLayer::SetPosition(GpsPushpin, location);
    
    		//Make GPS pushpin visible
    		GpsPushpin->Visibility = Windows::UI::Xaml::Visibility::Visible;
    
    		//Update the map view to the current GPS location
    		map->SetView(location, 17);
    
    	}, 
    		CallbackContext::Any 
    		) 
    		); 
    } 



    • Proposed as answer by jrboddie Friday, July 05, 2013 1:02 PM
    • Edited by jrboddie Friday, July 05, 2013 1:08 PM typo
    • Marked as answer by audi91 Monday, July 08, 2013 8:39 AM
    Friday, July 05, 2013 1:01 PM
  • Thank you so much for both answers! Exacly what I needed.
    Monday, July 08, 2013 8:41 AM