FindRoute() public method

public FindRoute ( NSAction callbackWhenDone ) : void
callbackWhenDone NSAction
return void
Exemplo n.º 1
0
        public MapViewController()
            : base()
        {
            BuildView();

            NavigationItem.Title = "Map";

            segmentControl = new UISegmentedControl(new RectangleF(0,0,200,25));
            segmentControl.InsertSegment("Find bikes", 0, false);
            segmentControl.InsertSegment("Find docks", 1, false);
            segmentControl.SelectedSegment = 0;
            segmentControl.ControlStyle = UISegmentedControlStyle.Bar;
            segmentControl.ValueChanged += delegate(object sender, EventArgs e) {
                if (segmentControl.SelectedSegment == 0)
                {
                    CurrentDisplayMode = DisplayMode.Bikes;
                    Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_MAP_BIKES, "", 1);
                } else
                {
                    CurrentDisplayMode = DisplayMode.Docks;
                    Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_MAP_DOCKS, "", 1);
                }

                RefreshPinColours();

                BikeLocation.UpdateFromWebsite(delegate {
                        InvokeOnMainThread(delegate{
                            RefreshPinColours();
                            //RefreshData();
                        });
                    });
            };

            NavigationItem.TitleView = segmentControl;

            NavigationItem.LeftBarButtonItem = new UIBarButtonItem(Resources.Routing, UIBarButtonItemStyle.Bordered, delegate {

                //nulls, think about the nulls!

                if (!CLLocationManager.LocationServicesEnabled)
                {
                    alert = new UIAlertView("No Location Available", "Sorry, no location services are available. However, you may still be able to use the timer and map.", null, "Ok");
                    alert.Show();
                    return;
                }

                if (mapView.UserLocation == null || mapView.UserLocation.Location == null)
                {
                    alert = new UIAlertView("No Location Available", "Sorry, your location is not yet available.", null, "Ok");
                    alert.Show();
                    return;
                }

                NSObject[] selectedPins = mapView.SelectedAnnotations;

                if (selectedPins == null || selectedPins.Length > 1)
                {
                    alert = new UIAlertView("Select a dock", "Please pick a cycle docking station to route to.", null, "Ok");
                    alert.Show();
                    return;
                }

                if (selectedPins.Length == 1)
                {
                    CycleAnnotation ca = selectedPins[0] as CycleAnnotation;
                    if (ca != null)
                    {
                        HideDistanceView();

                        var location = mapView.UserLocation.Location.Coordinate;
            #if DEBUG
                    location = Locations.BanksideMix;
            #endif

                        double distance = BikeLocation.CalculateDistanceInMeters(location, ca.Coordinate);
                        if (distance > 50000)
                        {
                            alert = new UIAlertView("Sorry, your route is too long", "We can only plot cycle routes up to 50km.", null, "Ok");
                            alert.Show();

                            return;
                        }

                        loadingView = new ActivityIndicatorLoadingView();
                        loadingView.Show("Finding your route");
                        Util.TurnOnNetworkActivity();

                        RemoveRouteAnnotation();

                        ThreadPool.QueueUserWorkItem(delegate {

                            using (NSAutoreleasePool newPool = new NSAutoreleasePool())
                            {
                                location = mapView.UserLocation.Location.Coordinate;
            #if DEBUG
                    location = Locations.BanksideMix;
            #endif

                                MapRouting routing = new MapRouting(location, ca.Coordinate);

                                //routing.FindRoute(delegate {
                                routing.FindRoute(delegate {
                                    InvokeOnMainThread(delegate {
                                        //Console.WriteLine("updating");
                                        loadingView.Hide();
                                        Util.TurnOffNetworkActivity();
                                        if (routing.HasRoute)
                                        {

                                            routeAnnotation = new CSRouteAnnotation(routing.PointsList);
                                            mapView.AddAnnotation(routeAnnotation);

                                            var region = routeAnnotation.Region;

                                            region.Span = new MKCoordinateSpan(region.Span.LatitudeDelta * 1.1f, region.Span.LongitudeDelta * 1.1f);

                                            mapView.SetRegion(region, true);

                                            //need to animate the distance etc here.

                                            ShowDistanceView(routing.DistanceForDisplay, routing.TimeForDisplay);

                                            BikeLocation.LogRoute();

                                        } else {
                                            alert = new UIAlertView("No route found", "Sorry, no route could be found or the route is too long.", null, "Ok");
                                            alert.Show();

                                        }
                                    });
                                });

                            }

                        });

                    }
                }
            });

            //NavigationController.NavigationBar.TintColor = Resources.DarkBlue;
            gpsButton = new UIBarButtonItem(Resources.Gps, UIBarButtonItemStyle.Bordered, delegate {

                if (!CLLocationManager.LocationServicesEnabled)
                {
                    alert = new UIAlertView("No Location Available", "Sorry, no location services are available. However, you may still be able to use the timer and map.", null, "Ok");
                    alert.Show();
                    return;
                }

                if (mapView.UserLocation != null)
                {
                    if (mapView.UserLocation.Location != null)
                    {
                        //NavigationItem.RightBarButtonItem = activityButton;

                        BikeLocation.UpdateFromWebsite(delegate {
                        InvokeOnMainThread(delegate{
                                RefreshPinColours();
                                //NavigationItem.RightBarButtonItem = gpsButton;
                            });
                        });

                        CLLocationCoordinate2D location = mapView.UserLocation.Location.Coordinate;
            #if DEBUG
                    location = Locations.BanksideMix;
            #endif
                        MKCoordinateRegion region = new MKCoordinateRegion(location, new MKCoordinateSpan(0.01, 0.01));

                        region = mapView.RegionThatFits(region);
                        mapView.SetRegion(region, true);

                    }
                }
            });

            NavigationItem.RightBarButtonItem = gpsButton;
        }
