Map renderers listen for Pin property changes and update native pin controls. (#2685) fixes #2121

This commit is contained in:
jcmanke 2018-05-15 06:27:29 -05:00 коммит произвёл Rui Marinho
Родитель a7737337a0
Коммит 9791291284
3 изменённых файлов: 94 добавлений и 3 удалений

Просмотреть файл

@ -91,7 +91,19 @@ namespace Xamarin.Forms.Controls
map.Pins.Add (new Pin { Position = pos, Label = "Rui" });
map.MoveToRegion (MapSpan.FromCenterAndRadius (pos, Distance.FromMiles (0.5)));
};
var buttonEditPin = new Button { Text = "Edit Pin" };
buttonEditPin.Clicked += (a, e) =>
{
var pin = map.Pins.First();
pin.Label += " Edited";
pin.Address = "Edited";
var pos = new Position(pin.Position.Latitude + 1, pin.Position.Longitude + 1);
pin.Position = pos;
map.MoveToRegion(MapSpan.FromCenterAndRadius(pos, Distance.FromMiles(0.5)));
};
map.VerticalOptions = LayoutOptions.FillAndExpand;
_stack.Children.Add (searchAddress);
@ -102,6 +114,7 @@ namespace Xamarin.Forms.Controls
_stack.Children.Add (buttonAddressFromPosition);
_stack.Children.Add (buttonHome);
_stack.Children.Add (buttonZoomPin);
_stack.Children.Add(buttonEditPin);
Content = _stack;
}

Просмотреть файл

@ -73,6 +73,11 @@ namespace Xamarin.Forms.Maps.Android
{
MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
((ObservableCollection<Pin>)Element.Pins).CollectionChanged -= OnCollectionChanged;
foreach (Pin pin in Element.Pins)
{
pin.PropertyChanged -= PinOnPropertyChanged;
}
}
if (NativeMap != null)
@ -106,6 +111,11 @@ namespace Xamarin.Forms.Maps.Android
Map oldMapModel = e.OldElement;
((ObservableCollection<Pin>)oldMapModel.Pins).CollectionChanged -= OnCollectionChanged;
foreach (Pin pin in oldMapModel.Pins)
{
pin.PropertyChanged -= PinOnPropertyChanged;
}
MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
if (NativeMap != null)
@ -229,12 +239,38 @@ namespace Xamarin.Forms.Maps.Android
var opts = CreateMarker(pin);
var marker = map.AddMarker(opts);
pin.PropertyChanged += PinOnPropertyChanged;
// associate pin with marker for later lookup in event handlers
pin.Id = marker.Id;
return marker;
}));
}
void PinOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Pin pin = (Pin)sender;
Marker marker = _markers.FirstOrDefault(m => m.Id == (string)pin.Id);
if (marker == null)
{
return;
}
if (e.PropertyName == Pin.LabelProperty.PropertyName)
{
marker.Title = pin.Label;
}
else if (e.PropertyName == Pin.AddressProperty.PropertyName)
{
marker.Snippet = pin.Address;
}
else if (e.PropertyName == Pin.PositionProperty.PropertyName)
{
marker.Position = new LatLng(pin.Position.Latitude, pin.Position.Longitude);
}
}
void MapOnMarkerClick(object sender, GoogleMap.InfoWindowClickEventArgs eventArgs)
{
// clicked marker
@ -336,6 +372,8 @@ namespace Xamarin.Forms.Maps.Android
foreach (Pin p in pins)
{
p.PropertyChanged -= PinOnPropertyChanged;
var marker = _markers.FirstOrDefault(m => (object)m.Id == p.Id);
if (marker == null)
{

Просмотреть файл

@ -59,6 +59,11 @@ namespace Xamarin.Forms.Maps.MacOS
var mapModel = (Map)Element;
MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged -= OnCollectionChanged;
foreach (Pin pin in mapModel.Pins)
{
pin.PropertyChanged -= PinOnPropertyChanged;
}
}
var mkMapView = (MKMapView)Control;
@ -101,6 +106,11 @@ namespace Xamarin.Forms.Maps.MacOS
var mapModel = (Map)e.OldElement;
MessagingCenter.Unsubscribe<Map, MapSpan>(this, MoveMessageName);
((ObservableCollection<Pin>)mapModel.Pins).CollectionChanged -= OnCollectionChanged;
foreach (Pin pin in mapModel.Pins)
{
pin.PropertyChanged -= PinOnPropertyChanged;
}
}
if (e.NewElement != null)
@ -283,12 +293,39 @@ namespace Xamarin.Forms.Maps.MacOS
{
foreach (Pin pin in pins)
{
pin.PropertyChanged += PinOnPropertyChanged;
var annotation = CreateAnnotation(pin);
pin.Id = annotation;
((MKMapView)Control).AddAnnotation(annotation);
}
}
void PinOnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
Pin pin = (Pin)sender;
var annotation = pin.Id as MKPointAnnotation;
if (annotation == null)
{
return;
}
if (e.PropertyName == Pin.LabelProperty.PropertyName)
{
annotation.Title = pin.Label;
}
else if (e.PropertyName == Pin.AddressProperty.PropertyName)
{
annotation.Subtitle = pin.Address;
}
else if (e.PropertyName == Pin.PositionProperty.PropertyName)
{
annotation.Coordinate = new CLLocationCoordinate2D(pin.Position.Latitude, pin.Position.Longitude);
}
}
void MkMapViewOnRegionChanged(object sender, MKMapViewChangeEventArgs e)
{
if (Element == null)
@ -335,8 +372,11 @@ namespace Xamarin.Forms.Maps.MacOS
void RemovePins(IList pins)
{
foreach (object pin in pins)
((MKMapView)Control).RemoveAnnotation((IMKAnnotation)((Pin)pin).Id);
foreach (Pin pin in pins)
{
pin.PropertyChanged -= PinOnPropertyChanged;
((MKMapView)Control).RemoveAnnotation((IMKAnnotation)pin.Id);
}
}
void UpdateHasScrollEnabled()