public async Task <IActionResult> OnGetAirportDetail(string name, double latitude, double longitude) { var airportDetail = new AirportDetail(); var searchResponse = await GooglePlaces.NearBySearch.QueryAsync( new PlacesNearBySearchRequest { Key = GoogleApiKey, Name = name, Location = new GoogleApi.Entities.Places.Search.NearBy.Request.Location(latitude, longitude), Radius = 1000, }); if (!searchResponse.Status.HasValue || searchResponse.Status.Value != Status.Ok || !searchResponse.Results.Any()) { return(new BadRequestResult()); } var nearbyResult = searchResponse.Results.FirstOrDefault(); string placeId = nearbyResult.PlaceId; string photoReference = nearbyResult.Photos?.FirstOrDefault()?.PhotoReference; string photoCredit = nearbyResult.Photos?.FirstOrDefault()?.HtmlAttributions.FirstOrDefault(); var detailResponse = await GooglePlaces.Details.QueryAsync(new PlacesDetailsRequest { Key = GoogleApiKey, PlaceId = placeId, }); if (!detailResponse.Status.HasValue || detailResponse.Status.Value != Status.Ok) { return(new BadRequestResult()); } var detailResult = detailResponse.Result; airportDetail.FormattedAddress = detailResult.FormattedAddress; airportDetail.PhoneNumber = detailResult.InternationalPhoneNumber; airportDetail.Website = detailResult.Website; if (photoReference != null) { var photoResponse = await GooglePlaces.Photos.QueryAsync(new PlacesPhotosRequest { Key = GoogleApiKey, PhotoReference = photoReference, MaxWidth = 400, }); if (photoResponse.Buffer != null) { airportDetail.Photo = Convert.ToBase64String(photoResponse.Buffer); airportDetail.PhotoCredit = photoCredit; } } return(new JsonResult(airportDetail)); }
public async Task <IActionResult> OnGetAirportDetail(string name, double latitude, double longitude) { var airportDetail = new AirportDetail(); // Execute the search request var searchResponse = await GooglePlaces.NearBySearch.QueryAsync(new PlacesNearBySearchRequest { Key = GoogleApiKey, Name = name, Location = new Location(latitude, longitude), Radius = 1000 }); // If we did not get a good response, or the list of results are empty then get out of here if (!searchResponse.Status.HasValue || searchResponse.Status.Value != Status.Ok || !searchResponse.Results.Any()) { return(new BadRequestResult()); } // Get the first result var nearbyResult = searchResponse.Results.FirstOrDefault(); string placeId = nearbyResult.PlaceId; string photoReference = nearbyResult.Photos?.FirstOrDefault()?.PhotoReference; string photoCredit = nearbyResult.Photos?.FirstOrDefault()?.HtmlAttributions.FirstOrDefault(); // Execute the details request var detailsResponse = await GooglePlaces.Details.QueryAsync(new PlacesDetailsRequest { Key = GoogleApiKey, PlaceId = placeId }); // If we did not get a good response then get out of here if (!detailsResponse.Status.HasValue || detailsResponse.Status.Value != Status.Ok) { return(new BadRequestResult()); } // Set the details var detailsResult = detailsResponse.Result; airportDetail.FormattedAddress = detailsResult.FormattedAddress; airportDetail.PhoneNumber = detailsResult.InternationalPhoneNumber; airportDetail.Website = detailsResult.Website; if (photoReference != null) { // Execute the photo request var photosResponse = await GooglePlaces.Photos.QueryAsync(new PlacesPhotosRequest { Key = GoogleApiKey, PhotoReference = photoReference, MaxWidth = 400 }); if (photosResponse.Buffer != null) { airportDetail.Photo = Convert.ToBase64String(photosResponse.Buffer); airportDetail.PhotoCredit = photoCredit; } } return(new JsonResult(airportDetail)); }