public ActionResult Navigate(NavigationViewModel navData)
        {
            ShowRouteViewModel model = new ShowRouteViewModel();
            string             id    = User.Identity.GetUserId();

            if (navData.selectedRoute != null)
            {
                model.savedRoute = db.SavedRoutes.Where(r => r.Observee.ApplicationUserId == id && r.name == navData.selectedRoute).Select(e => e).FirstOrDefault();
                model.observee   = db.Observees.Where(e => e.ApplicationUserId == id).Select(e => e).FirstOrDefault();
                model.avoid      = GetCrimeData(Double.Parse(model.savedRoute.start_latitude), Double.Parse(model.savedRoute.start_longitude), Double.Parse(model.savedRoute.end_latitude), Double.Parse(model.savedRoute.end_logitude));
                var thisId = db.Observees.Where(e => e.ApplicationUserId == id).Select(e => e.id).FirstOrDefault();
                List <AvoidanceRoute> avoidMarks  = db.AvoidanceRoutes.Where(e => e.ObserveeId == thisId || e.ObserveeId == null).ToList();
                List <string>         avoidCoords = new List <string>();
                foreach (AvoidanceRoute x in avoidMarks)
                {
                    avoidCoords.Add(x.TopLeftLatitude + "," + x.TopLeftLongitude + ";" + x.BottomRightLatitude + "," + x.BottomRightLongitude);
                    if (model.avoid != "")
                    {
                        model.avoid += "!";
                    }
                    model.avoid += (x.TopLeftLatitude + "," + x.TopLeftLongitude + ";" + x.BottomRightLatitude + "," + x.BottomRightLongitude);
                }
                model.route = CreateRoute.Retrieve(model.savedRoute.routeRequest);
            }
            else if (navData.StartAddress != null && navData.EndAddress != null)
            {
                GeoCode  geo        = new GeoCode();
                string   startcoord = geo.Retrieve(navData.StartAddress);
                string[] waypoint1;
                string   stopcoord;
                string[] waypoint2;
                try
                {
                    waypoint1      = startcoord.Split(',');
                    stopcoord      = geo.Retrieve(navData.EndAddress);
                    waypoint2      = stopcoord.Split(',');
                    model.observee = db.Observees.Where(e => e.ApplicationUserId == id).Select(e => e).FirstOrDefault();
                    model.avoid    = GetCrimeData(Double.Parse(waypoint1[0]), Double.Parse(waypoint1[1]), Double.Parse(waypoint2[0]), Double.Parse(waypoint2[1]));
                }
                catch
                {
                    return(RedirectToAction("Navigate"));
                }

                var thisId = db.Observees.Where(e => e.ApplicationUserId == id).Select(e => e.id).FirstOrDefault();
                List <AvoidanceRoute> avoidMarks  = db.AvoidanceRoutes.Where(e => e.ObserveeId == thisId || e.ObserveeId == null).ToList();
                List <string>         avoidCoords = new List <string>();
                foreach (AvoidanceRoute x in avoidMarks)
                {
                    avoidCoords.Add(x.TopLeftLatitude + "," + x.TopLeftLongitude + ";" + x.BottomRightLatitude + "," + x.BottomRightLongitude);
                    if (model.avoid != "")
                    {
                        model.avoid += "!";
                    }
                    model.avoid += (x.TopLeftLatitude + "," + x.TopLeftLongitude + ";" + x.BottomRightLatitude + "," + x.BottomRightLongitude);
                }
                model.route = CreateRoute.Retrieve(startcoord, stopcoord, model.avoid);
            }
            else
            {
                return(RedirectToAction("Navigate"));
            }
            TempData["myModel"] = model;
            return(View("ShowRoute", model));
        }
