简体   繁体   中英

C# Xamarin Geolocator Plugin not updating location

I am creating an app using Xamarin Forms which tracks a user's location. I am currently trying to use the this Geolocator plugin for tracking a user's location. I have followed the steps in the plugin's tutorial, but my location is currently not refreshing.

Here is my code:

public WhosOnFarm ()
{
    InitializeComponent ();
    locator = CrossGeolocator.Current;

    locator.PositionChanged += (sender, e) => {
        DisplayAlert("Success", "Your position has changed!", "OK");
        var position = e.Position;
        longitudeLabel.Text = "Longitude: "+position.Longitude.ToString() + " Latitude: " + position.Latitude.ToString();
    };
}

In the tutorial it did not specify the type of locator so I assumed the data it requires it CrossGeolocator.Current;

What am I missing?

It is a bit hard to answer your question without the project set-up. Location plug-ins, such as the GeoLocation, require some properties (rights) to be set-up in the project. Did you add those? On which platform are you using it? On what device or emulator did you test it?

I have implemented same library following is the my code which perfectly working on android and iOS

     public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            CrossGeolocator.Current.AllowsBackgroundUpdates = true;
            CrossGeolocator.Current.DesiredAccuracy = 50;
            CrossGeolocator.Current.PositionChanged += Current_PositionChanged;
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            updateLocation();
        }

        private async void updateLocation()
        {
            try
            {
                var locator = CrossGeolocator.Current;
                locator.DesiredAccuracy = 50;
                locator.AllowsBackgroundUpdates = true;
                var position = await locator.GetPositionAsync(timeoutMilliseconds: 10000);

                if (position == null)
                {
                    lblData.Text = "null gps :(";
                    return;
                }
                lblData.Text = string.Format("\nTime: {0} \nLat: {1} \nLong: {2} \n Altitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \n Heading: {6} \n Speed: {7}",
            position.Timestamp, position.Latitude, position.Longitude,
            position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);

                mapVar.MoveToRegion(
   MapSpan.FromCenterAndRadius(
       new Position(position.Latitude,position.Longitude), Distance.FromKilometers(1)));
                await CrossGeolocator.Current.StartListeningAsync(1, 2, true);
            }
            catch (Exception)
            {
            }
        }


        private void Current_PositionChanged(object sender, Plugin.Geolocator.Abstractions.PositionEventArgs e)
        {
            if (e.Position != null)
            {
                Device.BeginInvokeOnMainThread(() => {
                    var position = e.Position;
                    lblData.Text = string.Format("\n\nTime: {0} \nLat: {1} \nLong: {2} \n Altitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \n Heading: {6} \n Speed: {7}",
            position.Timestamp, position.Latitude, position.Longitude,
            position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);
                    mapVar.Pins.Clear();
                    mapVar.Pins.Add(new Xamarin.Forms.Maps.Pin
                    {
                        Position = new Xamarin.Forms.Maps.Position(position.Latitude, position.Longitude),
                        Type = Xamarin.Forms.Maps.PinType.Place,
                        Label = "My Location"
                    });

                    mapVar.MoveToRegion(
   MapSpan.FromCenterAndRadius(
       new Position(position.Latitude, position.Longitude), Distance.FromKilometers(1)));

                });
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM