示例#1
0
        public async Task <IEnumerable <Place> > GetNearbyPlacesAsync(Geocode g, string keyword)
        {
            // from: https://developers.google.com/places/documentation/search
            // e.g. https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=46,-122&rankby=distance&keyword=coffee&key=AddYourOwnKeyHere
            string request = string.Format("https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location={0},{1}&rankby=distance&keyword={2}&key={3}", g.Latitude, g.Longitude, keyword, GetGoogleApiKey());
            var    xml     = await(new HttpClient()).GetStringAsync(request);
            var    results = XDocument.Parse(xml).Element("PlaceSearchResponse").Elements("result");

            var places = new List <Place>();

            foreach (var result in results)
            {
                var loc  = result.Element("geometry").Element("location");
                var icon = result.Element("icon").Value;
                places.Add(new Place {
                    Name     = result.Element("name").Value,
                    Icon     = !string.IsNullOrWhiteSpace(icon) ? new Uri(icon) : null,
                    Vicinity = result.Element("vicinity").Value,
                    Location = new Geocode {
                        Latitude  = double.Parse(loc.Element("lat").Value),
                        Longitude = double.Parse(loc.Element("lng").Value),
                    },
                });
            }

            return(places);
        }
示例#2
0
        public async Task <string> GetAddressForLocationAsync(Geocode loc)
        {
            // from https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding
            // e.g. https://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452
            string request = string.Format("https://maps.googleapis.com/maps/api/geocode/xml?latlng={0},{1}", loc.Latitude, loc.Longitude);
            var    xml     = await(new HttpClient()).GetStringAsync(request);
            var    address = XDocument.Parse(xml).Element("GeocodeResponse").Element("result").Element("formatted_address").Value;

            return(address);
        }
示例#3
0
        public Geocode GetGreatCircleMidpoint(Geocode g1, Geocode g2)
        {
            // from http://stackoverflow.com/questions/6830959/c-sharp-find-midpoint-of-two-latitude-longitudes

            // convert to radians
            var dLon = deg2rad(g2.Longitude - g1.Longitude);
            var lat1 = deg2rad(g1.Latitude);
            var lat2 = deg2rad(g2.Latitude);
            var lon1 = deg2rad(g1.Longitude);

            // calculate the great circle midpoint
            var Bx   = Math.Cos(lat2) * Math.Cos(dLon);
            var By   = Math.Cos(lat2) * Math.Sin(dLon);
            var lat3 = Math.Atan2(Math.Sin(lat1) + Math.Sin(lat2), Math.Sqrt((Math.Cos(lat1) + Bx) * (Math.Cos(lat1) + Bx) + By * By));
            var lon3 = lon1 + Math.Atan2(By, Math.Cos(lat1) + Bx);

            // convert to degrees
            return(new Geocode {
                Latitude = rad2deg(lat3),
                Longitude = rad2deg(lon3),
            });
        }