0

我正在编写一个应用程序,您可以在其中显示您在屏幕上的位置。一切正常,但地图只有一个像素线高。有解决方案吗?

  <Grid x:Name="MapGrid"
     HorizontalOptions="CenterAndExpand"/>
  <Grid>

这是我项目的主要 cs 代码,我没有收到重大错误,只是关于包的警告。我的应用程序会很好地启动,只显示地图的一个像素线。

    using System;
    using System.ComponentModel;
    using Xamarin.Forms;
    using Xamarin.Essentials;
    using Mapsui.Utilities;
    using Mapsui.Projection;
    using System.Runtime.CompilerServices;
    using System.Threading.Tasks;
    private MapsuiView mapControl;
    
    public MainPage()
    {
        InitializeComponent();

        mapControl = new MapsuiView();
        mapControl.NativeMap.Layers.Add(OpenStreetMap.CreateTileLayer());
        MapGrid.Children.Add(mapControl);

        Device.StartTimer(TimeSpan.FromSeconds(2), () =>
         {
             GoToLocation();
             return true;
         });

        async void GoToLocation()
        {
            Location location = await getLocation();
            if (location != null)
            {
                var sphericalMercatorCoordinate = SphericalMercator.FromLonLat(location.Longitude, location.Latitude);
                mapControl.NativeMap.NavigateTo(sphericalMercatorCoordinate);
                mapControl.NativeMap.NavigateTo(mapControl.NativeMap.Resolutions[15]);
            }
        }
        async Task<Location> getLocation()
        {
            Location location = null;
            try
            {
                var request = new GeolocationRequest(GeolocationAccuracy.Best);
                location = await Geolocation.GetLocationAsync(request);

                if(location != null)
                {
                    Console.WriteLine($"Latitude: {location.Latitude}, " +
                        $"Longitude: {location.Longitude}, " +
                        $"Altitude: {location.Altitude}, " +
                        $"Accuracy: {location.Accuracy}"); 
                }
            }
            catch (FeatureNotSupportedException fnsEx)
            {
                Console.WriteLine(fnsEx.ToString());
            }
            catch (FeatureNotEnabledException fneEx)
            {
                Console.WriteLine(fneEx.ToString());
            }
            catch (PermissionException pEx)
            {
                Console.WriteLine(pEx.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine("Overige fout: " + ex.ToString());
            }
            return location;
        }
    }
}
}

这是我添加的一个类,因为否则它不会工作。

using System;
using System.Collections.Generic;
using System.Text;

namespace mobile_83504_3
 {
   public class MapsuiView : Xamarin.Forms.View
    {
      public Mapsui.Map NativeMap { get; }
      protected internal MapsuiView()
    {
        NativeMap = new Mapsui.Map();
   }
  }
 }

我正在使用 MapsUI 1.4.0,它正在尝试连接到找不到的 Newtonsoft.Json 9.0.0。所以它需要 9.0.1,我不知道这是否是错误,但这是我收到的少数警告之一。与可能不完全兼容的 .NET 框架一起使用。

4

1 回答 1

0

一切正常,但地图只有一个像素线高。有解决方案吗?

要使用 Mapsui,无需创建自定义 mapsui 视图。将声明行代码添加到 xaml 文件,视图将可用。

检查代码:

<ContentPage
    ...
    xmlns:mapsui="clr-namespace:Mapsui.UI.Forms;assembly=Mapsui.UI.Forms">
    <StackLayout>
        <mapsui:MapView x:Name="mapView"
            VerticalOptions="FillAndExpand"
            HorizontalOptions="Fill"
            BackgroundColor="Gray" />
    </StackLayout>
</ContentPage>

如何将 Mapsui 2.0.1 与 Xamarin.Forms 一起使用?

于 2020-06-26T14:14:48.517 回答