65.9K
CodeProject 正在变化。 阅读更多。
Home

用于 Google 地图地理编码器的 .NET API

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.38/5 (9投票s)

2008 年 5 月 27 日

CPOL
viewsIcon

139492

downloadIcon

3202

一个简单的 .NET 库,用于封装 Google 地图地理编码功能

引言

Google 地图允许您通过其 JavaScript API 或直接调用 http://maps.google.com/maps/geo?output=xml&key=yourkey&q=someaddress 来地理编码地址,以 XML 格式获取结果。将这两种方法集成到您的 .NET 代码中可能需要一些时间。为了让生活更轻松一些,我将 HTTP 地理编码请求的功能封装到一个 .NET 库中。

Using the Code

您可以调用该方法以仅获取所有 XML 数据到一个 字符串

string xml = GMapGeocoder.Util.GetXml("123 fake street", "your google map key");

您还可以以原始对象结构获取代码

string xml = GMapGeocoder.Util.GetXml("123 fake street", "your google map key");
GMapGeocoder.Generated.kml kml = GMapGeocoder.Util.DeserializeXml(xml);
string fullAddress = kml.Response.Placemark[0].address;
string countryCode = kml.Response.Placemark[0].AddressDetails.Country.CountryNameCode;
string stateCode = kml.Response.Placemark[0].AddressDetails.Country.
                AdministrativeArea.AdministrativeAreaName;

对于美国地址,以下调用比上面的 kml 对象更有意义

GMapGeocoder.Containers.Results results = 
    GMapGeocoder.Util.Geocode("123 fake street", "your google map key");
GMapGeocoder.Containers.USAddress match1 = results.Addresses[0];
string city = match1.City;
string state = match1.StateCode;
double lat = match1.Coordinates.Latitude;

关注点

我没有找到精确的 XML 定义,因此我根据 Google 上的文档以及通过进行随机查询找到的结果编写了 XSD 文件。我包含了 XSD 文件,以防此定义不正确并且需要重新生成代码。我还注意到,HttpWebRequest.GetResponse() 用于 GetXml 调用,在应用程序的初始调用中速度较慢,但后续调用会快得多。

历史

  • 2008年5月27日:初始发布
  • 2008 年 6 月 24 日:文章已更新
© . All rights reserved.