public void ContainsPoint()
        {
            MKMapRect  rect  = new MKMapRect();
            MKMapPoint point = new MKMapPoint();

            Assert.False(rect.Contains(point), "default");
        }
Example #2
0
        void Init(Tuple <GeoAnchor, GeoAnchor> anchors)
        {
            // To compute the distance between two geographical co-ordinates, we first need to
            // convert to MapKit co-ordinates
            fromAnchorFloorplanPoint = anchors.Item1.Pixel;
            fromAnchorMKPoint        = MKMapPoint.FromCoordinate(anchors.Item1.LatitudeLongitude);
            MKMapPoint toAnchorMKPoint = MKMapPoint.FromCoordinate(anchors.Item2.LatitudeLongitude);

            // So that we can use MapKit's helper function to compute distance.
            // this helper function takes into account the curvature of the earth.
            var distanceBetweenPointsMeters = (nfloat)MKGeometry.MetersBetweenMapPoints(fromAnchorMKPoint, toAnchorMKPoint);

            var dx = anchors.Item1.Pixel.X - anchors.Item2.Pixel.X;
            var dy = anchors.Item1.Pixel.Y - anchors.Item2.Pixel.Y;

            // Distance between two points in pixels (on the floorplan image)
            var distanceBetweenPointsPixels = Hypot(dx, dy);

            // This gives us pixels/meter
            PixelsPerMeter = distanceBetweenPointsPixels / distanceBetweenPointsMeters;

            // Get the 2nd anchor's eastward/southward distance in meters from the first anchor point.
            var hyp = FetchRect(fromAnchorMKPoint, toAnchorMKPoint);

            // Angle of diagonal to east (in geographic)
            nfloat angleFromEastAndHypo = NMath.Atan2(hyp.South, hyp.East);

            // Angle of diagonal to horizontal (in floorplan)
            nfloat angleFromXAndHypo = NMath.Atan2(dy, dx);

            // Rotation amount from the geographic anchor line segment
            // to the floorplan anchor line segment
            // This is angle between X axis and East direction. This angle shows how you floor plan exists in real world
            radiansRotated = angleFromXAndHypo - angleFromEastAndHypo;
        }
Example #3
0
        public static MKMapRect FBMapRectForBoundingBox(FBBoundingBox boundingBox)
        {
            MKMapPoint topLeft  = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(boundingBox.x0, boundingBox.y0));
            MKMapPoint botRight = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(boundingBox.xf, boundingBox.yf));

            return(new MKMapRect(topLeft.X, botRight.Y, Math.Abs(botRight.X - topLeft.X), Math.Abs(botRight.Y - topLeft.Y)));
        }
Example #4
0
        private void UpdateMapRect(MKMapRect visibleMapRect, bool animated)
        {
            if (this.Tree == null || visibleMapRect.IsNull || visibleMapRect.IsEmpty)
            {
                return;
            }

            MKMapRect clusterMapRect = new MKMapRect().World;

            if (this.MarginFactor != kCKMarginFactorWorld)
            {
                clusterMapRect = visibleMapRect.Inset(-this.MarginFactor * visibleMapRect.Width, -this.MarginFactor * visibleMapRect.Height);
            }

            double zoom      = this.map.GetZoom();
            var    algorithm = (zoom < this.MaxZoomLevel) ? this.Algorithm : new CKClusterAlgorithm();
            var    clusters  = algorithm.ClustersInRect(clusterMapRect, zoom, this.Tree);
            var    toRemove  = this.clusters.ToList();

            foreach (var newCluster in clusters)
            {
                this.map.AddCluster(newCluster);
                if (!visibleMapRect.Contains(MKMapPoint.FromCoordinate(newCluster.Coordinate)) || !animated)
                {
                    continue;
                }

                var c = this.Clusters.ToList();
                foreach (var oldCluster in c)
                {
                    if (!clusterMapRect.Contains(MKMapPoint.FromCoordinate(oldCluster.Coordinate)))
                    {
                        continue;
                    }

                    if (this.CLLocationCoordinateEqual(newCluster.Coordinate, oldCluster.Coordinate))
                    {
                        continue;
                    }

                    if (oldCluster.Annotations.Contains(newCluster.Annotations[0]))
                    {
                        this.map.MoveCluster(newCluster, oldCluster.Coordinate, newCluster.Coordinate, null);
                    }
                    else if (newCluster.Annotations.Contains(oldCluster.Annotations[0]))
                    {
                        this.map.MoveCluster(oldCluster, oldCluster.Coordinate, newCluster.Coordinate, (finished) => { this.map.RemoveCluster(oldCluster); });
                        toRemove.Remove(oldCluster);
                    }
                }
            }

            foreach (var cluster in toRemove)
            {
                this.map.RemoveCluster(cluster);
            }

            this.Clusters       = clusters;
            this.VisibleMapRect = visibleMapRect;
        }
Example #5
0
        public static double GetDistance(CLLocationCoordinate2D from, CLLocationCoordinate2D to)
        {
            MKMapPoint a = MKMapPoint.FromCoordinate(from);
            MKMapPoint b = MKMapPoint.FromCoordinate(to);

            return((a.X - b.X) * (a.X - b.X) + (a.Y - b.Y) * (a.Y - b.Y));
        }
Example #6
0
        public override List <CKCluster> ClustersInRect(MapKit.MKMapRect rect, double zoom, Tree.ICKAnnotationTree tree)
        {
            var clusters    = new Dictionary <double, CKCluster>();
            var annotations = tree.AnnotationsInRect(rect);
            var numCells    = Math.Ceiling(256 * Math.Pow(2, zoom) / this.CellSize);

            foreach (var ann in annotations)
            {
                var point = MKMapPoint.FromCoordinate(ann.Coordinate);
                var col   = numCells * point.X / MKMapSize.World.Width;
                var row   = numCells * point.Y / MKMapSize.World.Height;

                var key = numCells * row + col;

                CKCluster cluster;
                if (clusters.ContainsKey(key))
                {
                    cluster = clusters[key];
                }
                else
                {
                    cluster       = this.ClusterWithCoordinate(ann.Coordinate);
                    clusters[key] = cluster;
                }
                cluster.AddAnnotation(ann);
            }

            return(clusters.Values.ToList());
        }
 public MKMapRect MKMapRectByAddingPoint(MKMapRect rect, MKMapPoint point)
 {
     return(MKMapRect.Union(rect, new MKMapRect()
     {
         Origin = point, Size = MKMapRect.Null.Size
     }));
 }
