当我打开谷歌地图时,我想在地图中看到多个图钉。 我使用此链接作为帮助https://xamarinforms.wordpress.com/2015/07/07/adding-a-bindable-map-with-the-map-behavior/
但我在MapBehaviour.cs.中遇到错误。我的代码就是。
BindableProperty.Create>( p => p.ItemsSource,null,BindingMode.Default,null,ItemsSourceChanged); //我在这个页面中收到错误
public class MapBehavior: BindableBehavior<Map>
{
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create<MapBehavior, IEnumerable<ILocationViewModel>>(
p => p.ItemsSource, null, BindingMode.Default, null, ItemsSourceChanged);
public System.Collections.Generic.IEnumerable<ILocationViewModel> ItemsSource
{
get { return (IEnumerable<ILocationViewModel>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
private static void ItemsSourceChanged(BindableObject bindable, IEnumerable oldValue, IEnumerable newValue)
{
var behavior = bindable as MapBehavior;
if (behavior == null) return;
behavior.AddPins();
}
private void AddPins()
{
var map = AssociatedObject;
for (int i = map.Pins.Count - 1; i >= 0; i--)
{
map.Pins[i].Clicked -= PinOnClicked;
map.Pins.RemoveAt(i);
}
var pins = ItemsSource.Select(x =>
{
var pin = new Pin
{
Type = PinType.SearchResult,
Position = new Position(x.Latitude, x.Longitude),
Label = x.Title,
Address = x.Description,
};
pin.Clicked += PinOnClicked;
return pin;
}).ToArray();
foreach (var pin in pins)
map.Pins.Add(pin);
PositionMap();
}
private void PinOnClicked(object sender, EventArgs eventArgs)
{
var pin = sender as Pin;
if (pin == null) return;
var viewModel = ItemsSource.FirstOrDefault(x => x.Title == pin.Label);
if (viewModel == null) return;
viewModel.Command.Execute(null);
}
private void PositionMap()
{
if (ItemsSource == null || !ItemsSource.Any()) return;
var centerPosition = new Position(ItemsSource.Average(x => x.Latitude), ItemsSource.Average(x => x.Longitude));
var minLongitude = ItemsSource.Min(x => x.Longitude);
var minLatitude = ItemsSource.Min(x => x.Latitude);
var maxLongitude = ItemsSource.Max(x => x.Longitude);
var maxLatitude = ItemsSource.Max(x => x.Latitude);
var distance = MapHelper.CalculateDistance(minLatitude, minLongitude,
maxLatitude, maxLongitude, 'M') / 2;
AssociatedObject.MoveToRegion(MapSpan.FromCenterAndRadius(centerPosition, Distance.FromMiles(distance)));
Device.StartTimer(TimeSpan.FromMilliseconds(500), () =>
{
AssociatedObject.MoveToRegion(MapSpan.FromCenterAndRadius(centerPosition, Distance.FromMiles(distance)));
return false;
});
}
}