【问题标题】:Google maps API address validation in C#C# 中的 Google 地图 API 地址验证
【发布时间】:2016-07-13 06:49:04
【问题描述】:

我正在编写使用 google API 检查输入地址有效性的功能。用户输入街道号码、街道名称、郊区等,然后查找 google api 并返回最适合输入地址的格式化地址。

到目前为止,我得到了以下内容:

public string getFormattedAddress(string address)
{
     string url = "https://maps.googleapis.com/maps/api/geocode/json?address=%" + address + "&key=" + MyStaticMethods.GOOGLE_API_KEY;
     object result = new WebClient().DownloadString(url);
     string json = result.ToString();
     JObject obj = JObject.Parse(json);
     ...
}

但我无法读取 API 的响应。以下是我想做的伪代码:

var streetNumber = apiResult.streetNumber

var address = apiResult.address;

var postcode = apiResult.postcode;

我还想要一种方法来判断 API 查找是否找不到给定地址的任何匹配地址。

任何帮助都会很棒,

提前致谢。

【问题讨论】:

    标签: c# json asp.net-mvc google-maps


    【解决方案1】:

    我建议你使用这个库: https://github.com/chadly/Geocoding.net

    例子:

     GoogleGeocoder geocoder = new GoogleGeocoder();
     IEnumerable<GoogleAddress> addresses = await geocoder.GeocodeAsync("1600 pennsylvania ave washington dc");    
     var country = addresses.Where(a => !a.IsPartialMatch).Select(a => a[GoogleAddressType.Country]).First();    
     Console.WriteLine("Country: " + country.LongName + ", " + country.ShortName);     
    //Country: United States, U
    

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多