Exemple #1
0
        private static GeocodePoint GetGeoDataFromVirtualEarth(string geoString)
        {
            GeocodePoint geocodePoint = null;

            try
            {
                var geoServiceUrl = string.Format(GeoCodeApi, geoString);
                var client        = new WebClient();
                var str           = client.DownloadString(geoServiceUrl);
                var xml           = XElement.Parse(str);

                geocodePoint = new GeocodePoint();
                var pointNode = xml.Descendants(VeNameSpace + "Point").FirstOrDefault();
                var latNode   = pointNode != null?pointNode.Descendants(VeNameSpace + "Latitude").FirstOrDefault() : null;

                var lonNode = pointNode != null?pointNode.Descendants(VeNameSpace + "Longitude").FirstOrDefault() : null;

                if (latNode != null && lonNode != null)
                {
                    geocodePoint.Latitude  = double.Parse(latNode.Value);
                    geocodePoint.Longitude = double.Parse(lonNode.Value);
                }
            }
            catch
            {
            }

            return(geocodePoint);
        }
Exemple #2
0
        public static Tuple <bool, string> IsCloRegion(string locationId)
        {
            if (!CloLocationDatas.Any())
            {
                BuildCoordinateTable();
            }

            Tuple <bool, string> tuple = new Tuple <bool, string>(false, string.Empty);

            if (locationId.Contains("us:postal:"))
            {
                locationId = locationId.Replace("us:postal:", "");
            }
            bool isCloRegion = CloLocations.Contains(locationId);

            if (isCloRegion)
            {
                foreach (CloGeocodepoint cloGeocodepoint in cloGeocodepoints)
                {
                    if (cloGeocodepoint.LocationId == locationId)
                    {
                        tuple = IsCloRegion(cloGeocodepoint.GeocodePoint.Latitude, cloGeocodepoint.GeocodePoint.Longitude);
                        break;
                    }
                }
            }

            if (!isCloRegion && !NonCloLocations.Contains(locationId))
            {
                GeocodePoint geocodePoint = GetGeoDataFromVirtualEarth(locationId);
                if (geocodePoint != null)
                {
                    tuple = IsCloRegion(geocodePoint.Latitude, geocodePoint.Longitude);
                    if (tuple.Item1)
                    {
                        cloGeocodepoints.Add(new CloGeocodepoint()
                        {
                            LocationId   = locationId,
                            GeocodePoint = geocodePoint
                        });
                        CloLocations.Add(locationId);
                    }
                    else
                    {
                        NonCloLocations.Add(locationId);
                    }
                }
                else
                {
                    Console.WriteLine("Unable to determine Lat,Lon for location {0}", locationId);
                }
            }

            return(tuple);
        }