【问题标题】:Xamarin iOS - Getting User's current latitude and longitude (Simple Example)Xamarin iOS - 获取用户当前的经纬度(简单示例)
【发布时间】:2015-09-30 19:08:01
【问题描述】:

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

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

    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 文件中,我有一个页面,我在其中调用方法来获取位置,然后使用新获取的坐标以文本形式显示设备的当前位置. (顺便说一句,我确实在模拟器中设置了自定义位置。)

现在,我的问题是当我实际运行程序时,显示的字符串表示 lat.并且很长。都是 0。(“纬度 = 0,经度 = 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(原生类) - 包括以下内容:

    //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 类) - 包括以下内容:

    //Among other basic startup stuff...

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

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

    //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”。任何帮助表示赞赏!

【问题讨论】:

    标签: c# xamarin maps


    【解决方案1】:

    【讨论】:

    • 很抱歉再次打扰您,亚历山德罗,但我还有一个问题。我已经安装并运行了 NuGet 包,我已经设置了所有代码,但是当我激活 GetCurrentLocation 方法时,它会引发 Null 异常并使我的应用程序崩溃。 :( 有什么想法吗?
    • 我应该看看你的代码。如果我记得你应该在 iOS 和 Android 中初始化库。顺便说一句,从 github 下载源代码。应该是一个例子。运行它,看看它是否有效
    • 我实际上是复制粘贴了这个例子。您提到了初始化库。我在示例中没有看到这一点。我该怎么做?
    • 对于 Android,请查看 mainactivity.cs。如果你看 github 自述文件应该解释一下
    • 很抱歉打扰了,但我还有另一个错误。如果我使用简单的显示警报来告诉我我的坐标像这样,'DisplayAlert ("It Works!", DependencyService.Get().latitude + " + " + DependencyService.Get().longitude, “好的”);'它完美无缺。坐标是正确的。但是,如果我尝试实例化一个地图并将坐标作为“DependencyService.Get().latitude”和“DependencyService.Get().longitute”,它不喜欢它。例如,当 Map 实例化时,坐标是 0,0 而不是它们实际的坐标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-28
    • 2015-09-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    相关资源
    最近更新 更多