【问题标题】:getting location in xamarin forms以 xamarin 形式获取位置
【发布时间】:2018-02-10 20:16:22
【问题描述】:

我正在尝试从我的设备获取位置。这是代码:

public class Geolocation
{        
    private readonly LocationManager _locationManager;

    public Geolocation()
    {
        _locationManager = Forms.Context.GetSystemService(Context.LocationService) as LocationManager;
    }

    public Task<double> GetLocation()
    {

        var provider = _locationManager.GetBestProvider(new Criteria() { Accuracy = Accuracy.Fine }, true);
        var location = _locationManager.GetLastKnownLocation(provider);

        if(location == null) return null;

        var result = location.Latitude;

        return result;
    }
}

我从一本关于 xamarin 的书中得到了这段代码。我不明白,为什么大部分时间我都没有得到结果?几次都有效,但我不知道为什么。对于那些想知道为什么我不使用 James Montemagno 的 Geolocator 插件的人,因为我不能。它需要一些无法在我的 VS 中更新的内容。

【问题讨论】:

  • 您能否详细说明为什么不能使用 GeoLocator 插件?哪些依赖项与哪些 NuGet 包冲突?

标签: c# xamarin.forms xamarin.android geolocation


【解决方案1】:

尝试通过依赖服务来协调。阅读 xamrin 文档 Get Current Device Location github中有一个示例项目get_current_device_location

您仍然希望它在 xamarin.forms 中实现,然后观看此视频link

【讨论】:

    【解决方案2】:
    _locationManager.GetBestProvider(new Criteria() { Accuracy = Accuracy.Coarse}, true);
    

    这将使您更快地获得位置,尽管准确度当然较低。
    Here 是如何检索位置的综合示例。

    【讨论】:

      【解决方案3】:

      以下是如何使用 Xamarin.Forms 项目中的 GeoLocator 插件。

      此代码 sn-p 取自示例 Xamarin.Forms 应用,该应用使用带有 Xamarin.Forms 和 MVVM 架构的 GeoLocator 插件:

      https://github.com/brminnick/GeolocatorSample

      public class Geolocation
      { 
          string _latLongText, _latLongAccuracyText, _altitudeText, _altitudeAccuracyText;
      
          public Geolocation()
          {
              StartListeningForGeoloactionUpdates.GetAwaiter().GetResult();
          }
      
          public string LatLongText
          {
              get => _latLongText;
              set => _latLongText = value;
          }
      
          public string LatLongAccuracyText
          {
              get => _latLongAccuracyText;
              set => _latLongAccuracyText = value;
          }
      
          public string AltitudeText
          {
              get => _altitudeText;
              set => _altitudeText = value;
          }
      
          public string AltitudeAccuracyText
          {
              get => _altitudeAccuracyText;
              set => _altitudeAccuracyText = value;
          }
      
          Task StartListeningForGeoloactionUpdates()
          {
              bool isGeolocationAvailable = CrossGeolocator.IsSupported
                                          && CrossGeolocator.Current.IsGeolocationAvailable
                                          && CrossGeolocator.Current.IsGeolocationEnabled;
      
              if (isGeolocationAvailable)
              {
                  CrossGeolocator.Current.PositionChanged += HandlePositionChanged;
                  return CrossGeolocator.Current.StartListeningAsync(TimeSpan.FromSeconds(1), 10);
              }
      
              LatLongText = "Geolocation Unavailable";
              return Task.CompletedTask;
          }
      
          void HandlePositionChanged(object sender, PositionEventArgs e)
          {
              AltitudeAccuracyText = $"{ConvertDoubleToString(e.Position.AltitudeAccuracy, 0)}m";
              AltitudeText = $"{ConvertDoubleToString(e.Position.Altitude, 2)}m";
              LatLongAccuracyText = $"{ConvertDoubleToString(e.Position.Accuracy, 0)}m";
              LatLongText = $"{ConvertDoubleToString(e.Position.Latitude, 2)}, {ConvertDoubleToString(e.Position.Longitude, 2)}";
      
              string ConvertDoubleToString(double number, int decimalPlaces) => number.ToString($"F{decimalPlaces}");
          }
      }
      

      【讨论】:

      • 请回复我的朋友,我已经尝试使用地理定位器插件,但正如我在问题中所说,我无法使用它,因为它需要 api lvl25,而我的 vs 无法下载我不使用的那个 api不知道为什么
      • 使用 Android SDK 管理器下载最新的 Android SDK:developer.xamarin.com/guides/android/getting_started/…
      • 你不应该试图重新发明轮子; GeoLocator 插件完全满足您的需求。
      • 我确实做到了,但由于未知原因,它既没有出现在 project.droid 属性中的“使用 android 版本编译”列表也没有出现“目标 android 版本”列表,但我会尝试重新安装它如果这能解决我的朋友的问题,我会注意到你
      • 是的,我知道我不需要这样做,但有时我想它需要作为临时解决方案来完成
      猜你喜欢
      • 2019-11-16
      • 2016-11-03
      • 2017-03-31
      • 2016-10-22
      • 2012-02-15
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多