0
votes

Hello I am working on a app in xamarin forms that needs to take the gps location and combine the latitude and longitude into a url for forcast.io i'm using the Geolocator plugin by James Montemagno and followed the readme but i'm still getting these errors:

Severity Code Description Project File Line Suppression State Error CS0165 Use of unassigned local variable 'msi'

Severity Code Description Project File Line Suppression State Error CS0266 Cannot implicitly convert type 'Plugin.Geolocator.Abstractions.IGeolocator' to 'Plugin.Geolocator.CrossGeolocator'. An explicit conversion exists (are you missing a cast?)

Severity Code Description Project File Line Suppression State Error CS1061 'CrossGeolocator' does not contain a definition for 'IsGeolocationEnabled' and no extension method 'IsGeolocationEnabled' accepting a first argument of type 'CrossGeolocator' could be found (are you missing a using directive or an assembly reference?)

Severity Code Description Project File Line Suppression State Error CS1061 'CrossGeolocator' does not contain a definition for 'GetPositionAsync' and no extension method 'GetPositionAsync' accepting a first argument of type 'CrossGeolocator' could be found (are you missing a using directive or an assembly reference?)

Severity Code Description Project File Line Suppression State Error CS1061 'CrossGeolocator' does not contain a definition for 'DesiredAccuracy' and no extension method 'DesiredAccuracy' accepting a first argument of type 'CrossGeolocator' could be found (are you missing a using directive or an assembly reference?)

and then here's the radar code:

using Xamarin.Forms;
using System;
using System.Diagnostics;
using Plugin.Geolocator;

namespace AppName.Radar
{
    public interface MyLocationTracker
    {
        void ObtainMyLocation();
        event EventHandler<MyLocationEventArgs> locationObtained;    
    }

    public interface MyLocationEventArgs
    {
        double lat { get; set; }
        double lng { get; set; }
    }  

    public partial class RadarHome : ContentPage
    {    
        private readonly CrossGeolocator _locator;
        private double BetaLat;
        private double BetaLog;

        public RadarHome()
        {
            MyLocationTracker msi;

            _locator = CrossGeolocator.Current;

            if (_locator.IsGeolocationEnabled == false)
            {   
                if (Device.OS == TargetPlatform.Android)
                {                    

                    msi.locationObtained += (object Esender, MyLocationEventArgs ew) =>
                    {
                        Debug.WriteLine(ew.lat);
                    };
                    msi.ObtainMyLocation();  
                }

                else if (Device.OS == TargetPlatform.iOS)
                {
                    msi = DependencyService.Get<MyLocationTracker>();
                    msi.locationObtained += (object Jsender, MyLocationEventArgs je) =>
                    {
                        Debug.WriteLine(je.lat);
                    };
                    msi.ObtainMyLocation();
                }
            }

            _locator.DesiredAccuracy = 50;

            GetPositionAsynchronously();

            string str = string.Format(
                "https://forecast.io/?mobile=1#/f/Lat:{0} , Long: {1}", BetaLat, BetaLog);

            var client = new System.Net.Http.HttpClient();

            client.BaseAddress = new Uri(str);
        }

        private async void GetPositionAsynchronously()
        {
            //will run asynchronously in a diff thread
            var position = await _locator.GetPositionAsync(timeoutMilliseconds: 100000);

            BetaLat = position.Latitude; //will work
            BetaLog = position.Longitude; // will work      
        }    
    }
}

I have the latest Geolocator nuget package installed on all 3 platforms (Froms, iOS, Android) I am using VS2015 update 3 and am still learning xamrarin forms so I'm sorry for asking such a noob question.

Thanks in advance!

3

3 Answers

0
votes

Your _locator definition should look like this

private readonly IGeolocator _locator;

0
votes
if (Device.OS == TargetPlatform.Android)
{       
    //You missed to resolve plugin there
    msi = DependencyService.Get<MyLocationTracker>();

    msi.locationObtained += (object Esender, MyLocationEventArgs ew) =>
    {
        Debug.WriteLine(ew.lat);
    };
    msi.ObtainMyLocation();
}
0
votes

So both @William Corncob Decker, and @Greensy answers were correct

_locator definition needs to be: private readonly IGeolocator _locator;

and I did miss to resolve the msi plugin heres that code:

if (Device.OS == TargetPlatform.Android)
{
    //You missed to resolve plugin there
    msi = DependencyService.Get<MyLocationTracker>();

    msi.locationObtained += (object Esender, MyLocationEventArgs ew) =>
    {
        Debug.WriteLine(ew.lat);
    };
    msi.ObtainMyLocation();
}

The only reason I'm doing this is because I can't mark both William Corncob Decker, and Greensy answers as correct so full credit goes to both of them!

Thank you guys again!