GeoCode在C#.NET中使用Yahoo API

时间:2012-10-02 12:44:55

标签: c# asp.net .net asp.net-mvc-3

我的csome代码在几天前正常运行时遇到了一些问题。我正在使用Yahoos API来获取PostCodes的逻辑和纬度,代码如下:

string url = string.Format("http://where.yahooapis.com/geocode?flags=J&appid=xxxx&location={0}", postcode);

        decimal latitude = 0;
        decimal longitude = 0;
        Dictionary<string, decimal> geoCode = new Dictionary<string, decimal>();

        dynamic yahooResults = new Uri(url).GetDynamicJsonObject();
        foreach (var result in yahooResults.ResultSet.Results)
        {
            latitude = (decimal)result.latitude;
            longitude = (decimal)result.longitude;
        }

        geoCode.Add("latitude", latitude);
        geoCode.Add("longitude", longitude); 

        return geoCode;

正如我所说,代码在几天前工作正常,但现在逻辑和纬度总是返回0.我在下面列出了雅虎的回复:

{
  "@lang": "en-US",
  "ResultSet": {
    "@version": "2.0",
    "@lang": "en-US",
    "Error": "0",
    "ErrorMessage": "No error",
    "Locale": "en-US",
    "Found": "1",
    "Quality": "60",
    "Result": {
      "quality": "60",
      "latitude": "51.62071",
      "longitude": "-0.23616",
      "offsetlat": "51.620708",
      "offsetlon": "-0.23616",
      "radius": "4200",
      "name": "",
      "line1": "",
      "line2": "London",
      "line3": "NW7",
      "line4": "United Kingdom",
      "house": "",
      "street": "",
      "xstreet": "",
      "unittype": "",
      "unit": "",
      "postal": "NW7",
      "neighborhood": "",
      "city": "London",
      "county": "Greater London",
      "state": "England",
      "country": "United Kingdom",
      "countrycode": "GB",
      "statecode": "ENG",
      "countycode": "LND",
      "uzip": "NW7",
      "hash": "",
      "woeid": "26787971",
      "woetype": "11"
    }
  }
}

抱歉格式化堆栈似乎不想格式化它!显然有一个逻辑和纬度返回。我确信这很简单,但我看不到爱情和金钱,任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

似乎雅虎不再返回Result数组。下面的代码工作

dynamic yahooResults =.....;

var latitude = (decimal)yahooResults.ResultSet.Result.latitude;
var longitude = (decimal)yahooResults.ResultSet.Result.longitude;