Example #1
0
        public static void Main(string[] args)
        {
            Console.WriteLine("Press anything to start calling API");
            var ok = Console.ReadKey();

            GoogleGeocodingAPI.GoogleAPIKey = GooglePlacesAPI.GoogleAPIKey = TesterRes.GoogleAPIKey; // create your own TesterRes resource and put your google api there


            //GEOCODING

            var result1   = Task.Run(async() => await GoogleGeocodingAPI.SearchAddressAsync("100 Market St, Southbank")).Result;
            var location1 = result1.Results.FirstOrDefault().Geometry.Location;


            var location2 = Task.Run(async() => await GoogleGeocodingAPI.GetCoordinatesFromAddressAsync("322 Kings Way, South Melbourne VIC 3205")).Result;
            var result2   = Task.Run(async() => await GoogleGeocodingAPI.GetAddressFromCoordinatesAsync(location2.Item1, location2.Item2)).Result;

            var distanceBetweenThem = Helper.GetDistance(location1.Lat, location2.Item1, location1.Lng, location2.Item2);
            var midlePoint          = Helper.GetMiddleCoordinates(location1.Lat, location2.Item1, location1.Lng, location2.Item2);
            var midlePointLocation  = Task.Run(async() => await GoogleGeocodingAPI.GetAddressFromCoordinatesAsync(midlePoint.Item1, midlePoint.Item2)).Result;


            //PLACES

            var result4 = Task.Run(async() => await GooglePlacesAPI.SearchPlaceNearBy(lat: midlePoint.Item1, lng: midlePoint.Item2, radiusInMeters: distanceBetweenThem * 1000, type: GuigleAPI.Model.PlaceType.liquor_store)).Result;

            var result5 = Task.Run(async() => await GooglePlacesAPI.SearchPlaceNearBy(query: "100 Market St, Southbank")).Result;
        }
Example #2
0
        public static void GetPlacesData(City nyc)
        {
            GooglePlacesAPI   api             = new GooglePlacesAPI();
            string            file            = Path.Combine(NYCConst.Places_Dir, "nycplaces.json");
            var               tracts          = nyc.Select(t => new { id = t.UID, lng = t.Longitude, lat = t.Latitude, radius = t.Length / 6.562 });
            List <RatingData> tractPopularity = new List <RatingData>();

            Progress progress = new Progress(1000, tracts.Count());

            progress.Start();
            do
            {
                Parallel.ForEach(tracts, tract => {
                    GooglePlacesAPI.Place[] places = null;
                    Task.Run(async() => {
                        places = await api.GetPlacesAsync(tract.lat, tract.lng, tract.radius);
                    }).Wait();

                    RatingData data   = new RatingData();
                    float ratingTotal = 0;
                    int userTotal     = 0;
                    int placesCount   = 0;
                    foreach (var place in places)
                    {
                        if (place.User_ratings_total > 0)
                        {
                            ratingTotal += place.Rating * place.User_ratings_total;
                            userTotal   += place.User_ratings_total;
                            placesCount++;
                        }
                    }
                    data.id          = tract.id;
                    data.Rating      = (userTotal > 0) ? ratingTotal / userTotal : 0;
                    data.TotalUsers  = userTotal;
                    data.PlacesCount = placesCount;
                    tractPopularity.Add(data);
                    progress.inc();
                });
                tracts = tracts.Where(x => !tractPopularity.Select(i => i.id).Contains(x.id));
            }while (tracts.Count() > 0);

            progress.Stop();
            Console.WriteLine("Processed {0} places", tractPopularity.Count());
            File.WriteAllTextAsync(file, JsonConvert.SerializeObject(tractPopularity));
        }
Example #3
0
        //getting stuff for the kitchen
        public async Task <GooglePlacesAPI> GetPlaces(string GetRequestURL)
        {
            HttpClient client = new HttpClient();

            HttpResponseMessage response = await client.GetAsync(GetRequestURL);

            if (response.IsSuccessStatusCode)
            {
                string json = await response.Content.ReadAsStringAsync();

                GooglePlacesAPI googlePlacesAPI = JsonConvert.DeserializeObject <GooglePlacesAPI>(json);

                return(googlePlacesAPI);
            }
            else
            {
                return(null);
            }
        }
