Inheritance: MKAnnotationView
コード例 #1
0
        public void BuildView()
        {
            if (mapView == null)
            {

                mapView = new MKMapView();
                RectangleF frame = new RectangleF(0,0,320,367);

                mapView.Frame = frame;

                DV = new DistanceView();

                frame = DV.View.Frame;
                //frame.Y = mapView.Frame.Bottom;
                frame.Y = -DV.View.Frame.Height;

                DV.View.Frame = frame;

                DV.TouchUpInside += delegate(object sender, EventArgs e) {

                    RemoveRouteAnnotation();

                    HideDistanceView();
                };

                mapView.RegionWillChange += delegate(object sender, MKMapViewChangeEventArgs e) {
                    if (routeView != null)
                    {
                        routeView.Hidden = true;
                    }
                };

                mapView.RegionChanged += delegate(object sender, MKMapViewChangeEventArgs e) {
                    if (routeView != null)
                    {
                        routeView.Hidden = false;
                        routeView.RegionChanged();
                    }
                };

                mapView.GetViewForAnnotation = delegate(MKMapView mapViewForAnnotation, NSObject annotation) {
                    if (annotation is MKUserLocation) return null;

                    if (annotation is CycleAnnotation)
                    {
                        var mapAnnotation = annotation as CycleAnnotation;
                        if (mapAnnotation == null) return null;

                        MKPinAnnotationView pinView = (MKPinAnnotationView)mapViewForAnnotation.DequeueReusableAnnotation(MapPin);
                        if (pinView == null)
                        {
                            pinView = new MKPinAnnotationView(mapAnnotation, MapPin);
                        } else {
                            pinView.Annotation = annotation;
                        }

                        int valueToCheck = 0;
                        if (CurrentDisplayMode == DisplayMode.Bikes)
                        {
                            valueToCheck = mapAnnotation.Bike.BikesAvailable;
                        } else {
                            valueToCheck = mapAnnotation.Bike.DocksAvailable;
                        }

                        if ((valueToCheck < 5 && valueToCheck != -1)) {
                            if (valueToCheck == 0)
                            {
                                pinView.PinColor = MKPinAnnotationColor.Red;
                            } else {
                                pinView.PinColor = MKPinAnnotationColor.Purple;
                            }
                        } else {
                            pinView.PinColor = MKPinAnnotationColor.Green;
                        }

                        mapAnnotation.PinView = pinView;

                        pinView.CanShowCallout = true;
                        return pinView;
                    }

                    if (annotation is CSRouteAnnotation)
                    {
                        var routeAnnotation = annotation as CSRouteAnnotation;
                        MKAnnotationView annotationView = null;

                        if (annotationView == null)
                        {
                            routeView = new CSRouteView(new RectangleF (0,0, mapView.Frame.Size.Width, mapView.Frame.Size.Height));
                            routeView.Annotation = routeAnnotation;
                            routeView.MapView = mapViewForAnnotation;
                            annotationView = routeView;
                        }

                        return annotationView;
                    }

                    return null;

                };

                List<MKAnnotation> locations = new List<MKAnnotation>();

                double minLon = 200, minLat = 200, maxLon = -200, maxLat = -200;

                foreach(var bike in BikeLocation.AllBikes)
                {
                    if (bike.Location.Longitude < minLon) minLon = bike.Location.Longitude;
                    if (bike.Location.Latitude < minLat) minLat = bike.Location.Latitude;

                    if (bike.Location.Longitude < maxLon) maxLon = bike.Location.Longitude;
                    if (bike.Location.Latitude > maxLat) maxLat = bike.Location.Latitude;

                    locations.Add(new CycleAnnotation(bike));
                }

                if (locations.Count > 0)
                {
                    mapView.AddAnnotation(locations.ToArray());

                    var tl = new CLLocationCoordinate2D(-90, 180);
                    var br = new CLLocationCoordinate2D(90, -180);

                    foreach(MKAnnotation an in mapView.Annotations)
                    {
                        tl.Longitude = Math.Min(tl.Longitude, an.Coordinate.Longitude);
                        tl.Latitude = Math.Max(tl.Latitude, an.Coordinate.Latitude);

                        br.Longitude = Math.Max(br.Longitude, an.Coordinate.Longitude);
                        br.Latitude = Math.Min(br.Latitude, an.Coordinate.Latitude);

                    }

                    var center = new CLLocationCoordinate2D {
                        Latitude = tl.Latitude - (tl.Latitude - br.Latitude) *0.5,
                        Longitude = tl.Longitude - (tl.Longitude - br.Longitude) *0.5
                    };

                    var span = new MKCoordinateSpan
                    {
                        LatitudeDelta = Math.Abs(tl.Latitude - br.Latitude) *0.5,
                        LongitudeDelta = Math.Abs(tl.Longitude - br.Longitude) *0.5

                    };

                    MKCoordinateRegion region = new MKCoordinateRegion (center, span );

                    region = mapView.RegionThatFits(region);

                    mapView.SetRegion(region, true);
                }

                mapView.ShowsUserLocation = true;

                View.AddSubview(mapView);
            }
        }
