简体   繁体   中英

gps location xamarin android not working

  • In list viewList item get the gps location using this code segment but it is not working( public class ItemGeoLocationActivity : ListActivity, ILocationListener the class )

Please look at it what I did wrong.

[Activity(Label = "ItemGeoLocationActivity")]
public class ItemGeoLocationActivity : ListActivity, ILocationListener
{
LocationManager _locMgr;
        private string Latitude;
        private string Longitude;
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            _locMgr = GetSystemService(Context.LocationService) as LocationManager;
            list = new ItemGeoLocationAdapter(this);
            this.ListAdapter = list;
        }

        protected override void OnListItemClick(ListView l, View v, int position, long id)
        {
            var item = this.list.GetItemAtPosition(position);

            var builder = new AlertDialog.Builder(this);
            builder.SetMessage(item.ItemName + "-- Latitude ="+Latitude+" Longitude="+Longitude);
            builder.SetPositiveButton("OK", (s, e) =>
            {
                var itemGeoLocation =new ItemGeoLocation
                {
                    ItemId = item.ItemId,
                    Langtitued = Convert.ToDecimal(Longitude),
                    Latitued = Convert.ToDecimal(Latitude)
                };
                new DataAccess().AddItemGeoLocation(itemGeoLocation);
                Toast.MakeText(this, item.ItemName + "-- Recode Added Succesafully", ToastLength.Long).Show(); 

            });
            builder.SetNegativeButton("Cancel", (s, e) =>
            {
                Toast.MakeText(this, item.ItemName + " Save Cancelation Success!", ToastLength.Long).Show(); 

            });
            builder.Create().Show();

        }

        protected override void OnResume()
        {
            base.OnResume();
            var locationCriteria = new Criteria();
            locationCriteria.Accuracy = Accuracy.NoRequirement;
            locationCriteria.PowerRequirement = Power.NoRequirement;
            string locationProvider = _locMgr.GetBestProvider(locationCriteria, true);
            _locMgr.RequestLocationUpdates(locationProvider, 2000, 1, this);
        }

        protected override void OnPause()
        {
            base.OnPause();
            _locMgr.RemoveUpdates(this);
        }

        #region ILocationListener implementation
        public void OnLocationChanged(Location location)
        {
            Latitude = location.Latitude.ToString();
            Longitude=location.Longitude.ToString();
        }

        public void OnProviderDisabled(string provider)
        {
        }

        public void OnProviderEnabled(string provider)
        {
        }

        public void OnStatusChanged(string provider, Availability status, Bundle extras)
        {
        }

}

I change the Emulator and It worked fine.Use http://www.andyroid.net/ Andyroid emulator. It support Xamarin too

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