.net AMUI MAP no Pins in MAP

Jürgen Keitzl 61 Zuverlässigkeitspunkte
2024-01-24T19:35:18.8066667+00:00

Hello, I have the follow issue. I try to bind a list in ViewModel to XAML and will show five points in the map. In the ViewModel I defined a ObservableCollection filled with data from a Json by a service. No problem the Collection is filled with all data. Then I sort this collection and write the first five items in a list. Works too. After that I add some information in the list Pins. Works also. I have now five items in this list with Location, Address and Label. In the XAML I bind the Pins-List as ItemSource to the map. Normaly this should work but it doesn't. I don't see the pins in the map, and I can't find the problem. Can anyone give me an advice where the mistake is. Thank you very much. Juergen

ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              xmlns:maps="http://schemas.microsoft.com/dotnet/2021/maui/maps"              x:Class="LadeSaeulenFinder.View.MapsPage"              xmlns:viewmodel="clr-namespace:LadeSaeulenFinder.ViewModel"              
xmlns:ios="clr-namespace:Microsoft.Maui.Controls.PlatformConfiguration.iOSSpecific;assembly=Microsoft.Maui.Controls"
xmlns:sensors="clr-namespace:Microsoft.Maui.Devices.Sensors;assembly=Microsoft.Maui.Essentials"              ios:Page.UseSafeArea="True"
x:DataType="viewmodel:MapViewModel"
Title="{Binding Title}">
<Grid>
<maps:Map x:Name="map"
          IsShowingUser="True"
          IsZoomEnabled="True"
          IsScrollEnabled="True"
          ItemsSource="{Binding Pins}">
          <maps:Map.ItemTemplate>
            <DataTemplate x:DataType="maps:Pin">
               <maps:Pin Address="{Binding  Address}"
                         Label ="{Binding Label}"
                         Location ="{Binding Location}"
                         Type="Place"/>
            </DataTemplate>
           </maps:Map.ItemTemplate>
 </maps:Map>
 <ActivityIndicator IsVisible="{Binding IsBusy}"
                    IsRunning="{Binding IsBusy}"
                    HorizontalOptions="FillAndExpand"
                    VerticalOptions="CenterAndExpand"/>
  </Grid> 
</ContentPage>
public partial class MapViewModel : BaseViewModel
{  
public ObservableCollection<Position> Positions { get; } = new();
public List<Pin> Pins { get; set; } = new List<Pin>();
       PositionService positionService;
       IConnectivity connectivity;
       IGeolocation geolocation;
public MapViewModel(ChargerService chargerService, IConnectivity connectivity, IGeolocation geolocation)
{ Title = "Position Finder";
this.positiionService = positionService;
this.connectivity = connectivity;
this. Geolocation = geolocation;
OnAppearing();
}
protected async void OnAppearing()
{
if (IsBusy)
return;
try
{
if (connectivity.NetworkAccess != NetworkAccess.Internet)
{
await Shell.Current.DisplayAlert("Connection Error!", $"Check your Internet and try again!", "OK");
return;
}
var location = await geolocation.GetLastKnownLocationAsync();
if (location is null)
{
location = await geolocation.GetLocationAsync(
new GeolocationRequest
{
DesiredAccuracy = GeolocationAccuracy.High,
Timeout = TimeSpan.FromSeconds(10),
});
}
else
location = new Location(location.Latitude, location.Longitude);
IsBusy = true;
var positions = await positionService.GetPositions();
              
foreach (var position in positions)
{
Positions.Add(position);
}
var collection = Positions.OrderBy(c => location.CalculateDistance(new Location(c.Breitengrad, c.Laengengrad), DistanceUnits.Kilometers)).Take(5).ToList();
foreach (var c in collection)
{
Pins.Add(new Pin
{
Address = c.Strasse + ", " + c.Postleitzahl + " " + c.Ort,
Label = " c.Deatail,
Location = new Location(c.Breitengrad, c.Laengengrad)
});
}       
}
catch (Exception ex)
{
Debug.WriteLine(ex);
await Shell.Current.DisplayAlert("Error!", $"Unable to get Position: {ex.Message}", "OK");
}
finally
{
IsBusy = false;
}
}
}
}
public partial class MapsPage : ContentPage
{
    public MapsPage(MapViewModel viewModel)
    {
        InitializeComponent();
        BindingContext = viewModel;
       
    }

    protected override async void OnAppearing()
    {  
        base.OnAppearing();
        var geolocationRequest = new GeolocationRequest(GeolocationAccuracy.High, TimeSpan.FromSeconds(10));
        var location = await Geolocation.GetLocationAsync(geolocationRequest);
        var location1 =  new Location(xxxxxxx, xxxxxx);
        map.MoveToRegion(MapSpan.FromCenterAndRadius(location1, Distance.FromKilometers(200)));
                
    }

}
.NET
.NET
Microsoft-Technologien, die auf dem .NET-Softwareframework basieren.
29 Fragen
0 Kommentare Keine Kommentare
{count} Stimmen

1 Antwort

Sortieren nach: Am hilfreichsten
  1. Thomas Wycichowski TWyTec 1,040 Zuverlässigkeitspunkte
    2024-01-24T22:29:52.55+00:00
    Eine Person fand diese Antwort hilfreich.
    0 Kommentare Keine Kommentare

Ihre Antwort

Fragesteller*innen können Antworten als akzeptierte Antworten markiert werden, wodurch Benutzer*innen wissen, dass diese Antwort das Problem gelöst hat.