public async Task <EventDataDetailsViewModel> GetEventInfos(Event targetEvent) { var usr = await GetCurrentUser(); var dataDuration = _db.UserToEventsData.Include(t => t.Event).Include(f => f.User).Where(evf => evf.User.Id == usr.Id && evf.Event.Id == targetEvent.Id).ToList(); for (int i = 0; i < Enum.GetNames(typeof(DirectionModes)).Length; i++) { DirectionModes md = (DirectionModes)i; var direction = await _apiHelper.GetDirections(usr.Address, targetEvent.Address, "metric", md); var dataobje = ApiObjectToEventData(direction, usr, targetEvent, md); var exists = dataDuration.FirstOrDefault(d => d.DirectionMode == md); if (exists != null) { exists.Distance = dataobje.Distance; exists.Duration = dataobje.Duration; exists.MapLink = dataobje.MapLink; _db.Entry(exists).State = EntityState.Modified; } else { _db.UserToEventsData.Add(dataobje); } } await _db.SaveChangesAsync(); var parts = await _db.Participations.Include(p => p.Event).Include(p => p.User).Where(p => p.Event.Id == targetEvent.Id).ToListAsync(); var isCurrentUserTheOwner = usr != null && (targetEvent.Owner != null && targetEvent.Owner.Id == usr.Id); return(new EventDataDetailsViewModel { Event = targetEvent, UserData = dataDuration, CanModify = isCurrentUserTheOwner, Participants = parts }); }
private EventRelativeToUserInformation ApiObjectToEventData(DirectionObject dObj, ApplicationUser usr, Event targetEvent, DirectionModes md) { var data = new EventRelativeToUserInformation { User = usr, Event = targetEvent, DirectionMode = md, Unit = "km", Distance = (dObj.routes.Any() ? dObj.routes.FirstOrDefault().legs.FirstOrDefault().distance.value / 1000.0 : 0.0), Duration = (dObj.routes.Any() ? dObj.routes.FirstOrDefault().legs.FirstOrDefault().duration.value / 60 : 0), MapLink = _apiHelper.GenerateMapLink(usr.Address, targetEvent.Address, md) }; return(data); }
/// <summary> /// Get Directions from a Origin to a Destination /// </summary> /// <param name="Origin">The Origin BasicGeoposition</param> /// <param name="Destination">The Destination BasicGeoposition</param> /// <param name="Mode">Mode for example Driving, walking or etc.</param> /// <param name="WayPoints">Points you want to go in your way</param> /// <exception cref="ArgumentOutOfRangeException">Waypoints are not available in transit mode.</exception> /// <returns></returns> public static async Task <Rootobject> GetDirections(BasicGeoposition Origin, BasicGeoposition Destination, DirectionModes Mode = DirectionModes.driving, List <BasicGeoposition> WayPoints = null) { try { if (Mode == DirectionModes.transit && WayPoints != null) { throw new ArgumentOutOfRangeException("Waypoints are not available in transit mode."); } var m = Mode.ToString(); var requestUrl = String.Format("http://maps.google.com/maps/api/directions/json?origin=" + Origin.Latitude + "," + Origin.Longitude + "&destination=" + Destination.Latitude + "," + Destination.Longitude + "&units=metric&mode=" + Mode + "&lang=" + AppCore.GoogleMapRequestsLanguage); if (WayPoints != null && WayPoints.Count != 0) { requestUrl += "&waypoints="; for (int i = 0; i <= WayPoints.Count - 1; i++) { if (i < WayPoints.Count - 1) { requestUrl += $"{WayPoints[i].Latitude},{WayPoints[i].Longitude}|"; } else { requestUrl += $"{WayPoints[i].Latitude},{WayPoints[i].Longitude}"; } } } //requestUrl += $"&key={AppCore.GoogleMapAPIKey}"; var http = new HttpClient(); http.DefaultRequestHeaders.UserAgent.ParseAdd(AppCore.HttpUserAgent); var s = await http.GetStringAsync(new Uri(requestUrl, UriKind.RelativeOrAbsolute)); return(JsonConvert.DeserializeObject <Rootobject>(s)); } catch (Exception) { return(null); } }
/// <summary> /// Get Directions from a Origin to a Destination /// </summary> /// <param name="Origin">The Origin BasicGeoposition</param> /// <param name="Destination">The Destination BasicGeoposition</param> /// <param name="Mode">Mode for example Driving, walking or etc.</param> /// <returns></returns> public static async Task <Rootobject> GetDirections(BasicGeoposition Origin, BasicGeoposition Destination, DirectionModes Mode = DirectionModes.driving) { try { var m = Mode.ToString(); var requestUrl = String.Format("http://maps.google.com/maps/api/directions/json?origin=" + Origin.Latitude + "," + Origin.Longitude + "&destination=" + Destination.Latitude + "," + Destination.Longitude + "&units=metric&mode=" + Mode); var http = new HttpClient(); http.DefaultRequestHeaders.UserAgent.ParseAdd(AppCore.HttpUserAgent); var s = await http.GetStringAsync(new Uri(requestUrl, UriKind.RelativeOrAbsolute)); return(JsonConvert.DeserializeObject <Rootobject>(s)); } catch (Exception) { return(null); } }
public string GenerateMapLink(string usrPostalCode, string targeteventAddress, DirectionModes mode) { var start = $"origin={truncateaddress(usrPostalCode)}"; var end = $"destination={truncateaddress(targeteventAddress)}"; var mod = $"travelmode={mode}"; //https://www.google.com/maps/dir/?api=1&origin=Space+Needle+Seattle+WA&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling var result = $@"https://www.google.com/maps/dir/?api=1&" + start + "&" + end + "&" + mod; return(result); }
public async Task <DirectionObject> GetDirections(string userAdress, string eventAddress, string unit, DirectionModes md1) { string apkey = SecretConfiguration.Get("google.direction.api"); HttpClient httpcli = new HttpClient(); var start = truncateaddress(userAdress); var end = truncateaddress(eventAddress); var request = $@"https://maps.googleapis.com/maps/api/directions/json?origin=" + start + "&destination=" + end + $"&mode={md1}&units={unit}&key={apkey}"; var result = await httpcli.GetStringAsync(request); var objResult = JsonConvert.DeserializeObject <DirectionObject>(result); return(objResult); }