简体   繁体   English

带有地理编码的 Google 地图返回 null

[英]Google Maps with Geocode returning null

I'm using Google Maps to place markers from an array of zip codes but it only places about 10% of them.我正在使用 Google 地图从一组邮政编码中放置标记,但它只放置了大约 10% 的标记。 The rest return null results.其余的返回空结果。 I can't find the problem.我找不到问题。 Here is my code.这是我的代码。

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&amp;language=en"></script>
<script type="text/javascript">     
  var latlng = new google.maps.LatLng(37.09024, -95.712891);
  var map = new google.maps.Map(document.getElementById('pledge-map'), {
    zoom: 4,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  });               
  var geocoder = new google.maps.Geocoder();
  var locations = <?php echo(json_encode($zips)); ?>        
  for (var i = 0; i < locations.length; i++) {                
    geocoder.geocode( { 'address': locations[i]}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        var marker = new google.maps.Marker({
            position: results[0].geometry.location,
            map: map
          });
      }
    });     
  }
</script>

locations is an array of zip code: location 是一组邮政编码:

locations =["20190","22192","20148","06078","95136","84120","53066","53404","48323","52404","72762","89701","39667","78073","78016","76013","15613","84010","77662","77407","30127","94566","14625","45629","27406","27289","02339","01864","39116","77079","10954","37683","72801","19341","14580","64133","29579","08527","32258","33433","60093","76691","78261","77095","19426","10469","92038","85208","67220","61031","85706","72223","21222","66224","34242","30224","56232","32746","08558","72712","14209","43015","52722","32779","33518","33578","60565","11239","17007","60623","77469","20165","20002","36116","20769","20815","34209","02301","60025","48158","38804","30736","01803","45040","48069","07463","53018","73008","80951","70785","72065","91510","91506","67209","22408","36849"]   

The geocoder is subject to a quota and rate limits.地理编码器受配额和速率限制的约束。 you are not checking for the error case (there is no code if the status returned is not OK).您没有检查错误情况(如果返回的状态不正确,则没有代码)。

Either:任何一个:

  1. look for the quota exceeded error code and handle it intelligently (add a delay and retry, only add the marker if it succeeds)查找配额超出错误码并智能处理(加延时重试,成功才加marker)
  2. geocode the zipcodes offline and store the coordinates, use those to display your markers.离线对邮政编码进行地理编码并存储坐标,使用这些来显示您的标记。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM