0

I am trying to get location from the Tizen.Location.Locator using the GetLocation method but I'm getting the values of latitude and longitude set to 0.

I am using:

  • Visual Studio 2017
  • Emulator of Wearable 4.0
  • Have already granted the privileges for using location, also added the location feature in the manifest file.

Here's my code:

private static Locator locator = new Locator(LocationType.Gps);
try
{
    if (locator != null)
    {
        // Starts the Locator
        locator.Start();

        //Get Location
        Tizen.Location.Location currentLocation = locator.GetLocation();

        //Update xaml view
        latitudeValue.Text = currentLocation.Latitude.ToString();
        longitudeValue.Text = currentLocation.Longitude.ToString();
    }   
}
catch (InvalidOperationException exception)
{
    // Exception handling here
}

there's no description available about the variable currentLocation since it is being treated as a bool there. When I try to get the description by the quick watch feature of Visual Studio (by pressing Shift + F9), I get another error related to conversion of that bool variable

member reference base type 'bool' is not a structure or union

The above code is not showing any ServiceStateChanged event registered, but I've also included that using the way shown in the sample code, but it didn't work for me.

Now wondering about what I'm doing wrong here.

FreakyAli
  • 13,349
  • 3
  • 23
  • 63
Itban Saeed
  • 1,660
  • 5
  • 25
  • 38
  • 1
    Emulator emulates location. Have You changed to desired location in emulator's control panel? If not configured it will return 0,0 – Patryk Falba Aug 05 '19 at 23:06
  • I really forgot about it .. thanks a lot for making my day :) Now it's working in emulator but it's giving the mock values on real device as well (as I set the mock values using `locator.SetMockLocation()` method) when **I removed the mocking** and started debugging, it threw **an exception on `locator.Start()` method**. Unfortunately I'm unable to see the description of the exception here. – Itban Saeed Aug 07 '19 at 15:31

1 Answers1

0

Your GPS most of time inactive because of battery consumption or Get location via Service provider

or

Use this Plugin to Get Current Location - https://github.com/jamesmontemagno/GeolocatorPlugin

 public async Task<Position> GetCurrentLocation()
{
   public static async Task<Position> GetCurrentPosition()
    {
            Position position = null;
            try
            {
                    var locator = CrossGeolocator.Current;
                    locator.DesiredAccuracy = 100;

                    position = await locator.GetLastKnownLocationAsync();

                    if (position != null)
                    {
                            //got a cahched position, so let's use it.
                            return position;
                    }

                    if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
                    {
                            //not available or enabled
                            return null;
                    }

                    position = await locator.GetPositionAsync(TimeSpan.FromSeconds(20), null, true);

            }
            catch (Exception ex)
            {
                    Debug.WriteLine("Unable to get location: " + ex);
            }

            if (position == null)
                    return null;

            var output = string.Format("Time: {0} \nLat: {1} \nLong: {2} \nAltitude: {3} \nAltitude Accuracy: {4} \nAccuracy: {5} \nHeading: {6} \nSpeed: {7}",
                    position.Timestamp, position.Latitude, position.Longitude,
                    position.Altitude, position.AltitudeAccuracy, position.Accuracy, position.Heading, position.Speed);

            Debug.WriteLine(output);

            return position;
    }
}
logeshpalani31
  • 1,416
  • 12
  • 33
  • Good to see an alternate way of doing this. I've used [this](https://stackoverflow.com/questions/41763540/crossgeolocators-getpositionasync-does-not-work/41768952#41768952) way combined with your `if-else` condition. My code looks like [this](https://forums.xamarin.com/discussion/comment/305601#Comment_305601) but I'm getting an exception, when I check the value of `CrossGeolocator.IsSupported` or `locator.IsGeolocationAvailable` or even `var position`, I get an error in quick watch about the _undeclared identifier_ over, am I missing any reference to use `CrossGeolocator` properly ? – Itban Saeed Aug 04 '19 at 13:17
  • Alternatively get location in local service provider – logeshpalani31 Aug 04 '19 at 14:24