Example #4
0
        //NAMESPACE CONFLICT ERROR
        private void Mapresultphotocollection(GooglePlacesAPI place)
        {
            foreach (Result result in place.results)
            {
                var restaurantwhosphotosbelongtoguid = _context.Restaurants.Where(r => r.Place_Id == result.place_id).Select(re => re.RestaurantModelPrimaryKey).FirstOrDefault();

                foreach (Photo individualphoto  in result.Photos)
                {
                    PhotosFromGoogle photoToSAve = new PhotosFromGoogle();
                    photoToSAve.Height          = individualphoto.height;
                    photoToSAve.Width           = individualphoto.width;
                    photoToSAve.Photo_reference = individualphoto.photo_reference;
                    photoToSAve.RestaurantGuid  = restaurantwhosphotosbelongtoguid;
                    //loop through all the photos.
                    _context.PhotosFromGoogle.Add(photoToSAve);
                }
            }
            _context.SaveChanges();
        }
Example #5
0
    public static async Task promptGoogle(string hitGoogleWith)
    {
        Console.WriteLine("Enter your zipcode");
        string input = Console.ReadLine();

        string Googlekey = "AIzaSyAfdlKioHIQ6X06IfAoNa22KtU1t35Xd_A";
        string result    = await Prompter.getUrl("https://maps.googleapis.com/maps/api/geocode/json?address=" + input + "=" + Googlekey);

        Google g      = JsonConvert.DeserializeObject <Google>(result);
        double lat    = g.results.ElementAt(0).geometry.location.lat;
        double lng    = g.results.ElementAt(0).geometry.location.lng;
        string LatLng = (lat.ToString() + "," + lng.ToString());

        IJSONAPI googapi = new GooglePlacesAPI();

        Google place = await googapi.GetData <Google>(hitGoogleWith, Googlekey, LatLng);

        Console.WriteLine($"Here's a location nearby that serves your favorite search term: {LatLng}");
        // Console.WriteLine(googapi.ToJSON(place));
        if (place.results.Count > 0)
        {
            Console.WriteLine(place.results[0].name);
        }
    }
Example #6
0
        private async void MapGoogleresultToRestaurantModel(Dictionary <string, string> param, GooglePlacesAPI placesObject, string url)
        {
            List <string> placeIds            = new List <string>();
            Dictionary <string, string> idAdd = new Dictionary <string, string>();

            foreach (Result result in placesObject.results)
            {
                RestaurantModel restaurant = new RestaurantModel();
                restaurant.RestaurantName  = result.name;
                restaurant.PriceRangeIndex = result.price_level;
                restaurant.Open_now        = result.opening_hours.open_now;
                restaurant.Lat             = result.geometry.location.lat;
                restaurant.Lng             = result.geometry.location.lng;
                restaurant.Price_level     = result.price_level;
                restaurant.Rating          = result.rating;
                restaurant.Place_Id        = result.place_id;

                placeIds.Add(result.place_id);
                idAdd.Add(result.place_id, result.formatted_address);

                _context.Restaurants.Add(restaurant);
                //save place id to use in search by id query
                //map addressKey table to Restaurant model address key
                //photos mapped at different time
            }
            _context.SaveChanges();

            List <string> resaurantGuid = new List <string>();

            foreach (string id in placeIds)
            {
                var guid = _context.Restaurants.Where(r => r.Place_Id == id).Select(r => r.RestaurantModelPrimaryKey).FirstOrDefault();
                resaurantGuid.Add(guid);
            }

            MapQueriesToSearchJunctionTable(param, resaurantGuid);


            Mapresultphotocollection(placesObject);


            foreach (string id in placeIds)
            {
                RestaurantModel restaurant = _context.Restaurants.Where(r => r.Place_Id == id).FirstOrDefault();
                GooglePlacesAPI_PlaceIDSearchResults place = await GetResultsById(id);

                restaurant.RestaurantPhone = place.result.formatted_phone_number;
                restaurant.WebsiteUrl      = place.result.website;
                string addr = idAdd[id];
                MapAddressToLocalDb(id, addr); // Adds the new addresses to the context;
            }

            _context.SaveChanges();
        }
Example #7
0
 private Controller()
 {
     _placesAPI = new GooglePlacesAPI();
     _view      = View.Instance;
 }