public static List <NSObject> toFeatureList(this IEnumerable <Pin> pins) { var features = new List <NSObject>(); foreach (var pin in pins) { // Skip if the pin is hidden if (!pin.isVisible) { continue; } var feature = new MGLPointFeature { Coordinate = new CLLocationCoordinate2D(pin.position.latitude, pin.position.longitude) }; object[] offset = { pin.iconOffset.X, pin.iconOffset.Y }; //[x,y] coordinates Positive values indicate right and down feature.Attributes = NSDictionary <NSString, NSObject> .FromObjectsAndKeys( new object[] { pin.image, pin.heading, pin.imageScaleFactor, NSArray.FromObjects(offset), pin.id }, new object[] { MapboxRenderer.pin_image_key, MapboxRenderer.pin_rotation_key, MapboxRenderer.pin_size_key, MapboxRenderer.pin_offset_key, MapboxRenderer.pin_id_key } ); features.Add(feature); } return(features); }
/// <summary> /// This has a problem, if the same pin updates a couple of times a second pin might jump suddenly to the /// 2nd to latest position then smooth animate to the latest position, but I think this is ok logically since /// By the time it's animating its final animation position is not really up to date. /// </summary> /// <param name="pin">Pin.</param> private void animateLocationChange(Pin pin) { #region Simultaneous pin update System.Threading.Tasks.Task.Run(async () => { // If there exist a pin that caused this update if (isPinAnimating) return; //// Wait for pin position assignment in the same call isPinAnimating = true; // Get all pins with the same key and same flat value (animatable pins) var pinsWithSimilarKey = xMap.pins.Where( (Pin arg) => arg.IsCenterAndFlat == pin.IsCenterAndFlat).ToArray(); Device.BeginInvokeOnMainThread(() => { var geoJsonSource = (MGLShapeSource)nStyle.SourceWithIdentifier(mapLockedPinsSourceKey); var visibleMovablePinCount = pinsWithSimilarKey.Count(p => p.isVisible); var currentHeadingCollection = new double[visibleMovablePinCount]; var features = new NSObject[visibleMovablePinCount]; // Update the entire frame MapBox.Extensions.MapExtensions.animatePin( (double d) => { System.Threading.Tasks.Task.Run(() => { for (int i = 0; i < visibleMovablePinCount; i++) { var p = pinsWithSimilarKey[i]; Position theCurrentAnimationJump = p.position; double theCurrentHeading = p.heading; // Only update the pin if it is the pin that cause this animation call // OR the if it actually requested for a position update before this current animation has not been finished if (pin == p || p.requestForUpdate) { theCurrentAnimationJump = SphericalUtil.interpolate(p.previousPinPosition, p.position, d); theCurrentHeading = SphericalUtil.computeHeading(p.previousPinPosition, p.position); } currentHeadingCollection[i] = theCurrentHeading; var feature = new MGLPointFeature { Coordinate = new CLLocationCoordinate2D(theCurrentAnimationJump.latitude, theCurrentAnimationJump.longitude) }; feature.Attributes = NSDictionary<NSString, NSObject>.FromObjectsAndKeys( new object[] { p.image, theCurrentHeading, p.imageScaleFactor, p.id }, new object[] { MapboxRenderer.pin_image_key, MapboxRenderer.pin_rotation_key, MapboxRenderer.pin_size_key, MapboxRenderer.pin_id_key }); // Add to the new animation frame features[i] = feature; } // Update the entire layer Device.BeginInvokeOnMainThread(() => geoJsonSource.Shape = MGLShapeCollectionFeature.ShapeCollectionWithShapes(features)); }); }, async (d, b) => { // DO NOT REMOVE, this is essential for the simultaneous pin location update to work await System.Threading.Tasks.Task.Delay(500); isPinAnimating = false; // Stabilize the pins, at this moment all the pins are updated for (int i = 0; i < visibleMovablePinCount; i++) { var p = pinsWithSimilarKey[i]; // To avoid triggering heading property change event p.PropertyChanged -= Pin_PropertyChanged; p.requestForUpdate = false; p.heading = currentHeadingCollection[i]; p.PropertyChanged += Pin_PropertyChanged; } }, 500); }); }); #endregion }