public async Task <FoodTruckAPI> GetFoodTrucks() { string url = $"https://maps.googleapis.com/maps/api/place/textsearch/json?query=Foodtrucks+in+Milwaukee&key={API_KEY.googleMapsApiKey}"; HttpClient client = new HttpClient(); HttpResponseMessage response = await client.GetAsync(url); if (response.IsSuccessStatusCode) { string json = await response.Content.ReadAsStringAsync(); FoodTruckAPI foodTruckAPI = JsonConvert.DeserializeObject <FoodTruckAPI>(json); return(foodTruckAPI); } else { return(null); } }
private void MapGoogleResultsToFoodTruckModel(FoodTruckAPI foodTruckAPI) { var foodTrucks = foodTruckAPI; foreach (Result res in foodTrucks.results) { //Turn each one of these messy arrays of info into an actual FoodTruck Object for your DB FoodTruck ft = new FoodTruck { FoodTruckName = res.name, FoodTruckPhone = res.plus_code.compound_code, Lat = res.geometry.location.lat, Lng = res.geometry.location.lng, Address = res.formatted_address, FoodType = res.types.ToString(), Price_level = res.price_level, Rating = res.rating, Place_Id = res.place_id }; //add all the properties one at a time. _context.FoodTrucks.Add(ft); } _context.SaveChanges(); }
public async Task <IActionResult> Index() { FoodTruckAPI foodTruckAPI = await _googleMapService.GetFoodTrucks(); return(View(foodTruckAPI)); }