0

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

<!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>

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

4

0 回答 0