示例#1
0
        public async Task <IActionResult> OnGetAirportDetail(string name, double latitude, double longitude)
        {
            AirportDetail airportDetail = new AirportDetail();

            var searchResponse = await GooglePlaces.NearBySearch.QueryAsync(new PlacesNearBySearchRequest
            {
                Key      = GoogleApiKey,
                Name     = name,
                Location = new 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 photoRef     = nearbyResult.Photos?.FirstOrDefault()?.PhotoReference;
            string photoCredit  = nearbyResult.Photos?.FirstOrDefault()?.HtmlAttributions.FirstOrDefault();

            var detailsResponse = await GooglePlaces.Details.QueryAsync(new PlacesDetailsRequest
            {
                Key     = GoogleApiKey,
                PlaceId = placeId
            });

            if (!detailsResponse.Status.HasValue || detailsResponse.Status.Value != Status.Ok)
            {
                return(new BadRequestResult());
            }

            var detailResult = detailsResponse.Result;

            airportDetail.address = detailResult.FormattedAddress;
            airportDetail.phone   = detailResult.InternationalPhoneNumber;
            airportDetail.website = detailResult.Website;

            if (photoRef != null)
            {
                var photoResponse = await GooglePlaces.Photos.QueryAsync(new PlacesPhotosRequest
                {
                    Key            = GoogleApiKey,
                    PhotoReference = photoRef,
                    MaxWidth       = 400
                });

                if (photoResponse.Buffer != null)
                {
                    airportDetail.photo       = Convert.ToBase64String(photoResponse.Buffer);
                    airportDetail.photoCredit = photoCredit;
                }
            }

            return(new JsonResult(airportDetail));
        }
示例#2
0
        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.PhotoBuffer != null)
                {
                    airportDetail.Photo       = Convert.ToBase64String(photosResponse.PhotoBuffer);
                    airportDetail.PhotoCredit = photoCredit;
                }
            }
            return(new JsonResult(airportDetail));
        }
示例#3
0
        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 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 detailsResonse = await GooglePlaces.Details.QueryAsync(new PlacesDetailsRequest
            {
                Key     = GoogleApiKey,
                PlaceId = placeId
            });

            // Если мы не получили положительный ответ, или список результатов пуст, то уходим отсюда
            if (!detailsResonse.Status.HasValue || detailsResonse.Status.Value != Status.Ok)
            {
                return(new BadRequestResult());
            }

            // Установливаем детали
            var detailsResult = detailsResonse.Result;

            airportDetail.FormattedAddress = detailsResult.FormattedAddress;
            airportDetail.PhoneNumber      = detailsResult.InternationalPhoneNumber;
            airportDetail.Website          = detailsResult.Website;

            if (photoReference != null)
            {
                // Выполняем запрос к фотографии
                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));
        }