예제 #1
0
        public async Task <ResolvedLocation[]> Find(string term, ResolvedLocation userLocation)
        {
            var request = new GoogleMapsApi.Entities.PlacesText.Request.PlacesTextRequest()
            {
                Query  = term,
                ApiKey = options.GoogleMapsApiKey
            };

            if (null != userLocation?.Coordinate)
            {
                request.Location = new GoogleMapsApi.Entities.Common.Location(userLocation.Coordinate.Lat,
                                                                              userLocation.Coordinate.Lng);
            }
            var res = await GoogleMapsApi.GoogleMaps.PlacesText.QueryAsync(request);

            var resWithCooards = res.Results.Where(r => r.Geometry?.Location != null);

            if (!resWithCooards.Any())
            {
                return(null);
            }
            return(resWithCooards.Select(v => new ResolvedLocation(new Coordinate(v.Geometry.Location.Latitude,
                                                                                  v.Geometry.Location.Longitude))
            {
                Attributes = new Dictionary <string, string> {
                    { "GoogleMapsPlaceId", v.PlaceId }
                },
                Address = v.FormattedAddress
            }).ToArray());
        }
예제 #2
0
        public async Task <DirectionsResult> GetTransitAsync(DirectionsRequest request)
        {
            Coordinate       from, to;
            ResolvedLocation resolvedStart, resolvedEnd = null;

            if (request.UserId != null)
            {
                var locationBias = (await locationsService.ResolveAsync(request.UserId, new UnresolvedLocation(UnresolvedLocation.Home))) ?? Vienna;
                resolvedStart = await locationsService.ResolveAsync(request.UserId, request.StartAddress, locationBias);

                if (null != resolvedStart)
                {
                    resolvedEnd = await locationsService.ResolveAsync(request.UserId, request.EndAddress, resolvedStart);
                }
            }
            else
            {
                var locationBias = Vienna;
                resolvedStart = await locationsService.ResolveAnonymousAsync(request.StartAddress, locationBias);

                if (null != resolvedStart)
                {
                    resolvedEnd = await locationsService.ResolveAnonymousAsync(request.EndAddress, resolvedStart);
                }
            }
            if (null == resolvedStart)
            {
                throw new LocationNotFoundException(request.StartAddress);
            }
            if (null == resolvedEnd)
            {
                throw new LocationNotFoundException(request.EndAddress);
            }
            from = resolvedStart.Coordinate;
            to   = resolvedEnd.Coordinate;
            var plan = await transitDirectionProvider.GetDirectionsAsync(new TransitDirectionsRequest
            {
                ArriveBy = request.ArriveBy,
                DateTime = request.DateTime,
                From     = from,
                To       = to
            });

            if (null == plan)
            {
                return(null);
            }
            await directionsCache.PutAsync(plan.Id, plan);

            return(new DirectionsResult()
            {
                CacheKey = plan.Id,
                TransitDirections = plan.GetTransitDirections()
            });
        }
        public async Task <ResolvedLocation[]> Find(string term, ResolvedLocation userLocation)
        {
            var res = await _openRouteServiceClient.Geocode(term, userLocation?.Coordinate);

            if (!res.features.Any())
            {
                return(null);
            }
            return(res.features.Select(f => new ResolvedLocation(new Coordinate(f.geometry.coordinates[1], f.geometry.coordinates[0]))
            {
                Address = f.properties.label,
                Attributes = new Dictionary <string, string>()
            }).ToArray());
        }
        public async Task PersistAsync(string term, string userId, ResolvedLocation resolvedLocation)
        {
            var persisted = await travelServiceContext.PersistedLocations.Where(v => v.Term == term && v.UserId == userId).SingleOrDefaultAsync();

            if (null == persisted)
            {
                persisted = new PersistedLocation()
                {
                    Id     = Guid.NewGuid().ToString(),
                    UserId = userId,
                    Term   = term
                };
                travelServiceContext.Add(persisted);
            }
            persisted.Lat        = resolvedLocation.Coordinate.Lat;
            persisted.Lng        = resolvedLocation.Coordinate.Lng;
            persisted.Address    = resolvedLocation.Address;
            persisted.Attributes = resolvedLocation.Attributes;
            await travelServiceContext.SaveChangesAsync();
        }
예제 #5
0
        public async Task <ResolvedLocation> ResolveAsync(string userId, UnresolvedLocation toResolve, ResolvedLocation userLocation = null)
        {
            if (null != toResolve.Coordinate)
            {
                return(new ResolvedLocation(toResolve.Coordinate));
            }
            if (null == toResolve.Address)
            {
                return(null);
            }
            var resolved = await resolvedLocationsStore.GetAsync(toResolve.Address, userId);

            if (null != resolved)
            {
                return(resolved);
            }
            if ("#home" == toResolve.Address || toResolve.Address.StartsWith("#event:"))
            {
                return(null);
            }
            return((await locationsProvider.Find(toResolve.Address, userLocation))?.FirstOrDefault());
        }
예제 #6
0
 public async Task <ResolvedLocation> ResolveAnonymousAsync(UnresolvedLocation toResolve, ResolvedLocation userLocation = null)
 {
     if (null != toResolve.Coordinate)
     {
         return(new ResolvedLocation(toResolve.Coordinate));
     }
     if (null == toResolve.Address)
     {
         return(null);
     }
     return((await locationsProvider.Find(toResolve.Address, userLocation))?.FirstOrDefault());
 }
예제 #7
0
 public async Task PersistAsync(string userId, string term, ResolvedLocation resolvedLocation)
 {
     await resolvedLocationsStore.PersistAsync(term, userId, resolvedLocation);
 }
예제 #8
0
        private async Task <IActionResult> ResolveLocations(string term, string userId, ResolvedLocation userLocation)
        {
            var location = await locationsService.ResolveAsync(userId, new UnresolvedLocation(term), userLocation);

            return(Ok(location));
        }