我已在我的网站上实施了googleMap api,但我不断收到以下错误消息:
ReferenceError: deleteOverlays is not defined
ReferenceError: clearOverlays is not defined
我在home.php中使用的javascript:
document.addEventListener('DOMContentLoaded' , drawMap);
var map,line;
var locationsArray = new Array();
var linesArray = new Array();
var markersArray = new Array();
function drawMap()
{
var myCenter =new google.maps.LatLng(24.87877,67.06403);
var mapProp = {
center:myCenter,
zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
heading: 90,
tilt: 45
};
var map = new google.maps.Map(document.getElementById("googleMap") ,mapProp);
var marker = new google.maps.Marker({
position: myCenter,
});
marker.setMap(map);
});
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
markersArray.push(marker);
}
var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3
};
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
google.maps.event.addListener(map, 'click', placeLine);
我的职能是:
function deleteOverlays() {
alert("enter in function above th if condition");
if (markersArray) {
for (var i=0; i<markersArray.length; i++) {
markersArray[i].setMap(null);
}
}
markersArray.length = 0;
if (linesArray) {
for (var i=0; i<linesArray.length; i++) {
linesArray[i].setMap(null);
}
linesArray.length = 0;
}
}
function clearOverlays() {
alert("enter in function");
if (markersArray)
{
var markersArraylength = markersArray.length -1;
markersArray[markersArraylength].setMap(null);
markersArray.pop();
locationsArray[markersArraylength] = null;
locationsArray.pop();
}
if (linesArray)
{
var linesArraylength = linesArray.length -1;
linesArray[linesArraylength].setMap(null);
linesArray.pop();
}
}
function placeLine(event){
linesArray = poly.getPath();
linesArray.push(event.latLng);
}
}
这是我用来调用函数的代码:
<input type="button" id="deletelastmarker" value="Delete Last marker" onclick="clearOverlays()"></input>
<input type="button" id="deleteallmarker" value="Delete all markers" onclick="deleteOverlays()"></input>
我该如何解决上述问题
答案 0 :(得分:2)
要将函数用作HTML onclick函数,必须在全局范围内。