Saturday, May 4, 2024
HomeiOS Developmentcrash - Geolocation hear for location modifications crashes in IOS

crash – Geolocation hear for location modifications crashes in IOS


My app makes use of hear for location modifications in Geolocation following the microsoft script examples. It really works advantageous on Home windows and Android however in IOS clicking the beginning listening button it crashes this system. Scorching reload additionally fails as soon as the app is put in so I am struggling to discover a strategy to debug. Any recommendation will probably be gratefully acquired.

NavigationViewModel

public NavigationViewModel()
{   
    StartButtonClickedCommand = new Command(OnStartListening);
    StopButtonClickedCommand = new Command(OnStopListening);
}


public async void OnStartListening()
{
    strive
    {
        MyBearing = "0";
        MyCourse = "0";

        Geolocation.LocationChanged += Geolocation_LocationChanged;

        var request = new GeolocationListeningRequest(GeolocationAccuracy.Excessive);   

        var success = await Geolocation.StartListeningForegroundAsync(request);

        string standing = success
            ? "GPS listening"
            : "Could not begin listening";
        MyMessage = standing;

    }
    catch (Exception ex)
    {
        // Unable to start out listening for location modifications
        MyMessage = "Unable to start out";
    }
    StartBtnColor = Colours.Purple;
    StopBtnColor = Colours.White;

    if (Compass.Default.IsSupported)
    {
        if (!Compass.Default.IsMonitoring)
        {
            // Activate compass
            Compass.Default.ReadingChanged += Compass_ReadingChanged;
            Compass.Default.Begin(SensorSpeed.UI, applyLowPassFilter: true);
            MyMessage2 = "Compass began";
        }
    }
    else
    {
        // Unable to start out listening for location modifications
        MyMessage2 = "No Compass accessible";
    }

    if (Barometer.Default.IsSupported)
    {
            Barometer.Default.ReadingChanged += Barometer_ReadingChanged;
            Barometer.Default.Begin(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)
{
    // Course of e.Location to get the brand 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.Velocity) * 3.6):0.##} Km/h ";     //Velocity is in m/s so x 3.6 to get Km/h
   


    // CurrentPositionlabel.Textual content = $"I'm presently 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 levels

    i = i + 1;
    MyMessage3= $"GPS samples Rx {i}"; // testing the listening
}

public void OnStopListening()
{
    strive
    {
        Geolocation.LocationChanged -= Geolocation_LocationChanged;
        Geolocation.StopListeningForeground();

        MyMessage = "GPS Stopped"; 
    }
    catch (Exception ex)
    {
        //
        MyMessage = "Cannot cease";
    }

    // Flip off compass
    if (Compass.Default.IsSupported)
    {
        Compass.Default.Cease();
        Compass.Default.ReadingChanged -= Compass_ReadingChanged;
        MyMessage2 = "Compass Stopped";
    }

    if (Barometer.Default.IsSupported)
    {
        Compass.Default.Cease();
        Compass.Default.ReadingChanged -= Compass_ReadingChanged;
        MyMessage2 = "Compass Stopped";
    }

    StartBtnColor = Colours.White;
    StopBtnColor = Colours.Purple;
}

non-public void Compass_ReadingChanged(object sender, CompassChangedEventArgs e)
{
    // Replace UI Label with compass state
    Mag1 = $"{e.Studying}";
    MyCourse = Mag1.Substring(22,3);       
}

non-public void Barometer_ReadingChanged(object sender, BarometerChangedEventArgs e)
{
    // Replace UI Label with barometer state
    Bar1 = $"Barometer: {e.Studying}";
    BarometerLabel = Bar1.Substring(35, 4) + " mBar";
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments