我写了一个非常简单的应用程序,其中我使用谷歌绘图库(https://developers.google.com/maps/documentation/javascript/examples/drawing-tools)让用户在地图上绘制圆圈(第一个圆圈是源地点,下一个圆圈是目的地,用户只能选择一个来源)。这段代码也得到了lat。用户在地图上绘制的圆和半径。
我可以访问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>
答案 0 :(得分:1)
您已定义doStuff
,但是您致电doStaff
,最后一个不存在,您收到错误,请改为拨打doStuff
。
答案 1 :(得分:1)
您的doStuff方法似乎位于匿名函数范围内。您需要将它带到全局范围,以便在onClick属性中可访问。