Example #8
0
        public void OnMapMessage(Map sender, XamMapz.Messaging.MapMessage message)
        {
            if (message is ZoomMessage)
            {
//                var msg = (ZoomMessage)message;
                if (_mapViewDelegate.IsRegionChanging == false)
                {
                    UpdateRegion();
                }
            }
            else if (message is ProjectionMessage)
            {
                var msg       = (ProjectionMessage)message;
                var screenPos = MKMapPoint.FromCoordinate(msg.Position.ToCoordinate2D());
                msg.ScreenPosition = new Point(screenPos.X, screenPos.Y);
            }
            else if (message is MoveMessage)
            {
                if (_mapViewDelegate.IsRegionChanging == false)
                {
                    var msg = (MoveMessage)message;
                    NativeMap.CenterCoordinate = msg.Target.ToCoordinate2D();
                }
            }
        }
		void Init (Tuple<GeoAnchor, GeoAnchor> anchors)
		{
			// To compute the distance between two geographical co-ordinates, we first need to
			// convert to MapKit co-ordinates
			fromAnchorFloorplanPoint = anchors.Item1.Pixel;
			fromAnchorMKPoint = MKMapPoint.FromCoordinate (anchors.Item1.LatitudeLongitude);
			MKMapPoint toAnchorMKPoint = MKMapPoint.FromCoordinate (anchors.Item2.LatitudeLongitude);

			// So that we can use MapKit's helper function to compute distance.
			// this helper function takes into account the curvature of the earth.
			var distanceBetweenPointsMeters = (nfloat)MKGeometry.MetersBetweenMapPoints (fromAnchorMKPoint, toAnchorMKPoint);

			var dx = anchors.Item1.Pixel.X - anchors.Item2.Pixel.X;
			var dy = anchors.Item1.Pixel.Y - anchors.Item2.Pixel.Y;

			// Distance between two points in pixels (on the floorplan image)
			var distanceBetweenPointsPixels = Hypot (dx, dy);

			// This gives us pixels/meter
			PixelsPerMeter = distanceBetweenPointsPixels / distanceBetweenPointsMeters;

			// Get the 2nd anchor's eastward/southward distance in meters from the first anchor point.
			var hyp = FetchRect (fromAnchorMKPoint, toAnchorMKPoint);

			// Angle of diagonal to east (in geographic)
			nfloat angleFromEastAndHypo = NMath.Atan2 (hyp.South, hyp.East);

			// Angle of diagonal to horizontal (in floorplan)
			nfloat angleFromXAndHypo = NMath.Atan2 (dy, dx);

			// Rotation amount from the geographic anchor line segment
			// to the floorplan anchor line segment
			// This is angle between X axis and East direction. This angle shows how you floor plan exists in real world
			radiansRotated = angleFromXAndHypo - angleFromEastAndHypo;
		}
