【发布时间】:2015-10-22 18:32:45
【问题描述】:
我已注册 mscodechallenge.net,目前正在参加 Bing 地图挑战。我目前有一个问题,但是得到了正确的结果。
要通过包含的单元测试,我需要检索地址“Frydenlunds Alle 6 2950 DK”的坐标(55.8408508、12.5594797)(纬度、经度)以通过测试,但是我当前的代码返回的小数点太少无法通过。我目前的结果是 (55.84085, 12,55948)。是否可以在 Bing Maps API 返回的结果中说明小数位数?我尝试过使用可用的查询和非结构化 URL,但结果相同。
我当前的代码和服务生成的 XML:
C#
public double[] Execute(string addressLine, string postalCode, string countryCode)
{
var bingKey = "anonymised";
var queryUrl = CreateURL(addressLine, countryCode, postalCode, bingKey);
WebClient client = new WebClient();
Stream data = client.OpenRead(queryUrl);
StreamReader reader = new StreamReader(data);
string result = reader.ReadToEnd();
data.Close();
reader.Close();
using (XmlReader xmlReader = XmlReader.Create(new StringReader(result)))
{
xmlReader.ReadToFollowing("Latitude");
var lat = xmlReader.ReadElementContentAsDouble();
xmlReader.ReadToFollowing("Longitude");
var lon = xmlReader.ReadElementContentAsDouble();
double[] resultArray = new double[] { lat, lon };
return resultArray;
}
}
private string CreateURL(string address, string countrycode, string postalcode, string bingKey)
{
return string.Format(
GetUrl(),
Uri.EscapeDataString(address),
Uri.EscapeDataString(postalcode),
Uri.EscapeDataString(countrycode),
Uri.EscapeDataString(bingKey));
}
private string GetUrl()
{
//return "http://dev.virtualearth.net/REST/v1/Locations?countryRegion={2}&postalCode={1}&addressLine={0}&includeNeighborhood=0&maxResults=10&key={3}&output=xml";
return "http://dev.virtualearth.net/REST/v1/Locations?q={0}, {1}, {2}&key={3}&output=xml";
}
来自服务的 XML 响应:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<StatusCode>200</StatusCode>
<StatusDescription>OK</StatusDescription>
<AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>1</EstimatedTotal>
<Resources>
<Location>
<Name>Frydenlunds Allé, 2950 Vedbæk, Denmark</Name>
<Point>
<Latitude>55.84085</Latitude>
<Longitude>12.55948</Longitude>
</Point>
<BoundingBox>
<SouthLatitude>55.83974</SouthLatitude>
<WestLongitude>12.55612</WestLongitude>
<NorthLatitude>55.84159</NorthLatitude>
<EastLongitude>12.56277</EastLongitude>
</BoundingBox>
<EntityType>RoadBlock</EntityType>
<Address>
<AddressLine>Frydenlunds Allé</AddressLine>
<AdminDistrict>Capital Region</AdminDistrict>
<AdminDistrict2>Rudersdal</AdminDistrict2>
<CountryRegion>Denmark</CountryRegion>
<FormattedAddress>Frydenlunds Allé, 2950 Vedbæk, Denmark</FormattedAddress>
<Locality>Vedbæk</Locality>
<PostalCode>2950</PostalCode>
</Address>
<Confidence>Medium</Confidence>
<MatchCode>Good</MatchCode>
<MatchCode>UpHierarchy</MatchCode>
<GeocodePoint>
<Latitude>55.84085</Latitude>
<Longitude>12.55948</Longitude>
<CalculationMethod>InterpolationOffset</CalculationMethod>
<UsageType>Display</UsageType>
</GeocodePoint>
<GeocodePoint>
<Latitude>55.84085</Latitude>
<Longitude>12.55948</Longitude>
<CalculationMethod>Interpolation</CalculationMethod>
<UsageType>Route</UsageType>
</GeocodePoint>
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
【问题讨论】:
标签: c# rest location maps bing-maps