コード例 #2
0
        public void BuildView()
        {
            if (mapView == null)
            {
                mapView = new MKMapView();
                RectangleF frame = new RectangleF(0, 0, 320, 367);

                mapView.Frame = frame;



                DV = new DistanceView();

                frame = DV.View.Frame;
                //frame.Y = mapView.Frame.Bottom;
                frame.Y = -DV.View.Frame.Height;


                DV.View.Frame = frame;

                DV.TouchUpInside += delegate(object sender, EventArgs e) {
                    RemoveRouteAnnotation();

                    HideDistanceView();
                };

                mapView.RegionWillChange += delegate(object sender, MKMapViewChangeEventArgs e) {
                    if (routeView != null)
                    {
                        routeView.Hidden = true;
                    }
                };

                mapView.RegionChanged += delegate(object sender, MKMapViewChangeEventArgs e) {
                    if (routeView != null)
                    {
                        routeView.Hidden = false;
                        routeView.RegionChanged();
                    }
                };

                mapView.GetViewForAnnotation = delegate(MKMapView mapViewForAnnotation, NSObject annotation) {
                    if (annotation is MKUserLocation)
                    {
                        return(null);
                    }

                    if (annotation is CycleAnnotation)
                    {
                        var mapAnnotation = annotation as CycleAnnotation;
                        if (mapAnnotation == null)
                        {
                            return(null);
                        }


                        MKPinAnnotationView pinView = (MKPinAnnotationView)mapViewForAnnotation.DequeueReusableAnnotation(MapPin);
                        if (pinView == null)
                        {
                            pinView = new MKPinAnnotationView(mapAnnotation, MapPin);
                        }
                        else
                        {
                            pinView.Annotation = annotation;
                        }



                        int valueToCheck = 0;
                        if (CurrentDisplayMode == DisplayMode.Bikes)
                        {
                            valueToCheck = mapAnnotation.Bike.BikesAvailable;
                        }
                        else
                        {
                            valueToCheck = mapAnnotation.Bike.DocksAvailable;
                        }

                        if ((valueToCheck < 5 && valueToCheck != -1))
                        {
                            if (valueToCheck == 0)
                            {
                                pinView.PinColor = MKPinAnnotationColor.Red;
                            }
                            else
                            {
                                pinView.PinColor = MKPinAnnotationColor.Purple;
                            }
                        }
                        else
                        {
                            pinView.PinColor = MKPinAnnotationColor.Green;
                        }

                        mapAnnotation.PinView = pinView;


                        pinView.CanShowCallout = true;
                        return(pinView);
                    }

                    if (annotation is CSRouteAnnotation)
                    {
                        var routeAnnotation             = annotation as CSRouteAnnotation;
                        MKAnnotationView annotationView = null;


                        if (annotationView == null)
                        {
                            routeView            = new CSRouteView(new RectangleF(0, 0, mapView.Frame.Size.Width, mapView.Frame.Size.Height));
                            routeView.Annotation = routeAnnotation;
                            routeView.MapView    = mapViewForAnnotation;
                            annotationView       = routeView;
                        }

                        return(annotationView);
                    }

                    return(null);
                };



                List <MKAnnotation> locations = new List <MKAnnotation>();

                double minLon = 200, minLat = 200, maxLon = -200, maxLat = -200;


                foreach (var bike in BikeLocation.AllBikes)
                {
                    if (bike.Location.Longitude < minLon)
                    {
                        minLon = bike.Location.Longitude;
                    }
                    if (bike.Location.Latitude < minLat)
                    {
                        minLat = bike.Location.Latitude;
                    }

                    if (bike.Location.Longitude < maxLon)
                    {
                        maxLon = bike.Location.Longitude;
                    }
                    if (bike.Location.Latitude > maxLat)
                    {
                        maxLat = bike.Location.Latitude;
                    }

                    locations.Add(new CycleAnnotation(bike));
                }



                if (locations.Count > 0)
                {
                    mapView.AddAnnotation(locations.ToArray());



                    var tl = new CLLocationCoordinate2D(-90, 180);
                    var br = new CLLocationCoordinate2D(90, -180);

                    foreach (MKAnnotation an in mapView.Annotations)
                    {
                        tl.Longitude = Math.Min(tl.Longitude, an.Coordinate.Longitude);
                        tl.Latitude  = Math.Max(tl.Latitude, an.Coordinate.Latitude);

                        br.Longitude = Math.Max(br.Longitude, an.Coordinate.Longitude);
                        br.Latitude  = Math.Min(br.Latitude, an.Coordinate.Latitude);
                    }

                    var center = new CLLocationCoordinate2D {
                        Latitude  = tl.Latitude - (tl.Latitude - br.Latitude) * 0.5,
                        Longitude = tl.Longitude - (tl.Longitude - br.Longitude) * 0.5
                    };

                    var span = new MKCoordinateSpan
                    {
                        LatitudeDelta  = Math.Abs(tl.Latitude - br.Latitude) * 0.5,
                        LongitudeDelta = Math.Abs(tl.Longitude - br.Longitude) * 0.5
                    };



                    MKCoordinateRegion region = new MKCoordinateRegion(center, span);


                    region = mapView.RegionThatFits(region);

                    mapView.SetRegion(region, true);
                }

                mapView.ShowsUserLocation = true;


                View.AddSubview(mapView);
            }
        }