Hallo guys,
I'm tring to write a simple mapping WPF app that use the Geolocator class to retreive the current position.
Code is simple:
Imports Windows.Devices.Geolocation 'Geolocation Namespace
Imports System.Threading 'Threading Namespace
Public NotInheritable Class MainPage
Inherits Page
Private WithEvents glGeo As New Geolocator 'Create New Geolocator Object With Its Associated Events
Private Async Sub btGetLocation_Click(sender As Object, e As RoutedEventArgs) Handles btGetLocation.Click
Try
' Find Position
Dim gpPos As Geoposition = Await glGeo.GetGeopositionAsync()
'Display Coordinates
tbLatitude.Text = gpPos.Coordinate.Point.Position.Latitude.ToString()
tbLongitude.Text = gpPos.Coordinate.Point.Position.Longitude.ToString()
Catch err As Exception
End Try
End Sub
This code show the current position when the tbGetPosition button is clicked.
But I want a continuous update so I've added the following code:
Private Sub glGeo_PositionChanged(sender As Geolocator, args As PositionChangedEventArgs) Handles glGeo.PositionChanged
Dim gpPos As Geoposition = Await glGeo.GetGeopositionAsync()
'Display Coordinates
tbLatitude.Text = gpPos.Coordinate.Point.Position.Latitude.ToString()
tbLongitude.Text = gpPos.Coordinate.Point.Position.Longitude.ToString()
End Sub
This code generate an exception in runtime because the tbLatitude and tbLongitude are not accessible form the PositionChanged thread.
Any suggestion on how to fix it ?
Thanks, Stefano
I'm tring to write a simple mapping WPF app that use the Geolocator class to retreive the current position.
Code is simple:
Imports Windows.Devices.Geolocation 'Geolocation Namespace
Imports System.Threading 'Threading Namespace
Public NotInheritable Class MainPage
Inherits Page
Private WithEvents glGeo As New Geolocator 'Create New Geolocator Object With Its Associated Events
Private Async Sub btGetLocation_Click(sender As Object, e As RoutedEventArgs) Handles btGetLocation.Click
Try
' Find Position
Dim gpPos As Geoposition = Await glGeo.GetGeopositionAsync()
'Display Coordinates
tbLatitude.Text = gpPos.Coordinate.Point.Position.Latitude.ToString()
tbLongitude.Text = gpPos.Coordinate.Point.Position.Longitude.ToString()
Catch err As Exception
End Try
End Sub
This code show the current position when the tbGetPosition button is clicked.
But I want a continuous update so I've added the following code:
Private Sub glGeo_PositionChanged(sender As Geolocator, args As PositionChangedEventArgs) Handles glGeo.PositionChanged
Dim gpPos As Geoposition = Await glGeo.GetGeopositionAsync()
'Display Coordinates
tbLatitude.Text = gpPos.Coordinate.Point.Position.Latitude.ToString()
tbLongitude.Text = gpPos.Coordinate.Point.Position.Longitude.ToString()
End Sub
This code generate an exception in runtime because the tbLatitude and tbLongitude are not accessible form the PositionChanged thread.
Any suggestion on how to fix it ?
Thanks, Stefano