uniquejava / blog

My notes regarding the vibrating frontend :boom and the plain old java :rofl.
Creative Commons Zero v1.0 Universal
11 stars 5 forks source link

zipcode api #262

Open uniquejava opened 5 years ago

uniquejava commented 5 years ago

微软的不需要credit card.

Bing Map SDK

文档: https://www.bingmapsportal.com/Announcement 文档: https://docs.microsoft.com/en-us/bingmaps/articles/geocoding-japanese-addresses

比如: https://dev.virtualearth.net/REST/v1/Locations?countryRegion=JP&postalCode=1350016&culture=ja&key=$API_KEY";

免费且带汉字假名

http://zip.cgis.biz

XML (UTF-8) http://zip.cgis.biz/xml/zip.php?zn=1350016

Cyper实战:

class ZipCodeService {
  static Future<List> getSuggestions(final String zipcode) async {
    if (zipcode.isEmpty || zipcode.length != 7) {
      return [];
    }
    var url = "http://zip.cgis.biz/xml/zip.php?zn=$zipcode";
    Response res = await Dio().get(url);
    var responseBody = res.data;
    var doc = xml.parse(responseBody);
    var stateKana = _getAttributeValue(doc, 'state_kana');
    if (stateKana != null) {
      var cityKana = _getAttributeValue(doc, 'city_kana');
      var addressKana = _getAttributeValue(doc, 'address_kana');
      var state = _getAttributeValue(doc, 'state');
      var city = _getAttributeValue(doc, 'city');
      var address = _getAttributeValue(doc, 'address');
      return [
        {
          'code': zipcode,
          'addr': '$state$city$address',
          'addrKana': '$stateKana$cityKana$addressKana'
        }
      ];
    } else {
      return [];
    }
  }

  static String _getAttributeValue(xml.XmlDocument doc, String name) {
    var nodes = doc.findAllElements('value');

    try {
      var node = nodes.singleWhere((f) {
        return f.attributes.first.name.local == name;
      });
      return node != null ? node.getAttribute(name) : null;
    } on StateError catch (_) {
      return null;
    }
  }
}

Raw CSV (euc-jp) http://zip.cgis.biz/csv/zip.php?zn=1350016

Cyper实战:

class ZipCodeService {
  static Future<List> getSuggestions(String query) async {
    var httpClient = new HttpClient();
    var uri = Uri.parse("http://zip.cgis.biz/csv/zip.php?zn=$query");
    var request = await httpClient.getUrl(uri);
    var response = await request.close();
    var responseBody =
        await response.transform(Encoding.getByName('euc-jp').decoder).join();
    print(responseBody);

最新postal code数据

http://zipcloud.ibsnet.co.jp/