private static MapLocation SearchGoogleMaps(int cragId, string cragName, CragLocationContext context) { var queries = new DatabaseQueries(context); var searchableCragName = SearchableString(cragName); var request = WebRequest.Create( $"https://maps.googleapis.com/maps/api/place/textsearch/json?query={searchableCragName}&key={GoogleApikey}"); request.Method = "GET"; var response = request.GetResponse(); var content = string.Empty; using (var stream = response.GetResponseStream()) { using (var streamReader = new StreamReader(stream)) { content = streamReader.ReadToEnd(); } } var result = JsonConvert.DeserializeObject <RootObject>(content); var location = result.results[0].geometry.location; queries.AddCragToDatabase(cragId, cragName, (decimal)location.lat, (decimal)location.lng, true); return(new MapLocation((decimal)location.lat, (decimal)location.lng)); }
public void CheckFindsStanage() { var connection = @"Server=(localdb)\mssqllocaldb;Database=CragLocations;Trusted_Connection=True;ConnectRetryCount=0"; var options = new DbContextOptionsBuilder <CragLocationContext>().UseSqlServer(connection).Options; var context = new CragLocationContext(options); var computedLocation = CragLocator.FindCrag("Stanage Planation", 101, context); var stanagesLocation = new MapLocation(53.347292m, -1.633261m); Assert.AreEqual(stanagesLocation.Latitude, computedLocation.Latitude); Assert.AreEqual(stanagesLocation.Longitude, computedLocation.Longitude); }
public static MapLocation FindCrag(string cragName, int ukcCragId, CragLocationContext context) { var queries = new DatabaseQueries(context); if (queries.TryGetLocationFromDatabase(ukcCragId, out var location)) { return(location); } try { return(SearchGoogleMaps(ukcCragId, cragName, context)); } catch { return(FindCountyLocation(ukcCragId, cragName, context)); } }
public HomeController(CragLocationContext context) { this.context = context; }