public async Task GeocodeSettlementWithoutStreet() { var request = new GeocoderRequest { Request = "Ярославская область Октябрьский 12" }; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var firstGeoObject = response.GeoObjectCollection.FeatureMember.FirstOrDefault(); var coordinate = firstGeoObject.GeoObject.Point.Pos; var addressComponents = firstGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var country = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Country)); var province = addressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var area = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Area)); var city = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); var street = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Street)); var house = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.House)); Assert.Equal("Россия", country.Name); Assert.Equal("Ярославская область", province.Name); Assert.Equal("Рыбинский район", area.Name); Assert.Equal("поселок Октябрьский", city.Name); Assert.Null(street); Assert.Equal("12", house.Name); Assert.Equal("39.110177 57.984794", coordinate); }
public async Task GeocodeCity() { var request = new GeocoderRequest { Request = "Ярославль серова 13" }; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var firstGeoObject = response.GeoObjectCollection.FeatureMember.FirstOrDefault(); var coordinate = firstGeoObject.GeoObject.Point.Pos; var addressComponents = firstGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var country = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Country)); var province = addressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var area = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Area)); var city = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); var street = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Street)); var house = addressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.House)); Assert.Equal("Россия", country.Name); Assert.Equal("Ярославская область", province.Name); Assert.Equal("городской округ Ярославль", area.Name); Assert.Equal("Ярославль", city.Name); Assert.Equal("улица Серова", street.Name); Assert.Equal("13", house.Name); Assert.Equal("39.802311 57.608719", coordinate); }
private async Task <bool> UpdateCoordinate(Pharmacy pharmacy) { var query = YekaterinburgRegionPrefix + pharmacy.City + ", " + pharmacy.Address; var request = new GeocoderRequest { Request = query }; var apiKey = _configuration["GeoCoderApiKey"]; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var geoObjects = response.GeoObjectCollection.FeatureMember; if (geoObjects.Count == 0) { _log.Error($"No geoobjects by query {query} - Id {pharmacy.Id}"); return(false); } var geoObjectFromSelectedCity = geoObjects .FirstOrDefault(e => e.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components .FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)) ?.Name == pharmacy.City); var geoObject = geoObjectFromSelectedCity ?? geoObjects.First(); if (geoObjects.Count > 1 && geoObjectFromSelectedCity == null) { _log.Warn($"Multiple geoobjects ({geoObjects.Count}) and No one from city {pharmacy.City} by query {query} - Id {pharmacy.Id}."); } var coordinate = geoObject.GeoObject.Point.Pos; var formattedAddress = geoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Formatted; pharmacy.FormattedAddress = formattedAddress.Replace("Россия, Свердловская область, ", string.Empty); try { UpdateCoordinates(coordinate, pharmacy); return(true); } catch (Exception exception) { _log.Error(exception, $"query {query} - Id {pharmacy.Id}"); return(false); } }
public async Task ParseCoordinates() { var request = new GeocoderRequest { Request = "Ярославская область Октябрьский 12" }; var client = new GeocoderClient(apiKey); var response = await client.Geocode(request); var firstGeoObject = response.GeoObjectCollection.FeatureMember.FirstOrDefault(); var strCoordinate = firstGeoObject.GeoObject.Point.Pos; var coordinate = strCoordinate.ToCoordinate(); Assert.Equal("39.110177 57.984794", strCoordinate); Assert.Equal(57.984794, coordinate.Latitude); Assert.Equal(39.110177, coordinate.Longitude); }
/// <summary> /// 逆地址解析 /// </summary> /// <param name="request"></param> /// <returns></returns> public GeocoderResponse GetAddressBylnglat(GeocoderRequest request) { var response = new GeocoderResponse() { StateCode = 201, ResultMsg = "查无数据" }; if (request == null || request.Location == null || request.Location == "") { response.ResultMsg = "请求参数异常,请稍后重试"; return(response); } var url = $"https://apis.map.qq.com/ws/geocoder/v1/?location={request.Location}&key={key}"; var resultPost = new HttpClient().GetAsync(url).Result; if (resultPost == null || !resultPost.IsSuccessStatusCode && resultPost.Content == null) { response.ResultMsg = "网络异常,请稍后重试"; return(response); } var resultStr = resultPost.Content.ReadAsStringAsync().Result; if (resultStr == null || resultStr == "") { response.ResultMsg = "网络异常,请稍后重试"; return(response); } var result = JsonConvert.DeserializeObject <Geocoder>(resultStr); if (result == null || result.status != 0 || result.result == null || result.result.location == null) { response.ResultMsg = "网络异常,请稍后重试"; return(response); } response.Address = result.result.address; response.Lat = result.result.location.lat; response.Lng = result.result.location.lng; response.StateCode = 200; response.ResultMsg = "查询成功"; return(response); }
public async Task NotFindGeoObjectInBordersArea() { var firstRequest = new GeocoderRequest { Request = "Песочное", BordersArea = new BoxArea { LowerLatitude = 59.600000, LowerLongitude = 41.641063, UpperLatitude = 59.700000, UpperLongitude = 41.666459 }, IsRestrictArea = true }; var client = new GeocoderClient(apiKey); var firstResponse = await client.Geocode(firstRequest); var firstResponseGeoObject = firstResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); Assert.Null(firstResponseGeoObject); }
public async Task FindGeoObjectInBordersArea() { var firstRequest = new GeocoderRequest { Request = "Песочное", BordersArea = new BoxArea { LowerLatitude = 59.440733, LowerLongitude = 39.641063, UpperLatitude = 59.457845, UpperLongitude = 39.666459 }, IsRestrictArea = true }; var client = new GeocoderClient(apiKey); var firstResponse = await client.Geocode(firstRequest); var firstResponseGeoObject = firstResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); var firstResponseAddressComponents = firstResponseGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var firstResponseProvince = firstResponseAddressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var firstResponseLocality = firstResponseAddressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); Assert.Equal("Вологодская область", firstResponseProvince.Name); Assert.Equal("поселок Песочное", firstResponseLocality.Name); }
public async Task <ICollection <AddressEM> > Geocode(string request, int maxResultCount = 5) { var result = new List <AddressEM>(); var clientRequest = new GeocoderRequest { Request = request, MaxCount = maxResultCount }; var client = new GeocoderClient(); var clientResponse = await client.Geocode(clientRequest); foreach (var geoFeatureMember in clientResponse.GeoObjectCollection.FeatureMember) { var geoObject = geoFeatureMember.GeoObject; var address = CreateAddressByGeoObject(geoObject); result.Add(address); } return(result); }
public async Task RestrictSearchArea() { var firstRequest = new GeocoderRequest { Request = "Песочное", SearchArea = new Area { Latitude = 59.300000, Longitude = 39.500000, LatitudeSpan = 0.500000, LongitudeSpan = 0.500000 }, IsRestrictArea = true }; var client = new GeocoderClient(apiKey); var firstResponse = await client.Geocode(firstRequest); var firstResponseGeoObject = firstResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); var firstResponseAddressComponents = firstResponseGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var firstResponseProvince = firstResponseAddressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var firstResponseLocality = firstResponseAddressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); var secondRequest = new GeocoderRequest { Request = "Песочное", SearchArea = new Area { Latitude = 58.00400, Longitude = 39.200000, LatitudeSpan = 0.500000, LongitudeSpan = 0.500000 }, IsRestrictArea = true }; var secondResponse = await client.Geocode(secondRequest); var secondResponseGeoObject = secondResponse.GeoObjectCollection.FeatureMember.FirstOrDefault(); var secondResponseAddressComponents = secondResponseGeoObject.GeoObject.MetaDataProperty.GeocoderMetaData.Address.Components; var secondResponseProvince = secondResponseAddressComponents.LastOrDefault(c => c.Kind.Equals(AddressComponentKind.Province)); var secondResponseLocality = secondResponseAddressComponents.FirstOrDefault(c => c.Kind.Equals(AddressComponentKind.Locality)); Assert.Equal("Вологодская область", firstResponseProvince.Name); Assert.Equal("поселок Песочное", firstResponseLocality.Name); Assert.Equal("Ярославская область", secondResponseProvince.Name); Assert.Equal("поселок Песочное", secondResponseLocality.Name); }
public string GetAddressBylnglat([FromBody] GeocoderRequest request) { var result = new MapServices(_context).GetAddressBylnglat(request); return(JsonConvert.SerializeObject(result)); }