Пример #1
0
        private void OnDirectionsCalculated(MKDirectionsResponse response, NSError error)
        {
            if (error != null)
            {
                Console.WriteLine($"Error while calculating directions, error: {error}");
                return;
            }

            if (response == null || !response.Routes.Any())
            {
                Console.WriteLine($"Couldn't calculate directions.");
                return;
            }

            if (map.Overlays != null && map.Overlays.Any())
            {
                map.RemoveOverlays(map.Overlays);
            }

            var route = response.Routes.First();

            map.AddOverlay(route.Polyline, MKOverlayLevel.AboveRoads);

            var rect = route.Polyline.BoundingMapRect;

            map.SetRegion(MKCoordinateRegion.FromMapRect(rect), animated: true);
        }
        private void DropGeofenceCircle(PointF point)
        {
            CLLocationCoordinate2D convertedPoint = this.mapView.ConvertPoint(point, this.mapView);
            String pinTitle       = String.Format("Centre of geofence.");
            String subCoordinates = String.Format("{0},{1}", convertedPoint.Latitude.ToString(), convertedPoint.Longitude.ToString());

            if (this.droppedPin != null)
            {
                this.mapView.RemoveAnnotation(this.droppedPin);
            }

            this.droppedPin = new SimpleAnnotation(convertedPoint, pinTitle, subCoordinates);
            this.mapView.AddAnnotation(droppedPin);


            if (this.GeofenceMapDelegate.Circle != null)
            {
                this.mapView.RemoveOverlay(this.GeofenceMapDelegate.Circle);
            }

            this.GeofenceMapDelegate.Circle = MKCircle.Circle(convertedPoint, this.GeofenceService.RadiusOfGeofenceInMeters);
            this.mapView.AddOverlay(this.GeofenceMapDelegate.Circle);

            if (!this.mapView.VisibleMapRect.Contains(this.GeofenceMapDelegate.Circle.BoundingMap))
            {
                this.mapView.SetRegion(MKCoordinateRegion.FromMapRect(this.GeofenceMapDelegate.Circle.BoundingMap), true);
            }
        }
Пример #3
0
        public override void RegionChanged(MKMapView mapView, bool animated)
        {
            var mkregion = MKCoordinateRegion.FromMapRect(mapView.VisibleMapRect);
            var region   = new MapRegion(new Position(mkregion.Center.Latitude, mkregion.Center.Longitude),
                                         mkregion.Span.LatitudeDelta, mkregion.Span.LongitudeDelta);

            _renderer.Map.VisibleRegion = region;
        }
Пример #4
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();
            }
        }
Пример #5
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));
        }
        void CalculateRouteDetails(bool zoomMapToShowFullRoute = true, bool requestAlternateRoutes = true, MKDirectionsTransportType transportType = MKDirectionsTransportType.Automobile)
        {
            MKDirectionsRequest directionRequest = new MKDirectionsRequest();

            directionRequest.Source                  = sourceMapItem;
            directionRequest.Destination             = destinationMapItem;
            directionRequest.RequestsAlternateRoutes = requestAlternateRoutes;
            directionRequest.TransportType           = transportType;

            MKDirections eta = new MKDirections(directionRequest);

            eta.CalculateETA((MKETAResponse response, Foundation.NSError error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.Description);
                }
                else
                {
                    TimeSpan time = TimeSpan.FromSeconds(response.ExpectedTravelTime);
                    System.Diagnostics.Debug.WriteLine(time.ToString(@"hh\:mm\:ss\:fff"));
                }
            });

            MKDirections directions = new MKDirections(directionRequest);


            directions.CalculateDirections((MKDirectionsResponse response, Foundation.NSError error) =>
            {
                if (error != null)
                {
                    System.Diagnostics.Debug.WriteLine(error.Description);
                }
                else
                {
                    //remove previous route overlays
                    ClearOverlays();

                    currentRouteOverlay = response.Routes[0].Polyline;

                    MKRoute route;

                    //loop through backwards so first most route is renderered on top
                    for (int i = response.Routes.Length - 1; i >= 0; i--)
                    {
                        route = response.Routes[i];

                        //save overlay so it can be removed later if requested
                        overlaysList.Add(route.Polyline);

                        NativeMapView.AddOverlay(route.Polyline, MKOverlayLevel.AboveRoads);

                        if (i == 0)
                        {
                            if (zoomMapToShowFullRoute)
                            {
                                MKMapRect rect         = route.Polyline.BoundingMapRect;
                                MKMapRect expandedRect = NativeMapView.MapRectThatFits(rect, new UIEdgeInsets(20, 20, 20, 20));

                                NativeMapView.SetRegion(MKCoordinateRegion.FromMapRect(expandedRect), true);
                            }
                        }
                    }
                }
            });
        }
