Google Maps API地理编码 - 处理多个请求

时间:2013-05-11 13:02:29

标签: 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>
    <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>

作为回报,我获得了一些反向地理编码机器人的点,因为我得到“Geocoder因为:OVER_QUERY_LIMIT而失败”,并且它与通过反向地理编码的点数不同,有时我有时会得到4点处理,有时候5,7等等。为什么我会得到OVER_QUERY_LIMIT错误?我今天提出了100个地理编码器请求,所以我远离2500天的限制。如果有更好的方法来处理多个请求,请向我展示。

0 个答案:

没有答案