Exemplo n.º 2
0
        public MapViewController() : base()
        {
            BuildView();

            NavigationItem.Title = "Map";



            segmentControl = new UISegmentedControl(new RectangleF(0, 0, 200, 25));
            segmentControl.InsertSegment("Find bikes", 0, false);
            segmentControl.InsertSegment("Find docks", 1, false);
            segmentControl.SelectedSegment = 0;
            segmentControl.ControlStyle    = UISegmentedControlStyle.Bar;
            segmentControl.ValueChanged   += delegate(object sender, EventArgs e) {
                if (segmentControl.SelectedSegment == 0)
                {
                    CurrentDisplayMode = DisplayMode.Bikes;
                    Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_MAP_BIKES, "", 1);
                }
                else
                {
                    CurrentDisplayMode = DisplayMode.Docks;
                    Analytics.TrackEvent(Analytics.CATEGORY_ACTION, Analytics.ACTION_MAP_DOCKS, "", 1);
                }

                RefreshPinColours();

                BikeLocation.UpdateFromWebsite(delegate {
                    InvokeOnMainThread(delegate {
                        RefreshPinColours();
                        //RefreshData();
                    });
                });
            };


            NavigationItem.TitleView = segmentControl;


            NavigationItem.LeftBarButtonItem = new UIBarButtonItem(Resources.Routing, UIBarButtonItemStyle.Bordered, delegate {
                //nulls, think about the nulls!

                if (!CLLocationManager.LocationServicesEnabled)
                {
                    alert = new UIAlertView("No Location Available", "Sorry, no location services are available. However, you may still be able to use the timer and map.", null, "Ok");
                    alert.Show();
                    return;
                }

                if (mapView.UserLocation == null || mapView.UserLocation.Location == null)
                {
                    alert = new UIAlertView("No Location Available", "Sorry, your location is not yet available.", null, "Ok");
                    alert.Show();
                    return;
                }



                NSObject[] selectedPins = mapView.SelectedAnnotations;

                if (selectedPins == null || selectedPins.Length > 1)
                {
                    alert = new UIAlertView("Select a dock", "Please pick a cycle docking station to route to.", null, "Ok");
                    alert.Show();
                    return;
                }



                if (selectedPins.Length == 1)
                {
                    CycleAnnotation ca = selectedPins[0] as CycleAnnotation;
                    if (ca != null)
                    {
                        HideDistanceView();

                        var location = mapView.UserLocation.Location.Coordinate;
#if DEBUG
                        location = Locations.BanksideMix;
#endif

                        double distance = BikeLocation.CalculateDistanceInMeters(location, ca.Coordinate);
                        if (distance > 50000)
                        {
                            alert = new UIAlertView("Sorry, your route is too long", "We can only plot cycle routes up to 50km.", null, "Ok");
                            alert.Show();


                            return;
                        }



                        loadingView = new ActivityIndicatorLoadingView();
                        loadingView.Show("Finding your route");
                        Util.TurnOnNetworkActivity();


                        RemoveRouteAnnotation();



                        ThreadPool.QueueUserWorkItem(delegate {
                            using (NSAutoreleasePool newPool = new NSAutoreleasePool())
                            {
                                location = mapView.UserLocation.Location.Coordinate;
#if DEBUG
                                location = Locations.BanksideMix;
#endif

                                MapRouting routing = new MapRouting(location, ca.Coordinate);



                                //routing.FindRoute(delegate {
                                routing.FindRoute(delegate {
                                    InvokeOnMainThread(delegate {
                                        //Console.WriteLine("updating");
                                        loadingView.Hide();
                                        Util.TurnOffNetworkActivity();
                                        if (routing.HasRoute)
                                        {
                                            routeAnnotation = new CSRouteAnnotation(routing.PointsList);
                                            mapView.AddAnnotation(routeAnnotation);

                                            var region = routeAnnotation.Region;

                                            region.Span = new MKCoordinateSpan(region.Span.LatitudeDelta * 1.1f, region.Span.LongitudeDelta * 1.1f);


                                            mapView.SetRegion(region, true);


                                            //need to animate the distance etc here.


                                            ShowDistanceView(routing.DistanceForDisplay, routing.TimeForDisplay);

                                            BikeLocation.LogRoute();
                                        }
                                        else
                                        {
                                            alert = new UIAlertView("No route found", "Sorry, no route could be found or the route is too long.", null, "Ok");
                                            alert.Show();
                                        }
                                    });
                                });
                            }
                        });
                    }
                }
            });

            //NavigationController.NavigationBar.TintColor = Resources.DarkBlue;
            gpsButton = new UIBarButtonItem(Resources.Gps, UIBarButtonItemStyle.Bordered, delegate {
                if (!CLLocationManager.LocationServicesEnabled)
                {
                    alert = new UIAlertView("No Location Available", "Sorry, no location services are available. However, you may still be able to use the timer and map.", null, "Ok");
                    alert.Show();
                    return;
                }

                if (mapView.UserLocation != null)
                {
                    if (mapView.UserLocation.Location != null)
                    {
                        //NavigationItem.RightBarButtonItem = activityButton;

                        BikeLocation.UpdateFromWebsite(delegate {
                            InvokeOnMainThread(delegate {
                                RefreshPinColours();
                                //NavigationItem.RightBarButtonItem = gpsButton;
                            });
                        });

                        CLLocationCoordinate2D location = mapView.UserLocation.Location.Coordinate;
#if DEBUG
                        location = Locations.BanksideMix;
#endif
                        MKCoordinateRegion region = new MKCoordinateRegion(location, new MKCoordinateSpan(0.01, 0.01));

                        region = mapView.RegionThatFits(region);
                        mapView.SetRegion(region, true);
                    }
                }
            });


            NavigationItem.RightBarButtonItem = gpsButton;
        }