Xamarin iOS - 获取用户当前的纬度和经度(简单示例)

Xamarin iOS - Getting User's current latitude and longitude (Simple Example)

在 Xamarin Forms 文件中,我有一个带有方法的接口:GetCurrentLocation,以及两个属性(均为字符串):纬度和经度。

在 Xamarin iOS 文件中,我有一个来自前面提到的接口的 class 接口。该方法(现在详述)指出,在激活时,它会创建一个 MKMapView,获取用户的纬度。和 long.,并将它们设置为等于纬度。和长。 class的属性(如下图)

    public string latitude { get; set; }
    public string longitude { get; set; }
    public void GetCurrentLocation () {
        var vm = new MKMapView();
        latitude = mv.UserLocation.Coordinate.Latitude.ToString();
        longitude = mv.UserLocation.Coordinate.Longitude.ToString();
    }

对于这个例子,这个方法只会被调用一次,目的只是为了获取纬度。和长。设备并以文本形式显示。

(经过一些依赖服务恶作剧......)回到 Xamarin Forms 文件中,我有一个页面,我在其中调用方法来获取位置,然后使用新获取的坐标来显示设备的当前位置在文本中。 (顺便说一句,我确实在模拟器中设置了一个自定义位置。)

现在,我的概率是,当我实际 运行 程序时,显示的字符串表示经纬度。和长。都是 0。 ("Latitude = 0, Longitude = 0") WHYYYYYYYYYYY?!?!? 终极悲伤脸


更新:这是我在添加 NuGet 包后的代码 ---

首先——IMapView(界面)——包括以下内容:

    public interface IMapView {
            double latitude { get; set; }
            double longitude { get; set; }
            void GetLatandLong ();
            IGeolocator locator { get; set; }
            Position position { get; set; }
    };

第二个 - MapView_iOS(原生 class) - 包括以下内容:

    //Obvious instantiations of the Interfaced Properties...

    public async void GetLatandLong () {

            locator = CrossGeolocator.Current;
            locator.DesiredAccuracy = 100;

            position = await locator.GetPositionAsync (timeoutMilliseconds: 10000);

            latitude = Convert.ToDouble (position.Latitude);
            longitude = Convert.ToDouble (position.Longitude);
    }

第三 - App.cs (x.forms class) - 包括以下内容:

    //Among other basic startup stuff...

    protected override void OnStart()
    {
        DependencyService.Get<IMapView> ().GetLatandLong();
    }

第四个也是最后一个 - ContentPage (x.forms class) - 包含:

    //Outside of the constructor:
    protected override void OnAppearing () {
        DisplayAlert ("It Works!", DependencyService.Get<IMapView> ().latitude + " + " + DependencyService.Get<IMapView> ().longitude, "Okay");
    }

    //Inside of the constructor:
    var LabelGest = new TapGestureRecognizer();
    LabelGest.Tapped += (s, e) => {
            DisplayAlert ("It Works!", DependencyService.Get<IMapView> ().latitude + " + " + DependencyService.Get<IMapView> ().longitude, "Okay");
    };

    var MyLabel = new Label {
            Text = "My Label",
            GestureRecognizers = {LabelGest}
    };

所以你可以看到我在两个不同的地方设置了显示坐标的 DisplayAlert:一次是在我打开页面时,一次是在我点击该页面上的某个项目时。当我点击该项目时,DisplayAlert 会正确激活并提供正确的坐标。但是,第一个 DisplayAlert(在页面出现时激活的那个)不正确地关闭。它适时弹出,但总是给出“0+0”的信息。 :( 这是我的问题的最简单示例。我在单独的页面上也有一个地图,如下所示:

    Content = new Map (MapSpan.FromCenterAndRadius (new Xamarin.Forms.Maps.Position
                    (DependencyService.Get<IMapView> ().latitude, DependencyService.Get<IMapView> ().longitude),
                        Distance.FromMiles (0.3))) {
            IsShowingUser = true,
            HeightRequest = 100,
            WidthRequest = 960,
            VerticalOptions = LayoutOptions.FillAndExpand
    }

这会呈现类似的结果,打开位置在地图上为“0, 0”。感谢您的帮助!

您可以使用https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Geolocator

这是 Xamarin Forms 的插件