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); }
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); } }
public void Init() { instance = new GeocodingApi(); }