0

I am writing a app where you can show your location on the screen. Everything works fine but the map is just one pixel line high. Is there a solution for this?

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

This is the main cs code of my project, i dont get breaking errors just warnings about packages. My app will start just fine just showing just one pixel line of the map.

    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;
        }
    }
}
}

This is a class which i added because it wouldn't work otherwise.

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();
   }
  }
 }

I'm using MapsUI 1.4.0 and it's trying to connect to a Newtonsoft.Json 9.0.0 that coudn't be found. so it takes 9.0.1, i don't know if that is the error but that is one of the few warnings that i get. alongside with a .NET framework that might not be fully compatible.

Zegert
  • 113
  • 10
  • 1
    If the version 1.4.0 doesn't compatible with the .net framework, try to update the package to Mapsui 2.0. And to use the Mapsui, it's not necessary to create a custom mapsui view. To use Mapsui in Xamarin.Forms, you could refer to this link.https://stackoverflow.com/questions/62130419/how-to-use-mapsui-2-0-1-with-xamarin-forms – Jarvan Zhang Jun 26 '20 at 14:09

1 Answers1

0

Everything works fine but the map is just one pixel line high. Is there a solution for this?

To use the Mapsui, it's not necessary to create a custom mapsui view. Add the declare line code to the xaml file, the view will be available.

Check the code:

<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>

How to use Mapsui 2.0.1 with Xamarin.Forms?

Jarvan Zhang
  • 968
  • 3
  • 11
  • 84