正如我已经尝试了下面的代码为MAPSUI获得映射,但它是无效的。新mapView.MyLocationLayer.UpdateMyLocation(e.Latitude,e.Longitude);
发布于 2022-07-12 14:47:12
我可以得到地图,但没有其他,我不确定我是否真的做得对。
然而,
在我的xaml中
//headers
...
<ContentView x:Name="mapView">
</ContentView>在我后面的代码中(因为MVVM可能还没有工作)
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}");
}
}
}您可能需要允许导入一些mapsui.xxxx
现在,我从悲惨的文档中注意到,有一种观点认为,Xamarin表单使用并应该在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>这显示了一个稍微不同的(更好的)视图的轮廓,但我找不到神奇的咒语,使地图出现。
我希望这能帮上忙。
G
编辑,30秒后,在我发现的下一篇文章中
<mapsui:MapView x:Name="mapView" />在代码中
mapView.Map = map;当你知道怎么做的时候很明显!
按照https://www.youtube.com/watch?v=eRXiiy800XY&list=PLfbOp004UaYVt1En4WW3pVuM-vm66OqZe&index=9编辑位置
https://stackoverflow.com/questions/72739111
复制相似问题