Example #1
0
        public VisitedPlace GetNearbyPlace(GeoPosition position, int locationId)
        {
            var response = _googleClient.GetNearbyPlaces(position.Latitude, position.Longitude);

            GPlacesResult selectedResult = null;

            if (response.IsOk && response.results.Any())
            {
                selectedResult = response.results.FirstOrDefault(r => r.types.Any(t => t == "point_of_interest"));

                if (selectedResult == null)
                {
                    selectedResult = response.results.FirstOrDefault();
                }
            }


            if (selectedResult == null)
            {
                Debug.WriteLine($"Could not find location by name for {position.GetDisplay()} ");
                return(null);
            }

            var place = new VisitedPlace()
            {
                Where      = position.LatLong,
                LocationId = locationId,
                PlaceName  = selectedResult.name.RemoveDiacritics(),
                Types      = selectedResult.types
            };

            var existing = _placeRepo.VisitedPlaces.FirstOrDefault(p => p.LocationId == place.LocationId &&
                                                                   p.PlaceName == place.PlaceName);

            if (existing != null)
            {
                return(existing);
            }

            // Handle New place
            place.Id = _placeRepo.GetNewId();
            _placeRepo.VisitedPlaces.Add(place);
            _placeRepo.Save();

            string baseDirectory = @"c:\TripLine\Places\Request\";

            Directory.CreateDirectory(baseDirectory);
            string fpath = baseDirectory + $"{place.Id}_{place.PlaceName}" + ".txt";

            using (var writer = new StreamWriter(File.Open(fpath, FileMode.Create, FileAccess.Write)))
            {
                writer.WriteLine(response.Serialize());
            }
            Debug.WriteLine($"Found new place {place.PlaceName} at {position.GetDisplay()} :: {place.Types} ");

            return(place);
        }
Example #2
0
        public Location GetLocation(GeoPosition geoPosition)
        {
            var location = _locationRepo.GetLocation(geoPosition.Latitude, geoPosition.Longitude);

            if (location != null)
            {
                return(location);
            }

            if (!Connected)
            {
                return(null);
            }

            var response = _googleClient.GetReverseGeocoding(geoPosition.Latitude, geoPosition.Longitude);

            if (response.IsOk)
            {
                location = CreateLocation(string.Empty, geoPosition, response);
                return(location);
            }
            else
            {
                Debug.WriteLine($"Could not find location by name for {geoPosition.GetDisplay()} ");
                //_unknownAddresses[address] = 1;
            }


            return(null);
        }