Пример #7
0
        /*private async void FindRouteAndDrawRoute (double sourceLat, double sourceLng, double destinationLat, double destinationLng, DirectionsMode directionsMode = DirectionsMode.Driving)
         *      {
         *  if (CLLocationManager.Status == CLAuthorizationStatus.NotDetermined ||  CLLocationManager.Status == CLAuthorizationStatus.Denied)
         *      return;
         *
         *  //get current location
         *  Geolocator locator = new Geolocator(){ DesiredAccuracy = 100};
         *  var location = await locator.GetPositionAsync(50000);
         *
         *  Console.WriteLine("Position Status: {0}", location.Timestamp);
         *  Console.WriteLine("Position Latitude: {0}", location.Latitude);
         *  Console.WriteLine("Position Longitude: {0}", location.Longitude);
         *
         *  MKPlacemark source = new MKPlacemark (new CLLocationCoordinate2D (sourceLat, sourceLng), new NSDictionary ());
         *              MKMapItem sourceItem = new MKMapItem (source);
         *
         *              desCoordinate = new CLLocationCoordinate2D (destinationLat, destinationLng);
         *              MKPlacemark destination = new MKPlacemark (new CLLocationCoordinate2D (destinationLat, destinationLng), new NSDictionary ());
         *              MKMapItem destinationItem = new MKMapItem (destination);
         *
         *              MKDirectionsRequest request = new MKDirectionsRequest ();
         *              request.Source = sourceItem;
         *              request.Destination = destinationItem;
         *              request.TransportType = directionsMode == DirectionsMode.Driving ? MKDirectionsTransportType.Automobile : MKDirectionsTransportType.Walking;
         *
         *              MKDirections direction = new MKDirections (request);
         *
         *              direction.CalculateDirections (delegate(MKDirectionsResponse response, NSError error) {
         *                      if (error == null) {
         *                              //remove all routes that has been drawn on map
         *                              if (mapView.Overlays != null && mapView.Overlays.Length != 0) {
         *                                      foreach (var overlay in mapView.Overlays) {
         *                                              mapView.RemoveOverlay (overlay);
         *                                      }
         *                              }
         *
         *                              //check if have route
         *                              if (response.Routes.Length == 0) {
         *                                      Mvx.Resolve<IMvxMessenger> ().Publish (new ToastMessage (this.ViewModel, "Cannot find the route"));
         *                              }
         *
         *                              //add new route
         *                              foreach (var route in response.Routes) {
         *                                      MKPolyline polyline = route.Polyline;
         *                                      mapView.AddOverlay (polyline);
         *
         *                                      ViewModel.TotalDistance = route.Distance;
         *                                      ViewModel.ExpectedTime = TimeSpan.FromSeconds(route.ExpectedTravelTime);
         *
         *                                      foreach (var step in route.Steps) {
         *                                              ViewModel.Routes.Add (new FlexyPark.Core.Models.RouteItem () {
         *                                                      Instruction = step.Instructions,
         *                                                      Distance = step.Distance,
         *                                                      Long = step.Polyline.Coordinate.Longitude,
         *                                                      Lat = step.Polyline.Coordinate.Latitude
         *                                              });
         *
         *                                              Console.WriteLine (step.Instructions);
         *                                              Console.WriteLine (step.Distance);
         *                                      }
         *                                      break;
         *                              }
         *
         *                      } else {
         *                              Console.WriteLine (error.LocalizedDescription);
         *                      }
         *              });
         *
         *              MKMapPoint userPoint = MKMapPoint.FromCoordinate (new CLLocationCoordinate2D (sourceLat, sourceLng));
         *              MKMapRect zoomRect = new MKMapRect (userPoint.X, userPoint.Y, 0.1, 0.1);
         *
         *              MKMapPoint annotationPoint = MKMapPoint.FromCoordinate (new CLLocationCoordinate2D (destinationLat, destinationLng));
         *              MKMapRect pointRect = new MKMapRect (annotationPoint.X, annotationPoint.Y, 0.1, 0.1);
         *
         *              zoomRect = MKMapRect.Union (zoomRect, pointRect);
         *
         *  overviewRegion = MKCoordinateRegion.FromMapRect (zoomRect);
         *  overviewRegion.Span.LatitudeDelta += 0.05;
         *  overviewRegion.Span.LongitudeDelta += 0.05;
         *
         *              StepAnnotation annotationSoure = new StepAnnotation ();
         *              annotationSoure.SetCoordinate (new CLLocationCoordinate2D (sourceLat, sourceLng));
         *              mapView.AddAnnotation (annotationSoure);
         *
         *              MKPointAnnotation annotationDest = new MKPointAnnotation ();
         *              annotationDest.SetCoordinate (new CLLocationCoordinate2D (destinationLat, destinationLng));
         *              mapView.AddAnnotation (annotationDest);
         *
         *              destinationAnnotation = annotationDest;
         *
         *  mapView.SetRegion (overviewRegion, true);
         *
         *  isShowRoute = true;
         *      }*/

        private async void FindRouteAndDrawRoute(double destinationLat, double destinationLng, DirectionsMode directionsMode = DirectionsMode.Driving)
        {
            if (CLLocationManager.Status == CLAuthorizationStatus.NotDetermined || CLLocationManager.Status == CLAuthorizationStatus.Denied)
            {
                return;
            }

            //get current location
            Geolocator locator = new Geolocator()
            {
                DesiredAccuracy = 100
            };
            var location = await locator.GetPositionAsync(50000);

            Console.WriteLine("Position Status: {0}", location.Timestamp);
            Console.WriteLine("Position Latitude: {0}", location.Latitude);
            Console.WriteLine("Position Longitude: {0}", location.Longitude);

            var sourceLat = location.Latitude;
            var sourceLng = location.Longitude;

            MKPlacemark source     = new MKPlacemark(new CLLocationCoordinate2D(sourceLat, sourceLng), new NSDictionary());
            MKMapItem   sourceItem = new MKMapItem(source);

            desCoordinate = new CLLocationCoordinate2D(destinationLat, destinationLng);
            MKPlacemark destination     = new MKPlacemark(new CLLocationCoordinate2D(destinationLat, destinationLng), new NSDictionary());
            MKMapItem   destinationItem = new MKMapItem(destination);

            MKDirectionsRequest request = new MKDirectionsRequest();

            request.Source        = sourceItem;
            request.Destination   = destinationItem;
            request.TransportType = directionsMode == DirectionsMode.Driving ? MKDirectionsTransportType.Automobile : MKDirectionsTransportType.Walking;

            MKDirections direction = new MKDirections(request);

            direction.CalculateDirections(delegate(MKDirectionsResponse response, NSError error) {
                if (error == null)
                {
                    //remove all routes that has been drawn on map
                    if (mapView.Overlays != null && mapView.Overlays.Length != 0)
                    {
                        foreach (var overlay in mapView.Overlays)
                        {
                            mapView.RemoveOverlay(overlay);
                        }
                    }

                    //check if have route
                    if (response.Routes.Length == 0)
                    {
                        Mvx.Resolve <IMvxMessenger> ().Publish(new ToastMessage(this.ViewModel, "Cannot find the route"));
                    }

                    //add new route
                    foreach (var route in response.Routes)
                    {
                        MKPolyline polyline = route.Polyline;
                        mapView.AddOverlay(polyline);

                        ViewModel.TotalDistance = route.Distance;
                        ViewModel.ExpectedTime  = TimeSpan.FromSeconds(route.ExpectedTravelTime);

                        foreach (var step in route.Steps)
                        {
                            ViewModel.Routes.Add(new FlexyPark.Core.Models.RouteItem()
                            {
                                Instruction = step.Instructions,
                                Distance    = step.Distance,
                                Long        = step.Polyline.Coordinate.Longitude,
                                Lat         = step.Polyline.Coordinate.Latitude
                            });

                            Console.WriteLine(step.Instructions);
                            Console.WriteLine(step.Distance);
                        }
                        break;
                    }
                }
                else
                {
                    Mvx.Resolve <IMvxMessenger>().Publish(new ToastMessage(this.ViewModel, error.LocalizedDescription));
                }
            });

            MKMapPoint userPoint = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(sourceLat, sourceLng));
            MKMapRect  zoomRect  = new MKMapRect(userPoint.X, userPoint.Y, 0.1, 0.1);

            MKMapPoint annotationPoint = MKMapPoint.FromCoordinate(new CLLocationCoordinate2D(destinationLat, destinationLng));
            MKMapRect  pointRect       = new MKMapRect(annotationPoint.X, annotationPoint.Y, 0.1, 0.1);

            zoomRect = MKMapRect.Union(zoomRect, pointRect);

            overviewRegion = MKCoordinateRegion.FromMapRect(zoomRect);
            overviewRegion.Span.LatitudeDelta  += 0.05;
            overviewRegion.Span.LongitudeDelta += 0.05;

            StepAnnotation annotationSoure = new StepAnnotation();

            annotationSoure.SetCoordinate(new CLLocationCoordinate2D(sourceLat, sourceLng));
            mapView.AddAnnotation(annotationSoure);

            MKPointAnnotation annotationDest = new MKPointAnnotation();

            annotationDest.SetCoordinate(new CLLocationCoordinate2D(destinationLat, destinationLng));
            mapView.AddAnnotation(annotationDest);

            destinationAnnotation = annotationDest;

            mapView.SetRegion(overviewRegion, true);

            isShowRoute = true;
        }