コード例 #1
0
        public async Task <IActionResult> Run([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req, ILogger log)
        {
            double lat = double.Parse(req.Query["lat"]);
            double lng = double.Parse(req.Query["lng"]);

            var origin = new GeoCoordinate(lat, lng);
            var jump   = 0.0025;

            // +1,-1 +1,0 +1,+1
            //  0,-1  oo   0,+1
            // -1,-1 -1,0 -1,+1

            var surroundingArea = new List <GeoCoordinate>
            {
                new GeoCoordinate(origin.Latitude + jump, origin.Longitude - jump),
                new GeoCoordinate(origin.Latitude + jump, origin.Longitude),
                new GeoCoordinate(origin.Latitude + jump, origin.Longitude + jump),
                new GeoCoordinate(origin.Latitude, origin.Longitude - jump),
                origin,
                new GeoCoordinate(origin.Latitude, origin.Longitude + jump),
                new GeoCoordinate(origin.Latitude - jump, origin.Longitude - jump),
                new GeoCoordinate(origin.Latitude - jump, origin.Longitude),
                new GeoCoordinate(origin.Latitude - jump, origin.Longitude + jump)
            };

            List <NearByResult> results = new List <NearByResult>();

            try
            {
                foreach (GeoCoordinate pos in surroundingArea)
                {
                    List <NearByResult> places = await GetNearByPlaces(pos.Latitude, pos.Longitude);

                    foreach (NearByResult place in places)
                    {
                        if (!results.Any(x => x.PlaceId == place.PlaceId))
                        {
                            results.Add(place);
                        }
                    }
                }

                foreach (NearByResult nearByResult in results)
                {
                    GooglePlacesPto googlePlacesPto = Context.GooglePlaces.Find(nearByResult.PlaceId);
                    if (googlePlacesPto != null)
                    {
                        // update
                        log.LogInformation($"Updating Google Places {googlePlacesPto.Id}");
                        nearByResult.Adapt(googlePlacesPto);
                        googlePlacesPto.Updated = DateTime.Now;
                        Context.GooglePlaces.Update(googlePlacesPto);
                    }
                    else
                    {
                        // insert
                        log.LogInformation($"Inserting new Google Places {nearByResult.PlaceId}");
                        googlePlacesPto = new GooglePlacesPto();
                        nearByResult.Adapt(googlePlacesPto);
                        googlePlacesPto.Updated = DateTime.Now;
                        PlacePto placePto = FindExistingPlace(log, lat, lng, googlePlacesPto.Name);
                        if (placePto == null)
                        {
                            placePto = new PlacePto
                            {
                                Location = new Point(googlePlacesPto.Longitude, googlePlacesPto.Latitude)
                                {
                                    SRID = 4326
                                },
                                Name         = googlePlacesPto.Name,
                                GooglePlaces = googlePlacesPto
                            };
                            Context.Places.Add(placePto);
                        }
                        googlePlacesPto.Place = placePto;
                        Context.GooglePlaces.Add(googlePlacesPto);
                    }
                    Context.SaveChanges();
                    await RemoveCachedPlace(log, googlePlacesPto.PlaceId.ToString());
                }

                Context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw;
            }

            return(new OkResult());
        }