Xamarin Forms implementing TK.CustomMaps
本文关键字:CustomMaps TK implementing Forms Xamarin | 更新日期: 2023-09-27 18:01:36
这可能是一个非常简单的修复,但我似乎找不到TK实现的完整示例。Xamarin Forms项目中的自定义地图。据我所知,下面的代码应该按预期工作,但是当调试使用项目的通用Windows元素时,我看不到自定义Pin,甚至删除imagesource(根据文档应该将Pin图像恢复为默认值)也不能解决问题。
同样值得注意的是,map、pin和drag事件不会触发。至少在调试UWP应用程序时,它们不会出现。
XAML:
<ContentPage.Content>
<Grid VerticalOptions="Fill" HorizontalOptions="Fill">
<Grid.RowDefinitions>
<RowDefinition Height="auto" />
<RowDefinition Height="*" />
<RowDefinition Height="auto" />
</Grid.RowDefinitions>
<Entry x:Name="Searchbar" Text="Town, City, Postcode ..." TextColor="Gray" Grid.Row="0" HorizontalOptions="FillAndExpand" VerticalOptions="Start" />
<z:TKCustomMap x:Name="mapView" Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" IsVisible="False" />
<maps:Map x:Name="mapViewNative" Grid.Row="1" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" MapType="Hybrid" IsVisible="False"/>
<Button x:Name="nextBtn" Grid.Row="2" Clicked="NextClicked" Text="Next" HorizontalOptions="End" VerticalOptions="End"/>
</Grid>
c# [SetPosition()在页面初始化时触发]
Plugin.Geolocator.Abstractions.Position location = null;
private ObservableCollection<TKCustomMapPin> _pins = new ObservableCollection<TKCustomMapPin>();
private async void SetPosition()
{
mapView.IsVisible = true;
if (CrossGeolocator.Current.IsGeolocationAvailable)
{
location = await CrossGeolocator.Current.GetPositionAsync();
mapView.SetBinding(TKCustomMap.CustomPinsProperty, "Pins");
mapView.SetBinding(TKCustomMap.MapClickedCommandProperty, "MapClickedCommand");
mapView.SetBinding(TKCustomMap.MapLongPressCommandProperty, "MapLongPressCommand");
mapView.SetBinding(TKCustomMap.MapCenterProperty, "MapCenter");
mapView.SetBinding(TKCustomMap.PinSelectedCommandProperty, "PinSelectedCommand");
mapView.SetBinding(TKCustomMap.SelectedPinProperty, "SelectedPin");
mapView.SetBinding(TKCustomMap.RoutesProperty, "Routes");
mapView.SetBinding(TKCustomMap.PinDragEndCommandProperty, "DragEndCommand");
mapView.SetBinding(TKCustomMap.CirclesProperty, "Circles");
mapView.SetBinding(TKCustomMap.CalloutClickedCommandProperty, "CalloutClickedCommand");
mapView.SetBinding(TKCustomMap.PolylinesProperty, "Lines");
mapView.SetBinding(TKCustomMap.PolygonsProperty, "Polygons");
mapView.SetBinding(TKCustomMap.MapRegionProperty, "MapRegion");
mapView.SetBinding(TKCustomMap.RouteClickedCommandProperty, "RouteClickedCommand");
mapView.SetBinding(TKCustomMap.RouteCalculationFinishedCommandProperty, "RouteCalculationFinishedCommand");
mapView.SetBinding(TKCustomMap.TilesUrlOptionsProperty, "TilesUrlOptions");
mapView.SetBinding(TKCustomMap.MapFunctionsProperty, "MapFunctions");
mapView.IsRegionChangeAnimated = true;
mapView.PinSelected += pinClicked;
mapView.MapClicked += mapClicked;
mapView.PinDragEnd += pinDragEnd;
//mapView.IsShowingUser = true;
mapView.CustomPins = _pins;
var initPin = new TKCustomMapPin()
{
IsDraggable = true,
Position = new Position(location.Latitude, location.Longitude),
IsVisible = true,
Title = "Concern Location",
Image = Device.OnPlatform("pinicon.png", "pinicon.png", "Assets/pinicon.png")
};
this._pins.Remove(initPin);
await Task.Delay(1000);
this._pins.Add(initPin);
mapView.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(initPin.Position.Latitude, initPin.Position.Longitude), Distance.FromMiles(0.3)));
}
}
'Initpin'正确地获取位置并且map将区域移动到正确的位置,但只是不想显示自定义pin。
对于任何到这里寻找解决方案的人来说,事实证明,使用IObservableCollection将自定义引脚传递给TKCustomMap是问题的原因。我使用了一个标准的TKCustomPin[]数组,它工作得很好。自从我发布了这条消息后,我就不再使用TKCustomMap插件,而是将工作合并到我们的代码库中,并继续实现缺失的功能,在需要的地方改进代码等。强烈建议其他人也这样做,它是一个坚实的基础,但最近几个月没有看到太多的发展。
我无法复制您的代码。
这是我的xaml
xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
xmlns:customMap="clr-namespace:TK.CustomMap;assembly=TK.CustomMap"
但是,我不知道如何集成拖放引脚功能