James Montemagno使用Geolocator xamarin插件的问题

时间:2016-09-21 13:28:53

标签: xamarin xamarin.forms

您好我正在开发一个xamarin表单的应用程序需要获取gps位置并将纬度和经度组合成forcast.io的网址我使用James Montemagno的Geolocator插件并按照自述文件但是我仍然遇到这些错误:

  

严重级代码描述项目文件行抑制状态   错误CS0165使用未分配的本地变量' msi'

     

严重级代码描述项目文件行抑制状态   错误CS0266无法隐式转换类型   ' Plugin.Geolocator.Abstractions.IGeolocator'至   ' Plugin.Geolocator.CrossGeolocator&#39 ;.存在显式转换   (你错过了演员吗?)

     

严重级代码描述项目文件行抑制状态   错误CS1061' CrossGeolocator'不包含的定义   ' IsGeolocationEnabled'没有扩展方法' IsGeolocationEnabled'   接受第一个类型为' CrossGeolocator'可以找到   (您是否缺少using指令或程序集引用?)

     

严重级代码描述项目文件行抑制状态   错误CS1061' CrossGeolocator'不包含的定义   ' GetPositionAsync'没有扩展方法' GetPositionAsync'   接受第一个类型为' CrossGeolocator'可以找到   (您是否缺少using指令或程序集引用?)

     

严重级代码描述项目文件行抑制状态   错误CS1061' CrossGeolocator'不包含的定义   ' DesiredAccuracy'没有扩展方法' DesiredAccuracy'验收   第一个类型为' CrossGeolocator'可以找到(是你   缺少using指令或程序集引用?)

然后是雷达代码:

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      
        }    
    }
}

我在所有3个平台上安装了最新的Geolocator nuget软件包(Froms,iOS,Android)我正在使用VS2015更新3,我仍然在学习xamrarin表格,所以我很抱歉问这样一个noob问题。

提前致谢!

3 个答案:

答案 0 :(得分:0)

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();
}

答案 1 :(得分:0)

您的_locator定义应该如下所示

implicit val semigroupInstance = new Semigroup[ResourceTree] { override def combine(x: ResourceTree, y: ResourceTree): ResourceTree = { dummyCombine(x, y) } } // FIXME: see if there's a better way to avoid the "no instance of Semigroup" problem def dummyCombine(x: ResourceTree, y: ResourceTree): ResourceTree = { ResourceTree( x.resources |+| y.resources ) }

答案 2 :(得分:0)

所以@William Corncob Decker和@Greensy的答案都是正确的

_locator定义必须是:private readonly IGeolocator _locator;

我确实错过了解决msi插件的代码:

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();
}

我这样做的唯一原因是因为我无法将William Corncob Decker和Greensy的答案都标记为正确,所以对他们两人都给予了充分的信任!

再次感谢你们!