Getting Geo Coordinates

MAJosephMAJoseph Member ✭✭

I'm trying to create an app. Here I'm trying to get the user's current location by clicking the button. But it produces an exception like Not Implemented exception - This functionality is not implemented in the portable version of this assembly. You should reference the NuGet package from your main application project in order to reference the platform-specific implementation

This is my code

string answer ="";
           try
           {
                await CrossGeolocator.Current.StartListeningAsync(new 
                  TimeSpan(20000),10);
                if (CrossGeolocator.Current.IsListening)
                {
                    var locator = CrossGeolocator.Current;
                    locator.DesiredAccuracy = 50;
                    var position = await 
                      locator.GetPositionAsync(TimeSpan.FromSeconds(20000));
                    string lat = position.Latitude.ToString();
                    string lon = position.Longitude.ToString();
                    answer = lat + lon;
                }
                else
                {
                    answer= "Not listening";
                }

            }
            catch (Exception ex)
            {
                answer= ex.Message;
            }

I need the result which contains the longitude and latitude values. What I've to do in my project?

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    What the version of the NETStandard.Library? Update the NETStandard.Library and ensure that you have your compile set to android 7.1 or higher.

    Refer to https://github.com/jamesmontemagno/DeviceInfoPlugin/issues/20

  • AnandmauryaAnandmaurya INMember ✭✭
    edited April 3

    @MAJoseph In some devices it will work but not all devices. So better to use Xamarin.GooglePlayServices.Location .

    Refer to [github.com/jamesmontemagno/blog-samples/tree/master/GooglePlayLocationAPIs]

  • MAJosephMAJoseph Member ✭✭

    No @Anandmaurya To use Xamarin.GooglePlayServices.Location I need to upgrade something in visual studio. I don't need to do that. I just need the solution with this existing one. I need only this.

  • AnandmauryaAnandmaurya INMember ✭✭

    @MAJoseph Please follow these steps:
    **Step 1: **Add this line in your onCreate() method.

         Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
    

    **Step 2: ** Need to add run time permission on your MainActivity.cs.

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    **Step 3: **Also add this method in your MainActivity.cs.

    public async Task CheckPermissions(Permission permission)
    {
    var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
    bool request = false;
    if (permissionStatus == PermissionStatus.Denied)
    {
    request = true;
    }
    if (request || permissionStatus != PermissionStatus.Granted)
    {
    await CrossPermissions.Current.RequestPermissionsAsync(permission);
    }
    return true;
    }

    **Step 4: ** Finally on your click event method add this code.

            try
            {
                var hasPermission = await CheckPermissions(Permission.Location);
                if (!hasPermission)
                    return;
    
                var locator = CrossGeolocator.Current;
                location.Text = "Getting location...";
    
                var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(30), null, false);
    
                if (position == null)
                {
                    location.Text = "null gps :(";
                    return;
                }
                location.Text = string.Format("Time: {0} \nLat: {1} \nLong: {2}",
                    position.Timestamp, position.Latitude, position.Longitude);
    
            }
            catch (Exception ex)
            {
                location.Text = "Exception";
            }
    

    Note : Not required to add any permission on your manifest file.

  • AnandmauryaAnandmaurya INMember ✭✭
    edited April 4

    @MAJoseph I have tested this code It's working properly.

          public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] 
                                                Android.Content.PM.Permission[] grantResults)
          {
            base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
            PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
          }
    
        public async Task<bool> CheckPermissions(Permission permission)
        {
            var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
            bool request = false;
            if (permissionStatus == PermissionStatus.Denied)
            {
                request = true;
            }
            if (request || permissionStatus != PermissionStatus.Granted)
            {
                await CrossPermissions.Current.RequestPermissionsAsync(permission);
            }
            return true;
        }
        public async void GetLocatonClickEvent(object sender, EventArgs eventArgs)
        {
    
            try
            {
                var hasPermission = await CheckPermissions(Permission.Location);
                if (!hasPermission)
                    return;
    
                var locator = CrossGeolocator.Current;
                location.Text = "Getting location...";
    
                var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(30), null, false);
    
                if (position == null)
                {
                    location.Text = "null gps :(";
                    return;
                }
                location.Text = string.Format("Time: {0} \nLat: {1} \nLong: {2}",
                    position.Timestamp, position.Latitude, position.Longitude);
    
            }
            catch (Exception ex)
            {
                location.Text = "Exception";
            }           
     }
    

    Note : Add this line in your onCreate() method.

     Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
    
  • MAJosephMAJoseph Member ✭✭

    Where you put the first portion of code. Bcoz it shows not implemented exception while calling the interface members outside shared portion in the project. Otherwise if I call the line inside the shared portion I can't inherit the interface with explicitly calling it. I don't know how can I use the code.

  • MAJosephMAJoseph Member ✭✭
    edited April 5


    This is that error. I need to inherit the interface .. I think then it will be done.

  • AnandmauryaAnandmaurya INMember ✭✭

    @MAJoseph Please follow these steps:
    Step 1: Add this line in your onCreate() method.

     Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
    

    Step 2. Need to add run time permission on your MainActivity.cs

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    Step 3. Also add this method in your MainActivity.cs.

        public async Task<bool> CheckPermissions(Permission permission)
        {
            var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
            bool request = false;
            if (permissionStatus == PermissionStatus.Denied)
            {
                request = true;
            }
            if (request || permissionStatus != PermissionStatus.Granted)
            {
                await CrossPermissions.Current.RequestPermissionsAsync(permission);
            }
            return true;
        }
    

    Step 4. Finally on your click event method add this code.

            try
            {
                var hasPermission = await CheckPermissions(Permission.Location);
                if (!hasPermission)
                    return;
    
                var locator = CrossGeolocator.Current;
                location.Text = "Getting location...";
    
                var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(30), null, false);
    
                if (position == null)
                {
                    location.Text = "null gps :(";
                    return;
                }
                location.Text = string.Format("Time: {0} \nLat: {1} \nLong: {2}",
                    position.Timestamp, position.Latitude, position.Longitude);
    
            }
            catch (Exception ex)
            {
                location.Text = "Exception";
            }
    

    Note : Not required to add any permission on your manifest file.

  • AnandmauryaAnandmaurya INMember ✭✭

    @MAJoseph Please follow these steps:
    Step 1: Add this line in your onCreate() method.

     Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, savedInstanceState);
    

    Step 2. Need to add run time permission on your MainActivity.cs

    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    Step 3. Also add this method in your MainActivity.cs.

        public async Task<bool> CheckPermissions(Permission permission)
        {
            var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
            bool request = false;
            if (permissionStatus == PermissionStatus.Denied)
            {
                request = true;
            }
            if (request || permissionStatus != PermissionStatus.Granted)
            {
                await CrossPermissions.Current.RequestPermissionsAsync(permission);
            }
            return true;
        }
    

    Step 4. Finally on your click event method add this code.

            try
            {
                var hasPermission = await CheckPermissions(Permission.Location);
                if (!hasPermission)
                    return;
    
                var locator = CrossGeolocator.Current;
                location.Text = "Getting location...";
    
                var position = await locator.GetPositionAsync(TimeSpan.FromSeconds(30), null, false);
    
                if (position == null)
                {
                    location.Text = "null gps :(";
                    return;
                }
                location.Text = string.Format("Time: {0} \nLat: {1} \nLong: {2}",
                    position.Timestamp, position.Latitude, position.Longitude);
    
            }
            catch (Exception ex)
            {
                location.Text = "Exception";
            }
    

    Note : Not required to add any permission on your manifest file.

Sign In or Register to comment.