public Coordinates GetByAddress(string streetNumber, string street, string postalCode, string country, string state) { Coordinates coordinatesResponse = null; try { using (var c = new WebClient()) { c.Headers.Add("Accept", "Application/json"); string address = string.Format(Const.SearchConst.FormatAddress, postalCode, ReplaceCharPlus(street), ReplaceCharPlus(streetNumber), ReplaceCharPlus(country), ReplaceCharPlus(state)); var json = c.DownloadString(string.Format(Const.SearchConst.ApiGeepersGetAddressCoordinate, ConfigurationData.GeepersHost(), Settings.GeepersApiKey, address)); var response = JsonConvert.DeserializeObject<dynamic>(json); var location = response.data; if (location != null) { coordinatesResponse = new Coordinates { Latitude = (double)location["latitude"].Value, Longitude = (double)location["longitude"].Value }; } } } catch (Exception ex) { Log.Error("GeoIpService_GetByAddress_Error: " + ex.ToString()); } return coordinatesResponse; }
/// <summary> /// Calculates the distance between two points of latitude and longitude. /// Great Link - http://www.movable-type.co.uk/scripts/latlong.html /// </summary> /// <param name="coordinate1">First coordinate.</param> /// <param name="coordinate2">Second coordinate.</param> /// <param name="unitsOfLength">Sets the return value unit of length.</param> public static Double Distance(Coordinates coordinate1, Coordinates coordinate2, UnitsOfLength unitsOfLength) { var theta = coordinate1.Longitude - coordinate2.Longitude; var distance = Math.Sin(coordinate1.Latitude.ToRadian()) * Math.Sin(coordinate2.Latitude.ToRadian()) + Math.Cos(coordinate1.Latitude.ToRadian()) * Math.Cos(coordinate2.Latitude.ToRadian()) * Math.Cos(theta.ToRadian()); distance = Math.Acos(distance); distance = distance.ToDegree(); distance = distance * 60 * 1.1515; if (unitsOfLength == UnitsOfLength.Kilometer) distance = distance * MilesToKilometers; else if (unitsOfLength == UnitsOfLength.NauticalMiles) distance = distance * MilesToNautical; return (distance); }
/// <summary> /// Get distance of an specialist /// </summary> /// <param name="searcherCoordinates"></param> /// <param name="specialist"></param> /// <returns></returns> public static double GetDistance(Coordinates searcherCoordinates, UserDto specialist) { double distance = 999999; if (searcherCoordinates != null) { var geoIp = new GeoIpService(); LocationDto location = specialist.Locations.FirstOrDefault(); Coordinates responseSpecialist = GetLocationByUserAddress(specialist); if (responseSpecialist != null) { distance = GeoHelper.Distance(searcherCoordinates, new Coordinates { Latitude = responseSpecialist.Latitude, Longitude = responseSpecialist.Longitude }, UnitsOfLength.Kilometer); } } return distance; }
/// <summary> /// Get user location follow address or geeperId /// </summary> /// <param name="user"></param> /// <returns></returns> public static Coordinates GetLocationByUserAddress(UserDto user) { var geoIp = new GeoIpService(); LocationDto location = user.Locations.FirstOrDefault(); Coordinates coordinates = new Coordinates(); if (location != null) { if (string.IsNullOrWhiteSpace(location.GeepersName)) { coordinates = geoIp.GetByAddress(location.StreetNumber, location.Street, location.Postcode, location.Country, location.State); } else { coordinates = geoIp.GetByGeepers(location.GeepersName); if (coordinates == null) { coordinates = geoIp.GetByAddress(location.StreetNumber, location.Street, location.Postcode, location.Country, location.State); } } } return coordinates; }
/// <summary> /// Gets the latitude, longitude and country information for a IP address. /// Uses the current HTTP Context's IP if none is supplied /// TODO: needs to be cached /// </summary> /// <param name="ip"></param> /// <returns></returns> public Coordinates GetByIp(string ip = null) { Coordinates coordinatesResponse = null; try { if (string.IsNullOrEmpty(ip)) { ip = IpAddressHelper.GetIP4Address(); } if (!_cache.Contains(ip)) { using (var c = new WebClient()) { var json = c.DownloadString(string.Format(Const.SearchConst.UrlIpInfo, ip)); var response = JsonConvert.DeserializeObject<dynamic>(json); string location = response["loc"].ToString(); string country = response["country"].ToString(); coordinatesResponse = new Coordinates { Latitude = double.Parse(location.Split(',')[0]), Longitude = double.Parse(location.Split(',')[1]), }; _cache.Add(ip, coordinatesResponse, new CacheItemPolicy { AbsoluteExpiration = DateTimeOffset.Now.AddDays(1) }); } } coordinatesResponse = (Coordinates)_cache[ip]; } catch (Exception ex) { Log.Error("GeoIpService_GetByIp_Error:" + ex.Message); } return coordinatesResponse; }
/// <summary> /// Get list of specialist who are favourite by customer id /// </summary> /// <param name="customerId"></param> public List<FavouriteDto> GetFavouriteListByCustomerId(Guid customerId, Coordinates searcherLocation = null) { var query = Repository.Query<Favourite>().Where(x => x.Customer.Id.Equals(customerId)); var favourites = Mapper.Map<List<Favourite>, List<FavouriteDto>>(query.ToList()); var favouritesList = !favourites.Any() ? new List<FavouriteDto>() : favourites; //get latitude, longitude of Searcher if (searcherLocation == null && favouritesList != null && favouritesList.Count > 0) { searcherLocation = GeoHelper.GetPositionSearcher(favouritesList.FirstOrDefault().Customer); } foreach (var f in favouritesList) { f.Proximity = GeoHelper.GetDistance(searcherLocation, f.Specialist); f.Feedback = bookingService.RatingCalculator(f.Specialist.Id); } return favouritesList; }
/// <summary> /// Advanced search by Amazon /// </summary> /// <param name="model"></param> /// <param name="start"></param> /// <param name="size"></param> /// <param name="sortBy"></param> /// <param name="searcherId"></param> /// <param name="searcherLocation"></param> /// <returns></returns> public List<SearchResultDto> AdvancedSearchByAmazon(AdvancedSearchDto model, int start, int size, SortBy sortBy, Guid searcherId, Coordinates searcherLocation = null) { var result = new List<SearchResultDto>(); try { if (model != null) { // Search var AmazonCloud = new AmazonCloudSearch(); var resultList = AmazonCloud.AdvancedSearch(model, sortBy); if (resultList != null && resultList.Count > 0) { //get latitude, longitude of Searcher if (searcherLocation == null) { searcherLocation = GetPositionSearcher(searcherId); } // Get list user by list user Id from database var userList = userService.GetListUserFromAWFSearchResults(resultList); // Join 2 list for sort by score var searchResultList = (from user in userList join item in resultList on user.Id equals item.Id where user.Profiles != null && user.Profiles.Any() && user.Profiles.FirstOrDefault().Specializations != null && user.Profiles.FirstOrDefault().Specializations.Any() && user.Locations != null && user.Locations.Any() && user.Status == Status.Active orderby item.Score descending select new SearchResultDto { User = user, Score = item.Score, Proximity = GetDistance(searcherLocation, user), TalkNowRate = (double)user.Profile.Specializations.SingleOrDefault().TalkNowRate, Feedback = bookingService.RatingCalculator(user.Id) }).ToList(); // Search result after sort result = ResultSequencing(searchResultList, sortBy, model.ShowVerifiedExperts, model.ShowIndependentMember, model.ShowBusinessMember); // Paging search result if (size != -1) { result = result.Skip(start).Take(size).ToList(); } } } } catch (Exception ex) { Log.Error(ex.ToString()); } return result; }
/// <summary> /// Get distance of an specialist /// </summary> /// <param name="searcherCoordinates"></param> /// <param name="specialist"></param> /// <returns></returns> public double GetDistance(Coordinates searcherCoordinates, UserDto specialist) { return GeoHelper.GetDistance(searcherCoordinates, specialist); }