gpt4 book ai didi

c# - Xamarin Forms 和 MVVM 架构中的 map

转载 作者:行者123 更新时间:2023-11-30 23:14:12 24 4
gpt4 key购买 nike

如何在 Xamarin Forms 和 MVVM 架构中显示 map 。如何在 ModelView 中的 map 上创建一个位置并在 PageView 中显示?

查看

<?xml version="1.0" encoding="UTF-8"?>
<d:ExtendedContentPage xmlns:d="clr-namespace:TestApp;assembly=TestApp"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.MapPageView">
<ContentPage.Content>
<StackLayout VerticalOptions="StartAndExpand" Padding="30">
<maps:Map WidthRequest="320" HeightRequest="200"
Text="{Binding MapModel.MyMap]"
IsShowingUser="true"
MapType="Hybrid"/>
</StackLayout>
</ContentPage.Content>
</d:ExtendedContentPage>

模型 View

public class MapViewModel: BaseViewModel
{
private MapModel _mapModel = null;
private MapModel MapModel
{
get
{
return _mapModel;
}
set
{
if (_mapModel != value)
{
_mapModel = value;
OnPropertyChanged();
}
}
}

public MapViewModel(INavigation navigation) : base(navigation)
{
InitializeMapsPosition();
}

private void InitializeMapsPosition()
{

}

public override void RemoveHandlers()
{
throw new NotImplementedException();
}
}

模型

public class MapModel
{
public string MyMap { set; get; }

public MapModel(String myMap)
{
MyMap = myMap;
}
}

最佳答案

在我的场景中,我需要在 map 中显示某些地方的位置和当前位置。我使用 Xamarin.Forms.Maps 来显示 map 对于 map 中的位置:我添加了一个 CustomMap 类并编写了两个属性注入(inject)以在 map 中显示图钉。一个属性 (CenterRegion) 用于显示当前位置,另一个属性 (CustomPins) 用于显示 map 中的地点位置。

自定义 map 类

  public class CustomMap : Map
{
private static IList<Pin> AllPins;

public Location CenterRegion
{
get { return (Location)GetValue(CenterRegionProperty); }
set { SetValue(CenterRegionProperty, value); }
}


public static readonly BindableProperty CenterRegionProperty =
BindableProperty.Create(propertyName: nameof(CenterRegion), returnType:
typeof(Location), declaringType: typeof(CustomMap), defaultValue: null,
propertyChanged: (sender, oldValue, newValue) =>
{
CustomMap map = (CustomMap)sender;

if (newValue is Location location)
{
map.MoveToRegion(MapSpan.FromCenterAndRadius(new
Position(location.Latitude, location.Longitude),
Distance.FromMiles(3)));
}
});


public IEnumerable CustomPins
{
get { return (IEnumerable)GetValue(CustomPinsProperty); }
set { SetValue(CustomPinsProperty, value); }
}

public static readonly BindableProperty CustomPinsProperty =
BindableProperty.Create(propertyName: nameof(CustomPins), returnType:
typeof(IEnumerable), declaringType: typeof(CustomMap), defaultValue: null,
propertyChanged: (sender, oldValue, newValue) =>
{
CustomMap map = (CustomMap)sender;

AllPins = new List<Pin>();

map.Pins.Clear();

if (newValue is IEnumerable spaces)
{
foreach (Pin pin in ConvertSpacesToPins(spaces))
map.Pins.Add(pin);
}
AllPins = map.Pins;
map.OnPropertyChanged("Pins");
});


public static List<Pin> ConvertSpacesToPins(IEnumerable spaces)
{
if (spaces == null)
return null;

List<Pin> result = new List<Pin>();

foreach (SpaceDto space in spaces.OfType<SpaceDto>())
{
double latitude = space.Latitude;
double longitude = space.Longitude;
string spaceTitle = space.Title;
Position position = new Position(latitude, longitude);

Pin pin = new Pin
{
Position = position,
Label = spaceTitle
};
result.Add(pin);
}
return result;
}
}

Xaml 页面

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:Map="clr-namespace:XamarinFormsPrismMapSample.Views"
x:Class="XamarinFormsPrismMapSample.Views.CustomMapSampleView">
<Map:CustomMap
CenterRegion="{Binding CurrentLocation}"
CustomPins="{Binding Spaces}"
HorizontalOptions="FillAndExpand"
IsShowingUser="true"
MapType="Street"
VerticalOptions="FillAndExpand" />
</ContentPage>

View 模型:

public class CustomMapSampleViewModel : BindableBase
{
public Location CurrentLocation { get; set; }
public SpaceDto Spaces { get; set; }

public CustomMapSampleViewModel()
{

}

public async Task OnNavigatedToAsync(NavigationParameters parameters)
{
Location CurrentLocation = await Geolocation.GetLastKnownLocationAsync();
List<SpaceDto> Spaces = new List<SpaceDto> {
new SpaceDto { Latitude = 45.6892 , Longitude = 51.3890, Title = "X" },
new SpaceDto { Latitude = 45.6891, Longitude = 51.3890, Title = "Y" },
new SpaceDto { Latitude = 45.6888, Longitude = 51.3890, Title = "Z" } };
}
}

public class SpaceDto
{
public string Title { get; set; }
public virtual double Latitude { get; set; }
public virtual double Longitude { get; set; }
}

关于c# - Xamarin Forms 和 MVVM 架构中的 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43256629/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com