与xam.forms.maps一起使用的xam.plugin.geolocator使屏幕变黑

时间:2018-03-13 02:47:52

标签: google-maps xamarin xamarin.forms

下一个代码用于制作一个具有特定坐标的给定信息的自定义引脚,我对其进行了调整,因此它可以使用用户的坐标

using System.Collections.Generic;
using Xamarin.Forms;
using Xamarin.Forms.Maps;
using Plugin.Geolocator;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CustomRenderer
{
public partial class MapPage : ContentPage
{
    //Plugin.Geolocator.Abstractions.Position position = null;
    public MapPage()
    {

        InitializeComponent();
        App.position = Initialize().Result;

        var pin = new CustomPin
        {
            Type = PinType.Place,
            Position = new Position(App.position.Latitude, App.position.Longitude),
            Label = "Xamarin San Francisco Office",
            Address = "394 Pacific Ave, San Francisco CA",
            Id = "Xamarin",
            Url = "http://xamarin.com/about/"
        };
        customMap.CustomPins = new List<CustomPin> { pin };
        customMap.Pins.Add(pin);
        customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(App.position.Latitude,App.position.Longitude), Distance.FromMiles(1.0)));
    }

    public async Task<Plugin.Geolocator.Abstractions.Position> Initialize()
    {
        var locator = CrossGeolocator.Current;
        locator.DesiredAccuracy = 20;

        if (!locator.IsGeolocationAvailable || !locator.IsGeolocationEnabled)
        {
            try
            {
                return await locator.GetLastKnownLocationAsync();
            }

            catch
            {

            }
        }

        try
        {
           return await locator.GetPositionAsync(timeout: TimeSpan.FromMilliseconds(30000.0));
        } //miliseconds 

        catch { }
        return await locator.GetLastKnownLocationAsync();
    }
}

}

但是当我这样做时,如果我不使用这行代码,我的屏幕会变黑。

App.position = Initialize().Result;

并将位置更改为数字,将显示地图中的点,并且地图正常工作。为什么 Initialize()。结果不起作用?

0 个答案:

没有答案