Example #10
0
        // MapKit alternative
        public static double MKGetDistanceBetween(Airport airport1, Airport airport2)
        {
            CLLocationCoordinate2D loc1   = new CLLocationCoordinate2D(airport1.Latitude, airport1.Longitude);
            CLLocationCoordinate2D loc2   = new CLLocationCoordinate2D(airport2.Latitude, airport2.Longitude);
            MKMapPoint             point1 = MKMapPoint.FromCoordinate(loc1);
            MKMapPoint             point2 = MKMapPoint.FromCoordinate(loc2);

            return(MKGeometry.MetersBetweenMapPoints(point1, point2) / MetersPerNauticalMile);
        }
        public override async Task <IEnumerable <Geoposition> > CalculateRoute(Geoposition from, Geoposition to)
        {
            IEnumerable <Geoposition> result = Enumerable.Empty <Geoposition>();

            var nativeFrom = CoordinateConverter.ConvertToNative(from);
            var nativeTo   = CoordinateConverter.ConvertToNative(to);

            var userPlaceMark      = new MKPlacemark(nativeFrom, new Foundation.NSDictionary());
            var incidencePlaceMark = new MKPlacemark(nativeTo, new Foundation.NSDictionary());

            var sourceItem = new MKMapItem(userPlaceMark);
            var destItem   = new MKMapItem(incidencePlaceMark);

            var request = new MKDirectionsRequest
            {
                Source        = sourceItem,
                Destination   = destItem,
                TransportType = MKDirectionsTransportType.Automobile
            };

            var directions = new MKDirections(request);

            MKPolyline polyRoute = null;

            directions.CalculateDirections((response, error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.LocalizedDescription);
                }
                else
                {
                    if (response.Routes.Any())
                    {
                        var firstRoute = response.Routes.First();
                        polyRoute      = firstRoute.Polyline;
                    }
                }
            });

            do
            {
                await Task.Delay(100);
            }while (directions.Calculating);

            if (polyRoute != null)
            {
                result = polyRoute.Points.Select(s =>
                {
                    CLLocationCoordinate2D coordinate = MKMapPoint.ToCoordinate(s);
                    return(CoordinateConverter.ConvertToAbstraction(coordinate));
                });
            }

            return(result);
        }
        public override IEnumerable <Position> GetCurrentUserRoute()
        {
            var positions = _currentUserRoute?.Points.Select(r =>
            {
                var coord = MKMapPoint.ToCoordinate(r);
                return(CoordinateConverter.ConvertToAbstraction(coord));
            });

            return(positions);
        }
        public void ShowCluster(CKCluster cluster, bool animated)
        {
            MKMapRect mapRect = MKMapRect.Null;

            foreach (var annoation in cluster.Annotations)
            {
                mapRect = MKMapRectByAddingPoint(mapRect, MKMapPoint.FromCoordinate(annoation.Coordinate));
            }
            _internalMap.SetVisibleMapRect(mapRect, animated);
        }
        private async Task <IEnumerable <Position> > RequestMKMapRoutePoints(RouteModel route, CLLocationCoordinate2D from, CLLocationCoordinate2D to)
        {
            IEnumerable <Position> result = Enumerable.Empty <Position>();

            var userPlaceMark      = new MKPlacemark(from, new Foundation.NSDictionary());
            var incidencePlaceMark = new MKPlacemark(to, new Foundation.NSDictionary());

            var sourceItem = new MKMapItem(userPlaceMark);
            var destItem   = new MKMapItem(incidencePlaceMark);

            var request = new MapKit.MKDirectionsRequest
            {
                Source        = sourceItem,
                Destination   = destItem,
                TransportType = MKDirectionsTransportType.Automobile
            };

            var directions = new MKDirections(request);

            MKPolyline polyRoute = null;

            directions.CalculateDirections((response, error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.LocalizedDescription);
                }
                else
                {
                    if (response.Routes.Any())
                    {
                        var firstRoute  = response.Routes.First();
                        polyRoute       = firstRoute.Polyline;
                        route.Distance += firstRoute.Distance;
                        route.Duration += firstRoute.ExpectedTravelTime / 60;
                    }
                }
            });

            do
            {
                await Task.Delay(100);
            }while (directions.Calculating);

            if (polyRoute != null)
            {
                result = polyRoute.Points.Select(s =>
                {
                    CLLocationCoordinate2D coordinate = MKMapPoint.ToCoordinate(s);
                    return(CoordinateConverter.ConvertToAbstraction(coordinate));
                });
            }

            return(result);
        }
        void SetMapToCoordinate(CLLocationCoordinate2D coordinate)
        {
            var region = new MKCoordinateRegion(coordinate, currentSpan);

            currentRegion = region;

            var newCenterPoint = MKMapPoint.FromCoordinate(coordinate);

            map.SetVisible(new MKMapRect(newCenterPoint.X, newCenterPoint.Y, currentSpan.LatitudeDelta, currentSpan.LongitudeDelta));
            map.SetRegion(region);
        }
Example #16
0
        // Two points in rectangular co-ordinate system create a rectange
        static EastSouthDistance FetchRect(MKMapPoint fromAnchorMKPoint, MKMapPoint toPoint)
        {
            double latitude          = MKMapPoint.ToCoordinate(fromAnchorMKPoint).Latitude;
            nfloat metersPerMapPoint = (nfloat)MKGeometry.MetersPerMapPointAtLatitude(latitude);

            var eastSouthDistance = new EastSouthDistance {
                East  = (nfloat)(toPoint.X - fromAnchorMKPoint.X) * metersPerMapPoint,
                South = (nfloat)(toPoint.Y - fromAnchorMKPoint.Y) * metersPerMapPoint
            };

            return(eastSouthDistance);
        }
Example #17
0
        public static FBBoundingBox FBBoundingBoxForMapRect(MKMapRect mapRect)
        {
            CLLocationCoordinate2D topLeft  = MKMapPoint.ToCoordinate(mapRect.Origin);
            CLLocationCoordinate2D botRight = MKMapPoint.ToCoordinate(new MKMapPoint(mapRect.MaxX, mapRect.MaxY));

            double minLat = botRight.Latitude;
            double maxLat = topLeft.Latitude;
            double minLon = topLeft.Longitude;
            double maxLon = botRight.Longitude;

            return(FBBoundingBoxMake(minLat, minLon, maxLat, maxLon));
        }