Exemplo n.º 2
0
        public ActionResult CreateRoutes(ObserverSaveRouteModel navData)
        {
            ShowRouteViewModel model = new ShowRouteViewModel();
            string             id    = User.Identity.GetUserId();

            if (navData.nav.StartAddress != null && navData.nav.EndAddress != null)
            {
                GeoCode  geo        = new GeoCode();
                string   startcoord = geo.Retrieve(navData.nav.StartAddress);
                string[] waypoint1;
                string   stopcoord;
                string[] waypoint2;
                List <AvoidanceRoute> avoidMarks;
                //try
                //{
                //    waypoint1 = startcoord.Split(',');
                //    stopcoord = geo.Retrieve(navData.nav.EndAddress);
                //    waypoint2 = stopcoord.Split(',');
                //    model.observee = db.Observees.Where(e => e.id == int.Parse(navData.observeeId)).FirstOrDefault();
                //    model.avoid = GetCrimeData(Double.Parse(waypoint1[0]), Double.Parse(waypoint1[1]), Double.Parse(waypoint2[0]), Double.Parse(waypoint2[1]));
                //    avoidMarks = db.AvoidanceRoutes.Where(e => e.ObserveeId == int.Parse(navData.observeeId) || e.ObserveeId == null).ToList();
                //}
                //catch
                //{
                //    return RedirectToAction("CreateRoutes");
                //}
                waypoint1 = startcoord.Split(',');
                stopcoord = geo.Retrieve(navData.nav.EndAddress);
                waypoint2 = stopcoord.Split(',');
                int temp = int.Parse(navData.observeeId);
                model.observee = db.Observees.Where(e => e.id == temp).FirstOrDefault();
                model.avoid    = GetCrimeData(Double.Parse(waypoint1[0]), Double.Parse(waypoint1[1]), Double.Parse(waypoint2[0]), Double.Parse(waypoint2[1]));
                temp           = int.Parse(navData.observeeId);
                avoidMarks     = db.AvoidanceRoutes.Where(e => e.ObserveeId == temp || e.ObserveeId == null).ToList();



                List <string> avoidCoords = new List <string>();
                foreach (AvoidanceRoute x in avoidMarks)
                {
                    avoidCoords.Add(x.TopLeftLatitude + "," + x.TopLeftLongitude + ";" + x.BottomRightLatitude + "," + x.BottomRightLongitude);
                    if (model.avoid != "")
                    {
                        model.avoid += "!";
                    }
                    model.avoid += (x.TopLeftLatitude + "," + x.TopLeftLongitude + ";" + x.BottomRightLatitude + "," + x.BottomRightLongitude);
                }
                model.route = CreateRoute.Retrieve(startcoord, stopcoord, model.avoid);

                try
                {
                    SavedRoute newRoute = new SavedRoute();
                    newRoute.name            = navData.route.name;
                    newRoute.ObserveeId      = int.Parse(navData.observeeId);
                    newRoute.start_latitude  = model.route.waypoint[0].mappedPosition.latitude.ToString();
                    newRoute.start_longitude = model.route.waypoint[0].mappedPosition.longitude.ToString();
                    newRoute.end_latitude    = model.route.waypoint[1].mappedPosition.latitude.ToString();
                    newRoute.end_logitude    = model.route.waypoint[1].mappedPosition.longitude.ToString();
                    newRoute.waypoint1       = newRoute.start_latitude + "," + newRoute.start_longitude;
                    newRoute.waypoint2       = newRoute.end_latitude + "," + newRoute.end_logitude;
                    newRoute.avoidstring     = model.avoid;
                    newRoute.routeRequest    = "https://route.api.here.com/routing/7.2/calculateroute.json?app_id=" + Keys.HEREAppID + "&app_code=" + Keys.HEREAppCode + "&waypoint0=" + newRoute.waypoint1 + "&waypoint1=" + newRoute.waypoint2 + "&mode=fastest;pedestrian;traffic:disabled&avoidareas=" + newRoute.avoidstring;

                    db.SavedRoutes.Add(newRoute);
                    db.SaveChanges();
                }
                catch
                {
                    return(RedirectToAction("CreateRoutes"));
                }
            }
            else
            {
                return(RedirectToAction("CreateRoutes"));
            }


            return(RedirectToAction("Index"));
        }