Beispiel #1
0
        private Sdk.Annotations.Annotation AddAnnotation(Annotation at)
        {
            Sdk.Annotations.Annotation options = null;
            if (at is PointAnnotation)
            {
                var marker = new MarkerOptions();
                marker.SetTitle(at.Title);
                marker.SetSnippet(at.Title);
                marker.SetPosition(((PointAnnotation)at).Coordinate.ToLatLng());
                options = map.AddMarker(marker);
            }
            else if (at is PolylineAnnotation)
            {
                var polyline = at as PolylineAnnotation;
                if (polyline.Coordinates?.Count() == 0)
                {
                    return(null);
                }
                var notifyCollection = polyline.Coordinates as INotifyCollectionChanged;
                if (notifyCollection != null)
                {
                    notifyCollection.CollectionChanged += (s, e) =>
                    {
                        if (e.Action == NotifyCollectionChangedAction.Add)
                        {
                            if (_annotationDictionaries.ContainsKey(at.Id))
                            {
                                var poly = _annotationDictionaries[at.Id] as Polyline;
                                poly.AddPoint(polyline.Coordinates.ElementAt(e.NewStartingIndex).ToLatLng());
                            }
                            else
                            {
                                var coords = new ArrayList();
                                for (var i = 0; i < polyline.Coordinates.Count(); i++)
                                {
                                    coords.Add(polyline.Coordinates.ElementAt(i).ToLatLng());
                                }
                                var polylineOpt = new PolylineOptions();
                                polylineOpt.Polyline.Width = Context.ToPixels(1);
                                polylineOpt.Polyline.Color = Android.Graphics.Color.Blue;
                                polylineOpt.AddAll(coords);
                                options = map.AddPolyline(polylineOpt);
                                _annotationDictionaries.Add(at.Id, options);
                            }
                        }
                        else if (e.Action == NotifyCollectionChangedAction.Remove)
                        {
                            if (_annotationDictionaries.ContainsKey(at.Id))
                            {
                                var poly = _annotationDictionaries[at.Id] as Polyline;
                                poly.Points.Remove(polyline.Coordinates.ElementAt(e.OldStartingIndex).ToLatLng());
                            }
                        }
                    };
                }
            }
            else if (at is MultiPolylineAnnotation)
            {
                var polyline = at as MultiPolylineAnnotation;
                if (polyline.Coordinates == null || polyline.Coordinates.Length == 0)
                {
                    return(null);
                }

                var lines = new List <PolylineOptions>();
                for (var i = 0; i < polyline.Coordinates.Length; i++)
                {
                    if (polyline.Coordinates[i].Length == 0)
                    {
                        continue;
                    }
                    var coords = new PolylineOptions();
                    for (var j = 0; j < polyline.Coordinates[i].Length; j++)
                    {
                        coords.Add(new LatLng(polyline.Coordinates[i][j].Lat, polyline.Coordinates[i][j].Long));
                    }
                    lines.Add(coords);
                }
                map.AddPolylines(lines);
            }
            if (options != null)
            {
                if (at.Id != null)
                {
                    _annotationDictionaries.Add(at.Id, options);
                }
            }

            return(options);
        }
        MGLShape ShapeFromAnnotation(FormsMB.Annotation annotation)
        {
            MGLShape shape = null;

            if (annotation is PointAnnotation)
            {
                shape = new MGLPointAnnotation()
                {
                    Coordinate = new CLLocationCoordinate2D(((PointAnnotation)annotation).Coordinate.Lat,
                                                            ((PointAnnotation)annotation).Coordinate.Long),
                };
            }
            else if (annotation is PolylineAnnotation)
            {
                var polyline = annotation as PolylineAnnotation;
                shape = PolyLineWithCoordinates(polyline.Coordinates.ToArray());
                var notifiyCollection = polyline.Coordinates as INotifyCollectionChanged;
                if (notifiyCollection != null)
                {
                    notifiyCollection.CollectionChanged += (sender, e) => {
                        //TODO Move to a separated method
                        if (e.Action == NotifyCollectionChangedAction.Add)
                        {
                            foreach (Position pos in e.NewItems)
                            {
                                var coord = TypeConverter.FromPositionToCoordinate(pos);
                                (shape as MGLPolyline).AppendCoordinates(ref coord, 1);
                            }
                        }
                        else if (e.Action == NotifyCollectionChangedAction.Remove)
                        {
                            (shape as MGLPolyline).RemoveCoordinatesInRange(new NSRange(e.OldStartingIndex, e.OldItems.Count));
                        }
                    };
                }
            }
            else if (annotation is MultiPolylineAnnotation)
            {
                var polyline = annotation as MultiPolylineAnnotation;
                if (polyline.Coordinates == null || polyline.Coordinates.Length == 0)
                {
                    return(null);
                }
                var lines = new MGLPolyline [polyline.Coordinates.Length];
                for (var i = 0; i < polyline.Coordinates.Length; i++)
                {
                    if (polyline.Coordinates [i].Length == 0)
                    {
                        continue;
                    }
                    lines [i] = PolyLineWithCoordinates(polyline.Coordinates[i]);
                }
                shape = MGLMultiPolyline.MultiPolylineWithPolylines(lines);
            }
            if (shape != null)
            {
                if (annotation.Title != null)
                {
                    shape.Title = annotation.Title;
                }
                if (annotation.SubTitle != null)
                {
                    shape.Subtitle = annotation.SubTitle;
                }
                if (!string.IsNullOrEmpty(annotation.Id))
                {
                    shape.SetId(annotation.Id);
                }
            }

            return(shape);
        }