Javascript 工作谷歌地图API我有它的设置,以便用户可以添加一个选择矩形到他们的屏幕上。我怎么能只做一次?

Javascript 工作谷歌地图API我有它的设置,以便用户可以添加一个选择矩形到他们的屏幕上。我怎么能只做一次?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,基本上,我只希望用户能够添加一个矩形选择地图。但现在它只是增加了你想要的数量。我尝试添加,使addSelection等于noop。但这对我来说没用。我认为这只允许函数执行一次。无论如何,我试图只允许用户添加一个矩形选择地图。我想知道阅读的最佳途径是什么 html,正文{ 身高:100%; 保证金:0; 填充:0; } #地图{ 身高:100%; } var映射; var矩形; var信息窗口; var penang={ 纬度:5.466277, 液化天然气:100.289981 }; /**

基本上,我只希望用户能够添加一个矩形选择地图。但现在它只是增加了你想要的数量。我尝试添加,使addSelection等于noop。但这对我来说没用。我认为这只允许函数执行一次。无论如何,我试图只允许用户添加一个矩形选择地图。我想知道阅读的最佳途径是什么

html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:100%;
}

var映射;
var矩形;
var信息窗口;
var penang={
纬度:5.466277,
液化天然气:100.289981
};
/**
*CenterControl向地图添加一个控件,该控件在芝加哥重新居中地图。
*此构造函数将控件DIV作为参数。
*@constructor
*/
函数noop(){}
功能添加选择(controlDiv、map){
//为控件边框设置CSS。
var controlUI=document.createElement('div');
controlUI.style.backgroundColor='#fff';
controlUI.style.border='2px solid#fff';
controlUI.style.borderRadius='3px';
controlUI.style.boxShadow='0 2px 6px rgba(0,0,0,3)';
controlUI.style.cursor='pointer';
controlUI.style.marginBottom='22px';
controlUI.style.textAlign='center';
controlUI.title='单击以进行选择';
controlDiv.appendChild(controlUI);
//为控件内部设置CSS。
var controlText=document.createElement('div');
controlText.style.color='rgb(25,25,25)';
controlText.style.fontFamily='Roboto,Arial,无衬线';
controlText.style.fontSize='16px';
controlText.style.lineHeight='38px';
controlText.style.paddingLeft='5px';
controlText.style.paddingRight='5px';
controlText.innerHTML='选择区域';
controlUI.appendChild(controlText);
//设置click事件侦听器:只需将映射设置为Chicago。
controlUI.addEventListener('单击',函数()){
var centerView=map.getCenter();
var bounds=map.getBounds();
var hoach=bounds.getNorthEast().lat();
var moach=bounds.getsoutwest().lat();
var dim=(霍奇-莫奇)/4;
var selectionBounds=new google.maps.LatLngBounds(
新的google.maps.LatLng(centerView.lat()-dim,centerView.lng()-dim),
新的google.maps.LatLng(centerView.lat()+dim,centerView.lng()+dim));
//定义矩形并将其“可编辑”属性设置为true
矩形=新的google.maps.rectangle({
界限:selectionBounds,
是的,
真的,
strokeColor:“#FF0000”,
笔划不透明度:0.8,
冲程重量:2,
填充颜色:'#FF0000',
不透明度:0.35,
地图:地图,
});
});
addSelection==noop;//为什么不将其限制为1?
}
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
缩放:15,
泛控制:对,
中心:槟城
});
//创建用于保存控件的DIV并调用CenterControl()构造函数
//经过这个分区。
var selectionControlDiv=document.createElement('div');
var selectionControl=新添加选择(selectionControlDiv,map);
selectionControlDiv.index=1;
map.controls[google.maps.ControlPosition.TOP\u CENTER].push(选择ControlDiv);
}

使
addSelection
等于
noop
是无用的,您必须删除click listener

但是,当您只需要一次按钮时,只需在单击后删除按钮即可:

controlUI.addEventListener('click', function() {

   this.parentNode.removeChild(this);

   //continue with your code......
});