Google maps 谷歌地图API地理编码-处理多个请求

Google maps 谷歌地图API地理编码-处理多个请求,google-maps,google-maps-api-3,google-geocoding-api,Google Maps,Google Maps Api 3,Google Geocoding Api,我希望能够对地图上的多个点进行反向地理编码。我的代码如下所示: <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <title>Simple Polylines</title> <

我希望能够对地图上的多个点进行反向地理编码。我的代码如下所示:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple Polylines</title>
    <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }

      #map-canvas, #map_canvas {
        height: 100%;
      }

      @media print {
        html, body {
          height: auto;
        }

        #map_canvas {
          height: 650px;
        }
      }

      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script type="text/javascript" src="src/polyline.edit.packed.js"></script>
    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
      var flightPath;
      var map;
      var geocoder;

      function initialize() {
        geocoder = new google.maps.Geocoder();
        var myLatLng = new google.maps.LatLng(0, -180);
        var mapOptions = {
          zoom: 3,
          center: myLatLng,
          mapTypeId: google.maps.MapTypeId.TERRAIN
        };

        map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

        var flightPlanCoordinates = [
            /*new google.maps.LatLng(37.772323, -122.214897),
            new google.maps.LatLng(21.291982, -157.821856),
            new google.maps.LatLng(-18.142599, 178.431),
            new google.maps.LatLng(-27.46758, 153.027892)*/
        ];
        flightPath = new google.maps.Polyline({
          path: flightPlanCoordinates,
          strokeColor: '#FF0000',
          strokeOpacity: 1.0,
          strokeWeight: 2,
          map: map
        });

        // Add a listener for the click event
        google.maps.event.addListener(map, 'click', addLatLng);

        // Add listener for end of editing event
        google.maps.event.addListener(flightPath, 'edit_end', function(path){
          var coords = [];

          //THIS PART IS INTERESTING FOR THIS POST
          //I'm requesting geocoding for every point on map
          path.forEach(function(position){ 
            coords.push(position.toUrlValue(5));
            var latlng = new google.maps.LatLng(position.lat(), position.lng());
            geocoder.geocode({'latLng': latlng}, function(results, status) {
              if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                  console.log(results[1].formatted_address);
                } else {
                  alert('No results found');
                }
              } else {
                alert('Geocoder failed due to: ' + status);
              }
            });
            window.setTimeout(console.log('tic tac'), 500);
          });

          console.log("[edit_end]   path: " + coords.join(" | "));
        });

        //flightPath.setMap(map);
        flightPath.edit();
      }

      //function when user clicked on map
      function addLatLng(event) {
        var path = flightPath.getPath();

        // Because path is an MVCArray, we can simply append a new coordinate
        // and it will automatically appear
        path.push(event.latLng);
        flightPath.edit();
      }

      // stop edit mode
      function save(){
        flightPath.edit(false);
      }

      google.maps.event.addDomListener(window, 'load', initialize);

      $(window).load(function(){

      });
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
  </body>
</html>

简单多段线
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图画布,#地图画布{
身高:100%;
}
@媒体印刷品{
html,正文{
高度:自动;
}
#地图画布{
高度:650px;
}
}
#面板{
位置:绝对位置;
顶部:5px;
左:50%;
左边距:-180px;
z指数:5;
背景色:#fff;
填充物:5px;
边框:1px实心#999;
}
var飞行路径;
var映射;
var地理编码器;
函数初始化(){
geocoder=新的google.maps.geocoder();
var mylatng=new google.maps.LatLng(0,-180);
变量映射选项={
缩放:3,
中心:myLatLng,
mapTypeId:google.maps.mapTypeId.TERRAIN
};
map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
var FlightPlan坐标=[
/*新google.maps.LatLng(37.772323,-122.214897),
新google.maps.LatLng(21.291982,-157.821856),
新google.maps.LatLng(-18.142599178.431),
新google.maps.LatLng(-27.46758153.027892)*/
];
flightPath=新建google.maps.Polyline({
路径:FlightPlan坐标,
strokeColor:“#FF0000”,
笔划不透明度:1.0,
冲程重量:2,
地图:地图
});
//为单击事件添加侦听器
google.maps.event.addListener(映射,'click',addLatLng);
//添加用于结束编辑事件的侦听器
google.maps.event.addListener(flightPath,'edit_end',函数(path){
var-coords=[];
//这一部分对这篇文章来说很有趣
//我要求对地图上的每个点进行地理编码
path.forEach(函数(位置){
协调推(位置、行程值(5));
var latlng=new google.maps.latlng(position.lat(),position.lng());
geocoder.geocode({'latLng':latLng},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[1]){
console.log(结果[1]。格式化的\u地址);
}否则{
警报(“未找到结果”);
}
}否则{
警报('地理编码器因:'+状态而失败);
}
});
setTimeout(console.log('tic tac'),500);
});
console.log(“[edit_end]路径:”+coords.join(“|”);
});
//flightPath.setMap(map);
flightPath.edit();
}
//用户单击地图时的函数
功能添加(事件){
var path=flightPath.getPath();
//因为path是一个MVCArray,所以我们可以简单地附加一个新的坐标
//它会自动出现
路径推送(event.latLng);
flightPath.edit();
}
//停止编辑模式
函数save(){
flightPath.edit(false);
}
google.maps.event.addDomListener(窗口“加载”,初始化);
$(窗口)。加载(函数(){
});
作为回报,我得到了一些反向地理编码机器人的分数,其中一些我得到了“地理编码失败原因:超过查询限制”,通过反向地理编码的点数并不相同,有时我得到4个点数,有时5个,7个,等等。为什么我会得到超过查询限制的错误?我今天提出了大约100个地理编码器请求,所以我离2500天的限制还很远。如果有更好的方法处理多个请求,请向我展示。

可能重复的可能重复的