【问题标题】:Define number of decimals returned by Bing Maps query C#定义 Bing 地图查询 C# 返回的小数位数
【发布时间】: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


    【解决方案1】:

    没有返回额外坐标的选项。我真的很惊讶这个代码挑战正在做这样的事情,因为地址坐标会不时改变。此外,他们使用 7 位小数,精确到 1 厘米。 Bing Maps REST 服务仅返回 5 位小数,精度约为 1m。其余小数位无关紧要。

    如果此代码挑战针对 UWP 应用,则内置地理编码器可能会返回更多小数位,因为它目前使用不同的地理编码引擎。

    【讨论】:

    • 挑战不适用于 UWP 应用,但我不能排除他们可能会使用相同的单元测试来应对多个挑战。我想我会向 mscodechallenge.net 团队开一张票并询问他们。
    • 开票是个好主意。如果您愿意,可以将我的电子邮件包含在票证中以供他们参考; microsoft.com 上的richbrun
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多