Exemplo n.º 1
0
        public static async Task <Point> GeocodeAsync(string location, GeocodingApi apiType)
        {
            Point result;

            switch (apiType)
            {
            case GeocodingApi.Yandex:
                var codingResult = await YandexGeocoder.GeocodeAsync(location);

                if (!codingResult.Any())
                {
                    return(null);
                }
                var firstPoint = codingResult.First().Point;
                result = new Point
                {
                    Latitude  = firstPoint.Latitude,
                    Longitude = firstPoint.Longitude
                };
                break;

            case GeocodingApi.Google:
                var geocoder = new GoogleGeocoder();
                lock (KeyLock)
                {
                    if (!string.IsNullOrEmpty(_googleApiKey))
                    {
                        geocoder.ApiKey = _googleApiKey;
                    }
                }
                IEnumerable <GoogleAddress> addresses;
                try
                {
                    addresses = await geocoder.GeocodeAsync(location);
                }
                catch (Exception ex)
                {
                    Logger.LogException("GOOGLE GEO", LogLevel.Debug, ex);
                    return(null);
                }

                var firstAddress = addresses?.FirstOrDefault();
                if (firstAddress == null)
                {
                    return(null);
                }
                result = new Point
                {
                    Latitude  = firstAddress.Coordinates.Latitude,
                    Longitude = firstAddress.Coordinates.Longitude
                };

                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(apiType), apiType, null);
            }
            return(result);
        }
Exemplo n.º 2
0
        public static void SetApiKey(string key, GeocodingApi apiType)
        {
            if (string.IsNullOrEmpty(key))
            {
                return;
            }
            switch (apiType)
            {
            case GeocodingApi.Yandex:
                YandexApiKey = key;
                break;

            case GeocodingApi.Google:
                GoogleApiKey = key;
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(apiType), apiType, null);
            }
        }
Exemplo n.º 3
0
 public void Init()
 {
     instance = new GeocodingApi();
 }