Example #1
0
        public ActionResult GetRoutesWithPiecesInArea(string minLatLng, string maxLatLng)
        {
            Response.AppendHeader("Access-Control-Allow-Origin", "*");
            try
            {
                SearchPosition min = SearchPosition.CreateSearchPosition(minLatLng, null);
                SearchPosition max = SearchPosition.CreateSearchPosition(maxLatLng, null);

                if (min != null && max != null)
                {
                    var routePieces = DataHandler.GetRoutePiecesTouchingArea(min.Lat, min.Lng, max.Lat, max.Lng);

                    var byRoute = routePieces.Where(x => x.Route.Email != "*****@*****.**").GroupBy(x => x.RouteID);

                    var jsonRoutes = byRoute.Select(r => new { ID = r.First().Route.ID, TypeID = r.First().Route.TypeID, Name = r.First().Route.Name, FromName = r.First().Route.FromName, ToName = r.First().Route.ToName, Path = r.OrderBy(x => x.ID).Select(x => new { Lat = x.Lat, Lng = x.Lng, Name = x.Name }) });

                    return(Json(jsonRoutes, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { error = "Wrong parameters" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                DataHandler.WriteException(ex, Request.UserHostAddress);
                return(Json(new { error = "Internal server error" }, JsonRequestBehavior.AllowGet));
            }
        }
Example #2
0
        public ActionResult GetRoutesInArea(string minLatLng, string maxLatLng)
        {
            Response.AppendHeader("Access-Control-Allow-Origin", "*");
            try
            {
                SearchPosition min = SearchPosition.CreateSearchPosition(minLatLng, null);
                SearchPosition max = SearchPosition.CreateSearchPosition(maxLatLng, null);

                if (min != null && max != null)
                {
                    var routes     = DataHandler.GetRoutesInArea(min.Lat, min.Lng, max.Lat, max.Lng);
                    var jsonRoutes = routes.Select(r => new { ID = r.ID, TypeName = r.Type.Name, FromName = r.FromName, ToName = r.ToName, Name = r.Name });

                    return(Json(jsonRoutes, JsonRequestBehavior.AllowGet));
                }
                else
                {
                    return(Json(new { error = "Wrong parameters" }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                DataHandler.WriteException(ex, Request.UserHostAddress);
                return(Json(new { error = "Internal server error" }, JsonRequestBehavior.AllowGet));
            }
        }
Example #3
0
        public ActionResult Search(string de, string a, int?count, string key)
        {
            Response.AppendHeader("Access-Control-Allow-Origin", "*");
            try
            {
                var error = DataHandler.ChargeServiceCall("/Service/Search", key, Request.UserHostAddress, de + "|" + a + "|" + count);

                if (error == null)
                {
                    int c = count ?? 1;
                    if (c > 5)
                    {
                        c = 5;
                    }
                    if (c < 1)
                    {
                        c = 1;
                    }

                    SearchResultModel model = new SearchResultModel();

                    Searcher searcher = new Searcher(ViaDFGraph.Instance);

                    SearchPosition spFrom = SearchPosition.CreateSearchPosition(de, null);
                    SearchPosition spTo   = SearchPosition.CreateSearchPosition(a, null);

                    if (spFrom != null && spTo != null)
                    {
                        SearchParams sp = new SearchParams();
                        sp.StartSearch = spFrom;
                        sp.StartSearch.LoadNameFromDB();
                        sp.EndSearch = spTo;
                        sp.EndSearch.LoadNameFromDB();
                        sp.NrOfResults = c;

                        DataHandler.LogSearch(sp, Request.UserHostAddress);

                        List <SearchResult> results = searcher.DoSearch(sp);
                        model.Results = results;
                    }
                    else
                    {
                        model.Results = new List <SearchResult>();
                    }

                    using (var stream = new MemoryStream())
                    {
                        new DataContractJsonSerializer(typeof(SearchResultModel)).WriteObject(stream, model);
                        return(Content(Encoding.UTF8.GetString(stream.ToArray()), "application/json", Encoding.UTF8));
                    }
                }
                else
                {
                    return(Json(new { error = error }, JsonRequestBehavior.AllowGet));
                }
            }
            catch (Exception ex)
            {
                DataHandler.WriteException(ex, Request.UserHostAddress);
                return(Json(new { error = "Internal server error" }, JsonRequestBehavior.AllowGet));
            }
        }
Example #4
0
        public ActionResult Search(string de, string a, string origen, string destino)
        {
            Response.Cache.SetCacheability(HttpCacheability.Private);
            Response.Cache.SetLastModified(DateTime.Now);

            // check if available on client cache
            if (!string.IsNullOrEmpty(Request.Headers["If-Modified-Since"]))
            {
                Response.StatusCode        = 304;
                Response.StatusDescription = "Not Modified";
                return(null);
            }

            // try to get coordinates from strings
            if ((string.IsNullOrWhiteSpace(de) || string.IsNullOrWhiteSpace(a)) && !string.IsNullOrWhiteSpace(origen) && !string.IsNullOrWhiteSpace(destino))
            {
                SearchPosition from = Utils.GeoCodeSearchPosition(origen.Trim());
                SearchPosition to   = Utils.GeoCodeSearchPosition(destino.Trim());

                if (from != null && to != null)
                {
                    de = from.Lat.ToString(CultureInfo.InvariantCulture) + "," + from.Lng.ToString(CultureInfo.InvariantCulture);
                    a  = to.Lat.ToString(CultureInfo.InvariantCulture) + "," + to.Lng.ToString(CultureInfo.InvariantCulture);
                }
            }

            if (!string.IsNullOrWhiteSpace(de) && !string.IsNullOrWhiteSpace(a))
            {
                SearchPosition spFrom = SearchPosition.CreateSearchPosition(de, origen);
                SearchPosition spTo   = SearchPosition.CreateSearchPosition(a, destino);

                if (spFrom != null && spTo != null)
                {
                    Searcher     searcher = new Searcher(ViaDFGraph.Instance);
                    SearchParams sp       = new SearchParams();
                    sp.StartSearch = spFrom;
                    sp.StartSearch.LoadNameFromDB();
                    sp.EndSearch = spTo;
                    sp.EndSearch.LoadNameFromDB();
                    sp.NrOfResults = 3;

                    // log search
                    DataHandler.LogSearch(sp, Request.UserHostAddress);

                    if (string.IsNullOrWhiteSpace(sp.StartSearch.Name))
                    {
                        sp.StartSearch.Name = "Origen";
                    }
                    if (string.IsNullOrWhiteSpace(sp.EndSearch.Name))
                    {
                        sp.EndSearch.Name = "Destino";
                    }

                    List <SearchResult> results = searcher.DoSearch(sp);

                    if (results.Count > 0)
                    {
                        SearchResultModel model = new SearchResultModel();
                        model.Results         = results;
                        model.CloseBusinesses = DataHandler.GetCloseBusinesses(spTo.Lat, spTo.Lng, 0.25, 30);

                        SetSEO(results[0].Start.Name + " a " + results[0].End.Name + " - ¿Cómo llegar en transporte público?", "", "");

                        return(View("Search", model));
                    }
                }
            }

            return(View("NotFound"));
        }