Example #18
0
        private void RefreshMap(CLLocationCoordinate2D centerCoords)
        {
            _mapRegion = new MKCoordinateRegion(centerCoords, _mapSpan);

            var center = MKMapPoint.FromCoordinate(centerCoords);

            map.SetVisible(new MKMapRect(center.X, center.Y, _mapSpan.LatitudeDelta, _mapSpan.LongitudeDelta));

            map.SetRegion(_mapRegion);

            map.AddAnnotation(centerCoords, WKInterfaceMapPinColor.Green);
        }
Example #19
0
        public void CenterOnLocations(List <LatitudeLongitude> locations
                                      , bool with_animation = false)
        {
            if (locations == null || locations.Count == 0)
            {
                return;
            }

            var minLat  = locations.Select(el => el.Latitude).Min();
            var minLong = locations.Select(el => el.Longitude).Min();

            var maxLat  = locations.Select(el => el.Latitude).Max();
            var maxLong = locations.Select(el => el.Longitude).Max();

            var coordinateMin = new CLLocationCoordinate2D(minLat, minLong);
            var coordinateMax = new CLLocationCoordinate2D(maxLat, maxLong);

            var upperLeft  = MKMapPoint.FromCoordinate(coordinateMin);
            var lowerRight = MKMapPoint.FromCoordinate(coordinateMax);

            var mapRect = new MKMapRect(upperLeft.X
                                        , upperLeft.Y
                                        , lowerRight.X - upperLeft.X
                                        , lowerRight.Y - upperLeft.Y);

            var region = MKCoordinateRegion.FromMapRect(mapRect);

            MapViewControl.SetVisibleMapRect(mapRect
                                             , new UIEdgeInsets(50, 20, 100, 20)
                                             , true);

            try
            {
                if (region.Center.Latitude > -89 && region.Center.Latitude < 89 && region.Center.Longitude > -179 && region.Center.Longitude < 179)
                {
                    if (region.Span.LatitudeDelta < 0)
                    {
                        region.Span.LatitudeDelta = 0.0;
                    }
                    if (region.Span.LongitudeDelta < 0)
                    {
                        region.Span.LongitudeDelta = 0.0;
                    }

                    MapViewControl.SetRegion(region, with_animation);
                }
            }
            catch (Exception)
            {
                //UIAlertView alert = new UIAlertView("Exception", String.Format("{0} \n {1}?", e.Message, e.StackTrace), null, "Cancel");
                //alert.Show();
            }
        }
Example #20
0
        public void ContainsPoint()
        {
            MKMapRect  rect  = new MKMapRect();
            MKMapPoint point = new MKMapPoint();

            if (TestRuntime.CheckXcodeVersion(8, 0))
            {
                Assert.True(rect.Contains(point), "default");
            }
            else
            {
                Assert.False(rect.Contains(point), "default");
            }
        }
Example #21
0
        private void CleanClustersOutsideMapRect(List <MapCluster> clusters, MKMapRect mapRect)
        {
            var clustersToRemove = new List <MapCluster>();

            foreach (var cluster in clusters)
            {
                if (!mapRect.Contains(MKMapPoint.FromCoordinate(cluster.ClusterCoordinate)))
                {
                    clustersToRemove.Add(cluster);
                }
            }

            clusters.RemoveAll(clustersToRemove.Contains);
        }
Example #22
0
        public MKMapRect CKCreateRectFromSpan(CLLocationCoordinate2D center, double span)
        {
            double halfSpan = span / 2;

            double     latitude  = Math.Min(center.Latitude + halfSpan, 90);
            double     longitude = Math.Max(center.Longitude - halfSpan, -180);
            MKMapPoint a         = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(latitude, longitude));

            latitude  = Math.Max(center.Latitude - halfSpan, -90);
            longitude = Math.Min(center.Longitude + halfSpan, 180);
            MKMapPoint b = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(latitude, longitude));

            return(new MKMapRect(Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Abs(a.X - b.X), Math.Abs(a.Y - b.Y)));
        }
Example #23
0
        public static void ShowCluster(this MKMapView map, CKCluster cluster, UIEdgeInsets insets, bool animated)
        {
            MKMapRect zoomRect = MKMapRect.Null;

            foreach (var annotation in cluster.Annotations)
            {
                var pointRect = new MKMapRect();
                pointRect.Origin = MKMapPoint.FromCoordinate(annotation.Coordinate);
                pointRect.Size   = new MKMapSize(0.1d, 0.1d);
                zoomRect         = MKMapRect.Union(zoomRect, pointRect);
            }

            map.SetVisibleMapRect(zoomRect, insets, animated);
        }
        public MKMapView setMapRegion(MKMapView map)
        {
            MKMapRect zoomRect = new MKMapRect(double.PositiveInfinity, double.PositiveInfinity, 0, 0);

            foreach (MKAnnotation annotation in map.Annotations)
            {
                if (annotation.Coordinate.Latitude != 0)
                {
                    MKMapPoint annotationPoint = MKMapPoint.FromCoordinate(annotation.Coordinate);
                    MKMapRect  pointRect       = new MKMapRect(annotationPoint.X, annotationPoint.Y, 0.1, 0.1);
                    zoomRect = MKMapRect.Union(zoomRect, pointRect);
                }
            }
            map.SetVisibleMapRect(zoomRect, new UIEdgeInsets(40, 20, 20, 20), true);

            return(map);
        }
Example #25
0
        public void MetersBetweenMapPoints()
        {
            MKMapPoint a = new MKMapPoint();

            Assert.That(MKGeometry.MetersBetweenMapPoints(a, a), Is.EqualTo(0.0), "a-a");

            MKMapPoint b = new MKMapPoint(1000, 1000);

            Assert.That(MKGeometry.MetersBetweenMapPoints(b, b), Is.EqualTo(0.0), "b-b");

            Assert.That(MKGeometry.MetersBetweenMapPoints(a, b), Is.EqualTo(18.153).Within(0.001), "a-b");
            Assert.That(MKGeometry.MetersBetweenMapPoints(b, a), Is.EqualTo(18.153).Within(0.001), "b-a");

            MKMapPoint c = new MKMapPoint(Double.NaN, Double.NaN);

            Assert.That(MKGeometry.MetersBetweenMapPoints(a, c), Is.NaN, "NaN");
        }
Example #26
0
        public static bool Insert(CKNode node, MKAnnotation annotation)
        {
            var point = MKMapPoint.FromCoordinate(annotation.Coordinate);

            if (!node.Bound.Contains(point))
            {
                return(false);
            }

            if (node.Count < node.Capacity)
            {
                CKPoint tPoint = new CKPoint();
                tPoint.Annotation = annotation;
                tPoint.Point      = point;
                CKNode.Add(node, tPoint);
                return(true);
            }

            if (node.NW == null)
            {
                CKNode.Subdivide(node);
            }

            if (CKNode.Insert(node.NW, annotation))
            {
                return(true);
            }

            if (CKNode.Insert(node.NE, annotation))
            {
                return(true);
            }

            if (CKNode.Insert(node.SW, annotation))
            {
                return(true);
            }

            if (CKNode.Insert(node.SE, annotation))
            {
                return(true);
            }

            return(false);
        }
Example #27
0
        public static RadarBounds GetRadarBounds(this MKMapView map)
        {
            CLLocationCoordinate2D center = map.CenterCoordinate;

            var topLeft     = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(center.Latitude + (map.Region.Span.LatitudeDelta / 2.0), center.Longitude - (map.Region.Span.LongitudeDelta / 2.0)));
            var bottomRight = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(center.Latitude - (map.Region.Span.LatitudeDelta / 2.0), center.Longitude + (map.Region.Span.LongitudeDelta / 2.0)));

            var bounds = new RadarBounds {
                MinLat = center.Latitude + (map.Region.Span.LatitudeDelta / 2.0),
                MaxLat = center.Latitude - (map.Region.Span.LatitudeDelta / 2.0),
                MinLon = center.Longitude - (map.Region.Span.LongitudeDelta / 2.0),
                MaxLon = center.Longitude + (map.Region.Span.LongitudeDelta / 2.0),

                Height = Math.Abs(topLeft.Y - bottomRight.Y),
                Width  = Math.Abs(topLeft.X - bottomRight.X)
            };

            return(bounds);
        }
Example #28
0
        public static MKCoordinateRegion CoordinateRegionForCoordinates(
            CLLocationCoordinate2D[] coordinates,
            MKMapSize margin)
        {
            var rect = default(MKMapRect);

            foreach (var coordinate in coordinates)
            {
                var point     = MKMapPoint.FromCoordinate(coordinate);
                var pointRect = new MKMapRect(
                    point.X - margin.Width / 2,
                    point.Y - margin.Height / 2,
                    margin.Width,
                    margin.Height);

                rect = rect != default(MKMapRect) ? MKMapRect.Union(rect, pointRect) : pointRect;
            }

            return(MKCoordinateRegion.FromMapRect(rect));
        }
Example #29
0
        // Convert the specified geographic coordinate to floorplan point
        public CGPoint Convert(CLLocationCoordinate2D coordinate)
        {
            // Get the distance east & south with respect to the first anchor point in meters
            EastSouthDistance rect = FetchRect(fromAnchorMKPoint, MKMapPoint.FromCoordinate(coordinate));

            // Convert the east-south anchor point distance to pixels (still in east-south)
            var     scaleTransform      = CGAffineTransform.MakeScale(PixelsPerMeter, PixelsPerMeter);
            CGPoint pixelsXYInEastSouth = scaleTransform.TransformPoint(new CGPoint(rect.East, rect.South));

            // Rotate the east-south distance to be relative to floorplan horizontal
            // This gives us an xy distance in pixels from the anchor point.
            var     rotateTransform = CGAffineTransform.MakeRotation(radiansRotated);
            CGPoint xy = rotateTransform.TransformPoint(pixelsXYInEastSouth);

            // From Anchor point may not be top letf corner.
            // however, we need the pixels from the (0, 0) of the floorplan
            // so we adjust by the position of the anchor point in the floorplan
            xy.X += fromAnchorFloorplanPoint.X;
            xy.Y += fromAnchorFloorplanPoint.Y;

            return(xy);
        }
Example #30
0
        private void MapTapped(MKMapView mapView, UITapGestureRecognizer tappedGesture)
        {
            var mapview = tappedGesture.View as MKMapView;

            CLLocationCoordinate2D coord2D = mapView.ConvertPoint(tappedGesture.LocationInView(mapView), mapView);
            var mapPoint = MKMapPoint.FromCoordinate(coord2D);

            foreach (IMKOverlay overlay in mapview.Overlays)
            {
                var renderer = (MKOverlayPathRenderer)mapview.RendererForOverlay(overlay);
                if (renderer == null)
                {
                    return;
                }

                var polyTouched = renderer.Path.ContainsPoint(renderer.PointForMapPoint(mapPoint), true);

                if (polyTouched)
                {
                    //Do something
                }
            }
        }
        void LoadRoute()
        {
            var filePath = NSBundle.MainBundle.PathForResource("route", "csv", "os4Maps","");
            var fileContents = System.IO.File.ReadAllText(filePath);
            var pointStrings = fileContents.Split('\n');

            MKMapPoint northEastPoint = new MKMapPoint(), southWestPoint = new MKMapPoint();
            var pointArr = new List<MKMapPoint>();
            var coordArr = new List<CLLocationCoordinate2D>();

            // start with the widest possible viewport
            var tl = new CLLocationCoordinate2D (-90, 180);
            // top left
            var br = new CLLocationCoordinate2D (90, -180);
            foreach (var s in pointStrings)
            {
                string[] latlonArr = s.Split(',');
                double latitude = Convert.ToDouble(latlonArr[0]);
                double longitude = Convert.ToDouble(latlonArr[1]);

                CLLocationCoordinate2D coordinate = new CLLocationCoordinate2D (latitude, longitude);

                // narrow the viewport bit-by-bit
                tl.Longitude = Math.Min (tl.Longitude, coordinate.Longitude);
                tl.Latitude = Math.Max (tl.Latitude, coordinate.Latitude);
                br.Longitude = Math.Max (br.Longitude, coordinate.Longitude);
                br.Latitude = Math.Min (br.Latitude, coordinate.Latitude);

                coordArr.Add(coordinate);

                // Can't find MonoTouch **MKMapPointForCoordinate**
            //				MKMapPoint point = ???????? (coordinate);
            //				if (northEastPoint.X == 0 & northEastPoint.Y == 0)
            //				{
            //					Debug.WriteLine("First Point: {0},{1}",point.X, point.Y);
            //					northEastPoint = point;
            //					southWestPoint = point;
            //				}
            //				else
            //				{
            //					if (point.X > northEastPoint.X)
            //						northEastPoint.X = point.X;
            //					if(point.Y > northEastPoint.Y)
            //						northEastPoint.Y = point.Y;
            //					if (point.X < southWestPoint.X)
            //						southWestPoint.X = point.X;
            //					if (point.Y < southWestPoint.Y)
            //						southWestPoint.Y = point.Y;
            //				}
            //				pointArr.Add(point);
            }
            // Can't use MKMapRect without the **MKMapPointForCoordinate**
            //_routeRect = new MKMapRect (southWestPoint.X, southWestPoint.Y, northEastPoint.X - southWestPoint.X, northEastPoint.Y - southWestPoint.Y);
            //Debug.WriteLine("RouteRect: {0},{1};{2},{3}", _routeRect.Origin.X, _routeRect.Origin.Y, _routeRect.Size.Width, _routeRect.Size.Height);

            CLLocationCoordinate2D[] clar = coordArr.ToArray();
            RouteLine = MKPolyline.FromCoordinates (clar);
            // divide the range by two to get the center
            var center = new CLLocationCoordinate2D { Latitude = tl.Latitude - (tl.Latitude - br.Latitude) * 0.5, Longitude = tl.Longitude + (br.Longitude - tl.Longitude) * 0.5 };
            // calculate the span, with 20% margin so pins aren't on the edge
            var span = new MKCoordinateSpan { LatitudeDelta = Math.Abs (tl.Latitude - br.Latitude) * 1.2, LongitudeDelta = Math.Abs (br.Longitude - tl.Longitude) * 1.2 };
            RouteRegion = new MKCoordinateRegion { Center = center, Span = span };
        }
 public static CLLocationCoordinate2D ToCLLocationCoordinate(this MKMapPoint mapPoint)
 {
     return(new CLLocationCoordinate2D(mapPoint.X, mapPoint.Y));
 }
		// Two points in rectangular co-ordinate system create a rectange
		static EastSouthDistance FetchRect (MKMapPoint fromAnchorMKPoint, MKMapPoint toPoint)
		{
			double latitude = MKMapPoint.ToCoordinate (fromAnchorMKPoint).Latitude;
			nfloat metersPerMapPoint = (nfloat)MKGeometry.MetersPerMapPointAtLatitude (latitude);

			var eastSouthDistance = new EastSouthDistance {
				East = (nfloat)(toPoint.X - fromAnchorMKPoint.X) * metersPerMapPoint,
				South = (nfloat)(toPoint.Y - fromAnchorMKPoint.Y) * metersPerMapPoint
			};
			return eastSouthDistance;
		}