使用Google Geocoding API(或类似)实现地址查找

-2 api postal-code geocode

我想在我的网站上添加一个功能,客户可以在其中输入邮政编码,并使用Google地理编码或开源/ Bing等效自动查找地址.

有没有人得到任何示例代码或知道一个好的指导我可以做到这一点?

如果使用jQuery完成甚至更好:)

提前谢谢了

达伦

sea*_*cob 5

使用jQuery getJSON和谷歌的API - http://codepen.io/seanjacob/pen/wfcHB

$('#submit').click(function(){  

  //Get Postcode
  var number = $('#number').val();
  var postcode = $('#postcode').val().toUpperCase();;

  //Get latitude & longitude
  $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?address=' + postcode + '&key=[[YOUR_API_KEY]]', function(data) {

    var lat = data.results[0].geometry.location.lat;
    var lng = data.results[0].geometry.location.lng;

    //Get address    
    $.getJSON('https://maps.googleapis.com/maps/api/geocode/json?latlng=' + lat + ',' + lng + '&key=[[YOUR_API_KEY]]', function(data) {              
      var address = data.results[0].address_components;              
      var street = address[1].long_name;
      var town = address[2].long_name;
      var county = address[3].long_name;                        

      //Insert
      $('#text').text(number + ', ' + street + ', ' + town + ', ' + county + ', ' + postcode);

    });
  });
});
Run Code Online (Sandbox Code Playgroud)

除非您使用Google Maps API for Business,否则每天仅限2,500个地理定位请求.由于此脚本每次运行发出两个请求,因此每天会有1,250个邮政编码查询.