【问题标题】:issues with the Geolocator xamarin plugin by James MontemagnoJames Montemagno 的 Geolocator xamarin 插件的问题
【发布时间】:2017-01-29 18:55:36
【问题描述】:

您好,我正在开发一个 xamarin 形式的应用程序,该应用程序需要获取 gps 位置并将纬度和经度组合成 forcast.io 的 url 我正在使用 James Montemagno 的 Geolocator 插件并遵循自述文件,但我我仍然收到这些错误:

严重性代码描述项目文件行抑制状态 错误 CS0165 使用未分配的局部变量“msi”

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

严重性代码描述项目文件行抑制状态 错误 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 个平台(Froms、iOS、Android)上都安装了最新的 Geolocator nuget 包

提前致谢!

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    您的 _locator 定义应如下所示

    private readonly IGeolocator _locator;

    【讨论】:

      【解决方案2】:
      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();
      }
      

      【讨论】:

        【解决方案3】:

        所以@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 的答案都标记为正确,所以他们俩都得到了充分的信任!

        再次感谢你们!

        【讨论】:

          猜你喜欢
          • 2018-04-30
          • 1970-01-01
          • 2020-02-03
          • 2013-12-10
          • 2018-12-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-04
          相关资源
          最近更新 更多