Exemple #1
0
        public Dictionary <string, double> GetCoordinates(string location)
        {
            string address = location;

            IGeocoder geocoder = new BingMapsGeocoder(_apiKey);
            var       result   = new Dictionary <string, double>();

            try
            {
                IEnumerable <Address> response = geocoder.Geocode(location);

                if (response != null)
                {
                    result.Add("lat", response.First().Coordinates.Latitude);
                    result.Add("long", response.First().Coordinates.Longitude);

                    Debug.WriteLine("Location: " + location);
                    Debug.WriteLine("Lat: " + result["lat"]);
                    Debug.WriteLine("Long: " + result["long"]);
                }
                ;
            } catch (Exception e)
            {
                Debug.WriteLine("Exception was thrown while processing location: " + location);
                Debug.WriteLine(e.Message);
                result.Add("lat", 0);
                result.Add("long", 0);
            }


            return(result);
        }
Exemple #2
0
        // Geocodes an address using the Bing Maps engine
        public static Location SearchBing(string add)
        {
            try
            {
                // Calls the Werservice
                BingMapsGeocoder      geocoder  = new BingMapsGeocoder(ConfigurationManager.AppSettings["BingApiKey"]);
                IEnumerable <Address> addresses = geocoder.Geocode(add);

                // Selects the firt result
                BingAddress b = (BingAddress)addresses.FirstOrDefault();

                Location r = new Location();

                r.Lat     = addresses.FirstOrDefault().Coordinates.Latitude;
                r.Lon     = addresses.FirstOrDefault().Coordinates.Longitude;
                r.Quality = b.Confidence.ToString();
                r.Address = addresses.FirstOrDefault().FormattedAddress;

                return(r);
            }
            catch (Exception Ex)
            {
                throw new Exception(Ex.Message);
            }
        }
        public string GetLatLonFromAddress(string address)
        {
            Func <string> getCordsFromAddress = delegate()
            {
                IGeocoder googleGeoCoder = new GoogleGeocoder()
                {
                    ApiKey = Config.MappingConfig.GoogleMapsApiKey
                };
                IGeocoder bingGeoCoder = new BingMapsGeocoder(Config.MappingConfig.BingMapsApiKey);
                string    coordinates  = null;

                try
                {
                    var addresses = googleGeoCoder.Geocode(address);

                    if (addresses != null && addresses.Any())
                    {
                        var firstAddress = addresses.First();

                        coordinates = string.Format("{0},{1}", firstAddress.Coordinates.Latitude, firstAddress.Coordinates.Longitude);
                    }
                }
                catch
                { }

                if (string.IsNullOrWhiteSpace(coordinates))
                {
                    try
                    {
                        var coords = GetLatLonFromAddressLocationIQ(address);

                        if (coords != null)
                        {
                            coordinates = string.Format("{0},{1}", coords.Latitude, coords.Longitude);
                        }
                    }
                    catch
                    { }
                }

                if (string.IsNullOrWhiteSpace(coordinates))
                {
                    try
                    {
                        var addresses = bingGeoCoder.Geocode(address);

                        if (addresses != null && addresses.Count() > 0)
                        {
                            coordinates = string.Format("{0},{1}", addresses.First().Coordinates.Latitude, addresses.First().Coordinates.Longitude);
                        }
                    }
                    catch
                    { }
                }

                return(coordinates);
            };

            return(_cacheProvider.Retrieve <string>(string.Format(ForwardCacheKey, address.GetHashCode()), getCordsFromAddress, CacheLength));
        }
Exemple #4
0
        // Geocodes an address using the Bing Maps engine
        public static Location Geocode_Bing(string ADDRESS)
        {
            try
            {
                // Calls the Werservice
                BingMapsGeocoder      GEOCODER  = new BingMapsGeocoder(ConfigurationManager.AppSettings["BingApiKey"]);
                IEnumerable <Address> ADDRESSES = GEOCODER.Geocode(ADDRESS);

                Location R = new Location();

                // Checks if the process returned a valid result
                if (ADDRESSES.Count() > 0)
                {
                    // Selects the firt result
                    BingAddress B = (BingAddress)ADDRESSES.FirstOrDefault();

                    R.Lat        = B.Coordinates.Latitude;
                    R.Lon        = B.Coordinates.Longitude;
                    R.Address    = B.AddressLine;      // Street + Number
                    R.City       = B.Locality;         // City
                    R.State      = B.AdminDistrict;    // State
                    R.PostalCode = B.PostalCode;       // Postal Code
                    R.Quality    = B.Confidence.ToString();
                    R.Complete   = B.FormattedAddress; // Full Address
                }
                else
                {
                    R.Lat     = 0;
                    R.Lon     = 0;
                    R.Quality = "Bad";
                }

                return(R);
            }
            catch (Exception ERROR)
            {
                throw new Exception(ERROR.Message);
            }
        }
Exemple #5
0
        /// <summary>
        /// Método para visualizar localização passada como parametro no construtor
        /// </summary>
        private void SetLocation()
        {
            //Instanciando objeto para recuperar as cordenadas
            IGeocoder geocoder = new BingMapsGeocoder(
                "AsHgFB0MOC02SgIYNbIwV9WOuo94eLp3brN5PvlD9Vu-p9DSjVUYfUZZIS5jfOeb"
                );
            IEnumerable <Address> results = geocoder.Geocode(this.location);

            //Recuperando primeiro endereco
            loc = new Microsoft.Maps.MapControl.WPF.Location();
            foreach (Address a in results)
            {
                loc = new Microsoft.Maps.MapControl.WPF.Location(
                    a.Coordinates.Latitude,
                    a.Coordinates.Longitude
                    );
                break;
            }

            //Visualizando coordenadas encontradas no mapa com zoom proprio
            bingMap.SetView(loc, zoom);    //Visualização em mapa
            bingMarker.Location = loc;     //Marcador de destino
        }
Exemple #6
0
 public void ApplyCulture(string address, string culture, string result)
 {
     geoCoder.Culture = culture;
     BingAddress[] addresses = geoCoder.Geocode(address).ToArray();
     Assert.Equal(result, addresses[0].FormattedAddress);
 }