My app uses listen for location changes in Geolocation following the microsoft script examples. It works fine on Windows and Android but in IOS clicking the start listening button it crashes the program. Hot reload also fails once the app is installed so I’m struggling to find a way to debug. Any advice will be gratefully received.
NavigationViewModel
public NavigationViewModel()
{
StartButtonClickedCommand = new Command(OnStartListening);
StopButtonClickedCommand = new Command(OnStopListening);
}
public async void OnStartListening()
{
try
{
MyBearing = "0";
MyCourse = "0";
Geolocation.LocationChanged += Geolocation_LocationChanged;
var request = new GeolocationListeningRequest(GeolocationAccuracy.High);
var success = await Geolocation.StartListeningForegroundAsync(request);
string status = success
? "GPS listening"
: "Couldn't start listening";
MyMessage = status;
}
catch (Exception ex)
{
// Unable to start listening for location changes
MyMessage = "Unable to start";
}
StartBtnColor = Colors.Red;
StopBtnColor = Colors.White;
if (Compass.Default.IsSupported)
{
if (!Compass.Default.IsMonitoring)
{
// Turn on compass
Compass.Default.ReadingChanged += Compass_ReadingChanged;
Compass.Default.Start(SensorSpeed.UI, applyLowPassFilter: true);
MyMessage2 = "Compass started";
}
}
else
{
// Unable to start listening for location changes
MyMessage2 = "No Compass available";
}
if (Barometer.Default.IsSupported)
{
Barometer.Default.ReadingChanged += Barometer_ReadingChanged;
Barometer.Default.Start(SensorSpeed.UI);
}
else
{
BarometerLabel = "NC";
}
}
public void Geolocation_failed(object sender, GeolocationListeningFailedEventArgs e)
{
MyMessage = e.Error.ToString();
}
public void Geolocation_LocationChanged(object sender, GeolocationLocationChangedEventArgs e)
{
// Process e.Location to get the new location
var otherLocation = new Location();
otherLocation.Latitude = RequiredLat;
otherLocation.Longitude = RequiredLong;
var myLocation = e.Location;
Altitudelabel = $"{myLocation.Altitude:0} m";
AccuracyLabel = $"{myLocation.Accuracy:0} m";
GPSMyBearing = $"{myLocation.Course:0} ° ";
var distance = myLocation.CalculateDistance(otherLocation, DistanceUnits.Kilometers);
MyDistance = $"{distance:0.##} Km";
if (distance < 1)
{
MyDistance = $"{1000 * distance:0.##} m";
}
MyLongitude = $"{myLocation.Altitude:0} m ";
MyLongitude = $"{myLocation.Longitude:0.#####} ° ";
MyLatitude = $"{myLocation.Latitude:0.#####} ° ";
MySpeed = $"{((myLocation.Speed) * 3.6):0.##} Km/h "; //Speed is in m/s so x 3.6 to get Km/h
// CurrentPositionlabel.Text = $"I am currently at Latitude {myLocation.Latitude} and Longitude {myLocation.Longitude}";
// Let's calculate the Bearing now
lat1 = myLocation.Latitude;
lat2 = otherLocation.Latitude;
long1 = myLocation.Longitude;
long2 = otherLocation.Longitude;
// convert to radians
lat1rads = lat1 * Math.PI / 180;
lat2rads = lat2 * Math.PI / 180;
deltalat = (lat1 - lat2) * Math.PI / 180;
long1rads = long1 * Math.PI / 180;
long2rads = long2 * Math.PI / 180;
deltalong = (long2 - long1) * Math.PI / 180;
x = Math.Sin(deltalong) * Math.Cos(lat2rads);
y = Math.Cos(lat1rads) * Math.Sin(lat2rads) - Math.Sin(lat1rads) * Math.Cos(lat2rads) * Math.Cos(deltalong);
angle = Math.Atan2(x, y);
MyBearing = $"{((angle / (Math.PI / 180)) + 360) % 360:0.##}"; // in degrees
i = i + 1;
MyMessage3= $"GPS samples Rx {i}"; // testing the listening
}
public void OnStopListening()
{
try
{
Geolocation.LocationChanged -= Geolocation_LocationChanged;
Geolocation.StopListeningForeground();
MyMessage = "GPS Stopped";
}
catch (Exception ex)
{
//
MyMessage = "Can't stop";
}
// Turn off compass
if (Compass.Default.IsSupported)
{
Compass.Default.Stop();
Compass.Default.ReadingChanged -= Compass_ReadingChanged;
MyMessage2 = "Compass Stopped";
}
if (Barometer.Default.IsSupported)
{
Compass.Default.Stop();
Compass.Default.ReadingChanged -= Compass_ReadingChanged;
MyMessage2 = "Compass Stopped";
}
StartBtnColor = Colors.White;
StopBtnColor = Colors.Red;
}
private void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
{
// Update UI Label with compass state
Mag1 = $"{e.Reading}";
MyCourse = Mag1.Substring(22,3);
}
private void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e)
{
// Update UI Label with barometer state
Bar1 = $"Barometer: {e.Reading}";
BarometerLabel = Bar1.Substring(35, 4) + " mBar";
}