2

As i have tried the below line of code for MAPSUI to get the map but it is not working. mapView.MyLocationLayer.UpdateMyLocation(new UI.Maui.Position(e.Latitude, e.Longitude));

Abhi
  • 85
  • 6
  • The package doesn't support the .net 6 for maui. So you can report this to the package developer on the github. – Liyun Zhang - MSFT Jun 27 '22 at 05:57
  • yes that i.e what i was thinking because i was not able to integrate with maui, lets hope for the maui map. – Abhi Jun 27 '22 at 09:00
  • 1
    I'm think that you might need the SphericalMercator.FromLonLat(location.Longitude, location.Latitude); function – gfmoore Jul 12 '22 at 15:49

1 Answers1

3

I can get the map, but not much else and I'm not sure that I'm really doing it right.

However,

In my xaml I have

//headers

...
<ContentView x:Name="mapView">

</ContentView>

In my code behind (as MVVM probably not working yet)

using Location = Microsoft.Maui.Devices.Sensors.Location;

namespace xxx

public partial class MainPage : ContentPage
{

  IGeolocation geolocation;

  public Location location;

  public MainPage(MainPageViewModel viewModel, IGeolocation geolocation)
    {
        InitializeComponent();
    BindingContext = viewModel;

    this.geolocation = geolocation;
    location = new();
    GetLocation();
  }

  //follow the example at https://www.youtube.com/watch?v=eRXiiy800XY&list=PLfbOp004UaYVt1En4WW3pVuM-vm66OqZe&index=9 for getting the location

  public async void GetLocation()
  {
    try
    {
      location = await geolocation.GetLocationAsync(new GeolocationRequest
      {
        DesiredAccuracy = GeolocationAccuracy.Best,
        Timeout = TimeSpan.FromSeconds(30)
      });

//now for mapsui
      var smc = SphericalMercator.FromLonLat(location.Longitude, location.Latitude);
      
      var mapControl = new Mapsui.UI.Maui.MapControl();
      var map = mapControl.Map;
      map?.Layers.Add(Mapsui.Tiling.OpenStreetMap.CreateTileLayer());
      
      map.Widgets.Add(new ZoomInOutWidget { MarginX = 10, MarginY = 20 });  //adds the +/- zoom widget

      map.Home = n => n.NavigateTo(new MPoint(smc.x, smc.y), map.Resolutions[16]);  //0 zoomed out-19 zoomed in


      //Add this to my xaml
      mapView.Content = mapControl;
      




    }
    catch (Exception e)
    {
      Debug.WriteLine($"GM: Can't query location: {e.Message}");
    }
  }

}

You might need to allow some imports of mapsui.xxxx

Now I note from the woeful documentation that there is a view that Xamarin forms used and should be usable in Maui

<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             
             xmlns:mapsui="clr-namespace:Mapsui.UI.Maui;assembly=Mapsui.UI.Maui"
             
             xmlns:local="clr-namespace:RouteIt.ViewModels"
             x:Class="RouteIt.MainPage">
...

  <mapsui:MapView x:Name="mapView" />

</ContentPage>

This shows the outline of a slightly different (and better) view, but I cannot find the magical incantation to make the map appear.

I hope that this might help.

G

EDIT and 30 seconds later in the next post I found

<mapsui:MapView x:Name="mapView" />

and in code

mapView.Map = map;

Obvious when you know how!!!!

Edit for location follow https://www.youtube.com/watch?v=eRXiiy800XY&list=PLfbOp004UaYVt1En4WW3pVuM-vm66OqZe&index=9

gfmoore
  • 976
  • 18
  • 31