当我在 javascript 的另一个函数中调用一个函数时出现 ReferenceError

ReferenceError when I call a function inside another function in javascript

我写了一个非常简单的应用程序,其中我使用 google 绘图库 (https://developers.google.com/maps/documentation/javascript/examples/drawing-tools) 让用户在地图上画圆圈(第一个圆圈是源地,下一个圆圈是目的地,用户只能 select 一个来源)。这段代码也得到了纬度。用户在地图上绘制的圆的长度和半径。

我可以访问 api( GET https://api.dandelion.eu/datagems/v2/SpazioDati/milano-grid/data?$limit=10&$offset=0&$app_id=YOUR_APP_ID&$app_key=YOUR_APP_KEY) 我必须调用这个 api url 以便获取用户在地图上绘制的圆圈内的单元 ID。

我的问题: 我在控制台看到这个错误 ReferenceError: doStaff is not defined

代码如下:

<!DOCTYPE html>
<html>
  <head>
    <title>Drawing tools</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <link type="text/css" href="res/jquery-ui.css" rel="stylesheet" />
    <script type="text/javascript" src="res/jquery.min.js"></script>
    <script type="text/javascript" src="res/jquery-ui.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
   <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=drawing,places"></script> 

<script type="text/javascript"> 

  (function () {
     var circle;
     var latitude;
     var longitude;
     var radius;

   function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-34.397, 150.644),
      zoom: 8
   };

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

  var drawingManager = new google.maps.drawing.DrawingManager({
    drawingMode: google.maps.drawing.OverlayType.MARKER,
    drawingControl: true,
    drawingControlOptions: {
      position: google.maps.ControlPosition.TOP_CENTER,
      drawingModes: [
        google.maps.drawing.OverlayType.MARKER,
        google.maps.drawing.OverlayType.CIRCLE
      ]
    },
    markerOptions: {
      icon: 'images/beachflag.png'
    },
    circleOptions: {
      fillColor: '#ffff00',
      fillOpacity: 1,
      strokeWeight: 5,
      clickable: false,
      editable: true,
      zIndex: 1
    }
  });
  drawingManager.setMap(map);
  google.maps.event.addListener(drawingManager, 'circlecomplete', onCircleComplete);
 }

 function onCircleComplete(shape) {
      var map=shape.getMap();
      var circle;
       //create an array where we store the circles
       if(!map.get('circles')){
          map.set('circles',[]); 
        }
       shape.setOptions(
                      (!map.get('circles').length)
                        ?//first circle
                         {type:'source',
                          fillColor:'#ff0000'
                         }
                        ://other circles
                         {type:'destination'}
                    );
      //push the circles onto the array 
      map.get('circles').push(shape);

        circle = shape;
        var radius = circle.getRadius();
        center = circle.getCenter();
        var latitude = circle.getCenter().lat();
        var longitude = circle.getCenter().lng();

         doStaff();
         alert(radius);

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

function doStuff() {
  var where_stm = 'within_circle('+latitude+','+longitude+','+radius+')';
  var app_id='7b22cb45';
  var app_key='dc836a05b4f775d8813d253ba07a4570';
     $.ajax({
         url: 'https://api.dandelion.eu/datagems/v2/SpazioDati/milano-grid/data',
         type: "GET",
        'content-Type': "application/json",
         dataType: "json",
         data: {where:where_stm, app_id:app_id, app_key:app_key},
         success: function(response) {        
                   console.log(response);
                   },
         error: function(XMLHttpRequest, textStatus, errorThrown) {
                            console.log("error :"+XMLHttpRequest.responseText);
                    }
        });
  }

    </script>
  </head>

<body>
  <div id="container">
     <div id="sidebar-left">
        <p> Please select your source place by using drawing tools on the map. </p>
        Then, you can select one or more destination on the map using the same drawing tools 
     <p>
       <button onClick="doStuff()">Run Code</button>
     </p>
     </div>
     <div id="map-canvas"></div>
    </div>
  </body>
</html>

您已经定义了 doStuff,但是您调用了 doStaff,最后一个不存在,您收到错误,请改为调用 doStuff

您的 doStuff 方法似乎在匿名函数范围内。您需要将它带到全局范围内才能在 onClick 属性中访问。