Forum Xamarin.Android

Issues when trying to get the latitude and longitude of a phone without internet

I am currently trying to have my Nexus 4 show toast notifications containing the latitude and longitude of my phone with only GPS enabled, without internet. My issue is that nothing is being displayed. I'm using the GeoLocator plugin.

Code:

using Android.App;
using Android.OS;
using Android.Widget;
using Plugin.Geolocator;

namespace Tracker {

    [Activity(Label = "Tracker", MainLauncher = true, Icon = "@mipmap/icon", Theme = "@android:style/Theme.Black.NoTitleBar")]
    public class MainActivity : Activity {

        protected override void OnCreate(Bundle savedInstanceState) {

            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.Main);

            var locator = CrossGeolocator.Current;

            locator.PositionChanged += (sender, e) => {

                var position = e.Position;
                string message = "Latitude: " + position.Latitude + " | " + "Longitude: " + position.Longitude;

                Toast.MakeText(this, message, ToastLength.Long).Show();

            };

        }

    }

}

Permissions:

  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
Tagged:

Posts

  • BillyLiuBillyLiu Member, Xamarin Team Xamurai

    @Technerder

    You need to call StartListeningAsync() first.
    For example:

    async Task StartListening()
    {
        if(CrossGeolocator.Current.IsListening)
            return;
                await CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true);   
            CrossGeolocator.Current.PositionChanged += (sender, e) => {
                var position = e.Position;
                string message = "Latitude: " + position.Latitude + " | " + "Longitude: " + position.Longitude;
                Toast.MakeText(this, message, ToastLength.Long).Show();
            };
    }
    

    You could refer to following tutroial for more information:
    https://jamesmontemagno.github.io/GeolocatorPlugin/LocationChanges.html

  • TechnerderTechnerder Member ✭✭

    I'm trying this and it doesn't seem to work

    using System;
    using Android;
    using Android.App;
    using Android.Content.PM;
    using Android.OS;
    using Android.Support.V4.App;
    using Android.Support.V7.App;
    using Android.Telephony;
    using Plugin.Geolocator;
    
    namespace Tracker {
    
        [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
        public class MainActivity : AppCompatActivity {
    
            protected override void OnCreate(Bundle savedInstanceState) {
    
                base.OnCreate(savedInstanceState);
                SetContentView(Resource.Layout.activity_main);
    
                if (Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Manifest.Permission.AccessFineLocation) != (int)Permission.Granted) {
    
                    ActivityCompat.RequestPermissions(this, new String[] { Manifest.Permission.BroadcastSms, Manifest.Permission.SendSms,
                        Manifest.Permission.WriteSms, Manifest.Permission.AccessCoarseLocation, Manifest.Permission.AccessFineLocation }, 1);
    
                }
    
                CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(5), 10, true);
    
                CrossGeolocator.Current.PositionChanged += (sender, e) => {
    
                    var position = e.Position;
                    string message = "Latitude: " + position.Latitude + " | " + "Longitude: " + position.Longitude;
                    SmsManager.Default.SendTextMessage("Snip", null, message, null, null);
    
                };
    
            }
    
        }
    
    }
    
Sign In or Register to comment.