public async Task <ContentResult> Get(string id) { if (string.IsNullOrEmpty(id)) { return(Content(JsonConvert.SerializeObject(new PlaceDetailsResponse { IsSucess = false }), "application/json")); } string googleApiUrl = $"https://maps.googleapis.com/maps/api/place/details/json?key={_Settings.PlacesApiKey}"; GooglePlaceDetailsResult googlePlaceDetailsResult; WebRequest googlePlaceDetailRequest = WebRequest.Create($"{googleApiUrl}&placeid={id}"); using (WebResponse webResponse = await googlePlaceDetailRequest.GetResponseAsync()) { using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream())) { string jsonResponse = streamReader.ReadToEnd(); googlePlaceDetailsResult = await _GoogleJsonParser.ParsePlacesDetailsAsync(jsonResponse); } if (!googlePlaceDetailsResult.IsSucess) { return(Content(JsonConvert.SerializeObject(new PlaceDetailsResponse { IsSucess = false }), "application/json")); } PlaceDetailsResponse response = new PlaceDetailsResponse { Place = googlePlaceDetailsResult.PlaceDetails, IsSucess = true }; return(Content(JsonConvert.SerializeObject(response), "application/json")); } }
private void searchBox_QuerySubmitted(AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args) { if (addressToId.ContainsKey(searchBox.Text)) { PlaceDetailsRequest placeDetailsRequest = new PlaceDetailsRequest(); placeDetailsRequest.PlaceID = addressToId[searchBox.Text]; PlaceDetailsResponse placeDetailsResponse = placeDetailsService.GetResponse(placeDetailsRequest); if (placeDetailsResponse.Status == Google.Maps.ServiceResponseStatus.Ok) { reverseGeocode = false; map.Center = new Geopoint(new BasicGeoposition() { Latitude = placeDetailsResponse.Result.Geometry.Location.Latitude, Longitude = placeDetailsResponse.Result.Geometry.Location.Longitude }); } } }
public PlaceDetailsResponse GetPlaceDetailsById(string placeId) { // Initialize API request with URL and API key RestRequest request = new RestRequest("/details/json?", Method.GET); request.AddQueryParameter("key", _apiKey); request.AddQueryParameter("placeid", placeId); // Only pull name, business hours, reviews, and address data request.AddQueryParameter("fields", "geometry,name,opening_hours,reviews,address_component,formatted_phone_number"); // Get responses from API using above request IRestResponse response = _client.Execute(request); // Convert JSON response to model and return response PlaceDetailsResponse apiResponse = JsonConvert.DeserializeObject <PlaceDetailsResponse>(response.Content); return(apiResponse); }
private async Task <Position> RequestDetails(AutocompletePlace place) { PlaceDetailsResponse details = await _placeAutocompleteService.GetPlaceDetails(place.Place_ID, _cancellationTokenSource.Token); PinPlaceDetails = details; // set details so viewmodel can use the coordinates var lat = details?.result?.geometry?.location?.lat; var lng = details?.result?.geometry?.location?.lng; if (lat != null && lng != null) { return(new Position( details.result.geometry.location.lat, details.result.geometry.location.lng )); } else { return(default(Position)); } }
/** * Add or update venue details in database */ private void UpdateVenueDetails() { // Get all venues List <Venue> venues = _venueRepository.GetAllVenues(); // Get only venues that don't have details List <Venue> venuesWithoutDetails = venues.Where(v => v.Address1 == null).ToList(); foreach (var venue in venuesWithoutDetails) { // Venue owners have ability to manually update their venues, do not auto-update if (VenueHasOwner(venue)) { continue; } // Get Place details from Google API using GooglePlaceId PlaceDetailsResponse venueDetails = _placesApi.GetPlaceDetailsById(venue.GooglePlaceId); // Map response data to database model properties venue.Phone = venueDetails.Result.FormattedPhoneNumber; string streetAddress = null; foreach (var addressComponent in venueDetails.Result.AddressComponents) { // Map Address from response to database model var type = addressComponent.Types.FirstOrDefault(); switch (type) { case "street_number": streetAddress += addressComponent.ShortName + " "; break; case "route": streetAddress += addressComponent.ShortName; break; case "locality": venue.City = addressComponent.ShortName; break; case "administrative_area_level_1": venue.State = addressComponent.ShortName; break; case "postal_code": venue.ZipCode = addressComponent.ShortName; break; default: continue; } venue.Address1 = streetAddress; } // Update Venue entity _venueRepository.Edit(venue); // Map OpeningHours API response to BusinessHours entity if (venueDetails.Result.OpeningHours != null) { // Initialize new BusinessHours entity using VenueID as foreign key BusinessHours hours = new BusinessHours { VenueId = venue.VenueId }; foreach (var period in venueDetails.Result.OpeningHours.Periods) { hours.DayOfWeek = period.Open.Day; string openTime = period.Open?.Time.Insert(2, ":"); if (!string.IsNullOrEmpty(openTime)) { hours.OpenTime = DateTime.Parse(openTime).TimeOfDay; } string closeTime = period.Close?.Time.Insert(2, ":"); if (!string.IsNullOrEmpty(closeTime)) { hours.CloseTime = DateTime.Parse(closeTime).TimeOfDay; } // Add BusinessHours entity _businessHoursRepository.AddBusinessHours(hours); } } // Map Review API response to Review database entity if (venueDetails.Result.Reviews != null) { foreach (var review in venueDetails.Result.Reviews) { DateTime timestamp = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc); Review reviewEntity = new Review { IsGoogleReview = true, GoogleAuthor = review.AuthorName, VenueId = venue.VenueId, Comments = review.Text, Rating = review.Rating, Timestamp = timestamp.AddSeconds(review.Time).ToLocalTime() }; _reviewRepository.AddReview(reviewEntity); } } // Map Location API response to Location database entity if (venueDetails.Result.Geometry != null) { Location locationEntity = new Location { Latitude = venueDetails.Result.Geometry.GeometryLocation.Latitude.ToString(CultureInfo.InvariantCulture), Longitude = venueDetails.Result.Geometry.GeometryLocation.Longitude.ToString(CultureInfo.InvariantCulture), VenueId = venue.VenueId }; // Add Location entity _locationRepository.AddLocation(locationEntity); } } }