0

我需要使用 Google 的地理编码服务对数据进行地理编码。Google 的地理编码服务不像 Bing 所说的那样对通过 .NET 进行消费友好(这并不奇怪),所以虽然我可以全力以赴 ContractDataSerializers,WCF,JSON 和一大堆其他首字母缩略词,但像下面这样的东西有什么问题吗如果我需要的只是纬度和经度,即。

string url = String.Format("http://maps.google.com/maps/api/geocode/xml?address=blah&region=ie&sensor=false", HttpUtility.UrlEncode(address));

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(url);
XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/GeocodeResponse/result");

if (xmlNodeList != null)
{
   // Do something here with the information
}

除了大量的前期开发工作之外,另一种方法究竟会购买什么?我对 WCF、DataContracts、ServiceContracts 等感到非常满意,但我看不出他们会在这里带来什么......

4

2 回答 2

1

在 codeplex 上使用 GoogleMap Control 项目:http: //googlemap.codeplex.com/

它具有使用 Google 进行地理编码的类:http: //googlemap.codeplex.com/wikipage ?title=Google%20Geocoder&referringTitle=Documentation 。

于 2010-09-24T11:34:40.277 回答
1

我会将 XDocument 与 WebRequest 一起使用。以下示例可能会有所帮助。

public static GeocoderLocation Locate(string query)
{
    WebRequest request = WebRequest.Create("http://maps.google.com/maps?output=kml&q="
        + HttpUtility.UrlEncode(query));

    using (WebResponse response = request.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            XDocument document = XDocument.Load(new StreamReader(stream));

            XNamespace ns = "http://earth.google.com/kml/2.0";

            XElement longitudeElement = document.Descendants(ns + "longitude").FirstOrDefault();
            XElement latitudeElement = document.Descendants(ns + "latitude").FirstOrDefault();

            if (longitudeElement != null && latitudeElement != null)
            {
                return new GeocoderLocation
                {
                    Longitude = Double.Parse(longitudeElement.Value, CultureInfo.InvariantCulture),
                    Latitude = Double.Parse(latitudeElement.Value, CultureInfo.InvariantCulture)
                };
            }
        }
    }

    return null;
}
于 2010-09-24T12:40:25.617 回答