public override MKAnnotationView GetViewForAnnotation(MKMapView mapView, IMKAnnotation annotation)
            {
                MKAnnotationView anView;

                Int32 annotationType =
                    Convert.ToInt32(annotation is MKUserLocation) * 1 +
                    Convert.ToInt32(annotation is CustomAnnotation) * 2;

                switch (annotationType)
                {
                case 1:
                {
                    return(null);
                }

                case 2:
                {
                    CustomAnnotation customAnnotation = annotation as CustomAnnotation;

                    anView = new MKAnnotationView(annotation, cId);

                    UILabel DocCountLabel = new UILabel();

                    DocCountLabel.Frame           = new CGRect(-25, -25, 50, 50);
                    DocCountLabel.BackgroundColor = UIColor.Red;
                    DocCountLabel.TextColor       = UIColor.White;
                    DocCountLabel.TextAlignment   = UITextAlignment.Center;
                    DocCountLabel.Font            = UIFont.BoldSystemFontOfSize(25f);
                    if (null == customAnnotation.Annotations)
                    {
                        DocCountLabel.Text = customAnnotation.Count.ToString();
                    }
                    else
                    {
                        DocCountLabel.Text = customAnnotation.Annotations.Count.ToString();
                    }
                    DocCountLabel.Layer.MasksToBounds = true;
                    DocCountLabel.Layer.CornerRadius  = 8;
                    //DocCountLabel.Alpha = 0.5f;

                    anView.AddSubview(DocCountLabel);

                    anView.CanShowCallout = true;

                    break;
                }

                default:
                {
                    anView = (MKPinAnnotationView)mapView.DequeueReusableAnnotation(pId);

                    if (anView == null)
                    {
                        anView = new MKPinAnnotationView(annotation, pId);
                    }

                    ((MKPinAnnotationView)anView).PinColor = MKPinAnnotationColor.Red;
                    anView.CanShowCallout = true;
                    break;
                }
                }

                return(anView);
            }
Exemple #2
0
        public List <IMKAnnotation> ClusteredAnnotationsWithinMapRect(MKMapRect rect, double zoomScale, Dictionary <IMKAnnotation, bool> filter)
        {
            double cellSize = CellSizeForZoomScale(zoomScale);

            if (RespondsToSelector != null)
            {
                cellSize *= RespondsToSelector(this);
            }
            double scaleFactor = zoomScale / cellSize;

            int minX = (int)Math.Floor(rect.MinX * scaleFactor);
            int maxX = (int)Math.Floor(rect.MaxX * scaleFactor);
            int minY = (int)Math.Floor(rect.MinY * scaleFactor);
            int maxY = (int)Math.Floor(rect.MaxY * scaleFactor);

            List <IMKAnnotation> clusteredAnnotations = new List <IMKAnnotation>();

            lock (this)
            {
                for (int x = minX; x <= maxX; x++)
                {
                    for (int y = minY; y <= maxY; y++)
                    {
                        MKMapRect   mapRect = new MKMapRect(x / scaleFactor, y / scaleFactor, 1.0 / scaleFactor, 1.0 / scaleFactor);
                        BoundingBox mapBox  = Utils.BoundingBoxForMapRect(mapRect);

                        double totalLatitude  = 0;
                        double totalLongitude = 0;

                        List <IMKAnnotation> annotations = new List <IMKAnnotation>();

                        _tree.EnumerateAnnotationsInBox(mapBox, delegate(IMKAnnotation annotation)
                        {
                            if (filter == null || filter[annotation])
                            {
                                totalLatitude  += annotation.Coordinate.Latitude;
                                totalLongitude += annotation.Coordinate.Longitude;
                                annotations.Add(annotation);
                            }
                        });

                        int count = annotations.Count;
                        if (count == 1)
                        {
                            clusteredAnnotations.AddRange(annotations);
                        }
                        if (count > 1)
                        {
                            CLLocationCoordinate2D coordinate = new CLLocationCoordinate2D(totalLatitude / count, totalLongitude / count);
                            CustomAnnotation       cluster    = new CustomAnnotation()
                            {
                                Location = coordinate
                            };
                            cluster.Annotations = annotations;
                            clusteredAnnotations.Add(cluster);
                        }
                    }
                }
            }

            return(clusteredAnnotations);
        }