问题描述
我尝试在此处和 Google Maps API 文档中查看各种代码块,但仍然无法弄清楚如何隐藏标记.
这是我正在使用的当前代码,它适用于一个实例.一旦我使用markers.setMap(null)在函数中添加了for"循环,Firefox就会显示以下错误:
错误:TypeError:markers.setMap 不是函数
function removeMarkers(){var 标记;警报(标记);警报(位置.长度);for (i = 0; i
解决方案
您需要保留
google.maps.Marker
对象的数组以隐藏(或删除或对其运行其他操作).
在全局范围内:
var gmarkers = [];
然后在创建它们时推动该阵列上的标记:
var 标记 = new google.maps.Marker({位置:新 google.maps.LatLng(locations[i].latitude,locations[i].longitude),标题:location[i].title,图标:图标,地图:地图});//将新创建的标记推入数组:gmarkers.push(标记);
然后删除它们:
function removeMarkers(){for(i=0; i<gmarkers.length; i++){gmarkers[i].setMap(null);}}
工作示例(切换标记)
代码片段:
var gmarkers = [];var RoseHulman = new google.maps.LatLng(39.483558, -87.324593);var 样式 = [{造型师:[{色调:黑色"}, {饱和度:-90}]}, {特征类型:道路",元素类型:几何",造型师:[{亮度:100}, {可见性:简化"}]}, {特征类型:道路",元素类型:标签",造型师:[{可见性:开"}]}];var styledMap = new google.maps.StyledMapType(styles, {名称:《校园》});var mapOptions = {中锋:罗斯赫尔曼,变焦:15,地图类型控制:真,缩放控制:真,缩放控制选项:{样式:google.maps.ZoomControlStyle.SMALL},地图类型控制选项:{mapTypeIds: ['map_style', google.maps.MapTypeId.HYBRID],样式:google.maps.MapTypeControlStyle.DROPDOWN_MENU},滚轮:假,街景控制:真,};var map = new google.maps.Map(document.getElementById('map'), mapOptions);map.mapTypes.set('map_style', styledMap);map.setMapTypeId('map_style');var infowindow = new google.maps.InfoWindow({最大宽度:300,infoBoxClearance: 新的 google.maps.Size(1, 1),disableAutoPan: 假});var 标记、i、图标、图像;变量位置 = [{"id": "1","类别": "6","campus_location": "F2","title": "Alpha Tau Omega Fraternity","description": "<p>Alpha Tau Omega house</p>",经度":-87.321133",纬度":39.484092"}, {"id": "2","类别": "6","campus_location": "B2","title": "公寓公地","description": "<p>公寓式住宅小区的公共区域</p>",经度":-87.329282",纬度":39.483599"}, {"id": "3","类别": "6","campus_location": "B2","title": "东公寓","description": "<p>东公寓</p>",经度":-87.328809",纬度":39.483748"}, {"id": "4","类别": "6","campus_location": "B2","title": "公寓西","description": "<p>西公寓</p>",经度":-87.329732",纬度":39.483429"}, {"id": "5","类别": "6","campus_location": "C2","title": "Baur-Sames-Bogart (BSB) Hall","description": "<p>Baur-Sames-Bogart Hall</p>",经度":-87.325714",纬度":39.482382"}, {"id": "6","类别": "6","campus_location": "D3","title": "布隆伯格音乐厅","description": "<p>Blumberg Hall</p>",经度":-87.328321",纬度":39.483388"}, {"id": "7","类别": "1","campus_location": "E1","title": "布拉南创新中心","description": "<p>布拉南创新中心</p>",经度":-87.322614",纬度":39.48494"}, {"id": "8","类别": "6","campus_location": "G3","title": "Chi Omega Sorority","description": "<p>Chi Omega house</p>",经度":-87.319905",纬度":39.482071"}, {"id": "9","类别": "3","campus_location": "D1","title": "库克体育场/菲尔布朗菲尔德","description": "<p>菲尔布朗球场的库克体育场</p>",经度":-87.325258",纬度":39.485007"}, {"id": "10","类别": "1","campus_location": "D2","title": "克拉波大厅","description": "<p>克拉波大厅</p>",经度":-87.324368",纬度":39.483709"}, {"id": "11","类别": "6","campus_location": "G3","title": "三角洲三角洲联谊会","description": "<p>Delta Delta Delta</p>",经度":-87.317477",纬度":39.482951"}, {"id": "12","类别": "6","campus_location": "D2","title": "德明堂","description": "<p>戴明霍尔</p>",经度":-87.325822",纬度":39.483421"}, {"id": "13","类别": "5","campus_location": "F1","title": "设施运营","description": "<p>设施运营</p>",经度":-87.321782",纬度":39.484916"}, {"id": "14","类别": "2","campus_location": "E3","title": "千年之火","description": "<p>千年雕塑的火焰</p>",经度":-87.323306",纬度":39.481978"}, {"id": "15","类别": "5","campus_location": "E2","title": "哈德利大厅","description": "<p>Hadley Hall</p>",经度":-87.324046",纬度":39.482887"}, {"id": "16","类别": "2","campus_location": "F2","title": "哈特菲尔德大厅","description": "<p>Hatfield Hall</p>",经度":-87.322340",纬度":39.482146"}, {"id": "17","类别": "6","campus_location": "C2","title": "赫尔曼纪念联盟","description": "<p>Hulman Memorial Union</p>",经度":-87.32698",纬度":39.483574"}, {"id": "18","类别": "1","campus_location": "E2","title": "John T. Myers 工业技术研究中心","description": "<p>John T. Myers 工业技术研究中心</p>",经度":-87.322984",纬度":39.484063"}, {"id": "19","类别": "6","campus_location": "A2","title": "湖畔厅","描述": "<p></p>",经度":-87.330612",纬度":39.482804"}, {"id": "20","类别": "6","campus_location": "F2","title": "Lambda Chi Alpha 兄弟会","description": "<p>Lambda Chi Alpha</p>",经度":-87.320999",纬度":39.48305"}, {"id": "21","类别": "1","campus_location": "D2","title": "洛根图书馆","description": "<p>Logan 图书馆</p>",经度":-87.324851",纬度":39.483408"}, {"id": "22","类别": "6","campus_location": "C2","title": "Mees Hall","description": "<p>Mees Hall</p>",经度":-87.32778",纬度":39.483533"}, {"id": "23","类别": "1","campus_location": "E2","title": "Moench Hall","description": "<p>Moench Hall</p>",经度":-87.323695",纬度":39.483471"}, {"id": "24","类别": "1","campus_location": "G4","title": "奥克利天文台","description": "<p>奥克利天文台</p>",经度":-87.31616",纬度":39.483789"}, {"id": "25","类别": "1","campus_location": "D2","title": "奥林堂和奥林高级学习中心","description": "<p>Olin Hall</p>",经度":-87.324550",纬度":39.482796"}, {"id": "26","类别": "6","campus_location": "C3","title": "Percopo Hall","description": "<p>Percopo Hall</p>",经度":-87.328182",纬度":39.482121"}, {"id": "27","类别": "6","campus_location": "G3","title": "公共安全办公室","description": "<p>公共安全办公室</p>",经度":-87.320377",纬度":39.48191"}, {"id": "28","类别": "1","campus_location": "E2","title": "Rotz 机械工程实验室","description": "<p>Rotz Lab</p>",经度":-87.323247",纬度":39.483711"}, {"id": "28","类别": "6","campus_location": "C2","title": "沙彭贝格音乐厅","description": "<p>Scharpenberg Hall</p>",经度":-87.328139",纬度":39.483582"}, {"id": "29","类别": "6","campus_location": "G2","title": "Sigma Nu Fraternity","description": "The Sigma Nu house
",经度":-87.31999",纬度":39.48374"}, {"id": "30","类别": "6","campus_location": "E4","title": "南校区/Rose-Hulman Ventures","描述": "<p></p>",经度":-87.330623",纬度":39.417646"}, {"id": "31","类别": "6","campus_location": "C3","title": "速度大厅","description": "<p>Speed Hall</p>",经度":-87.326632",纬度":39.482121"}, {"id": "32","类别": "3","campus_location": "C1","title": "运动休闲中心","描述": "<p></p>",经度":-87.3272",纬度":39.484874"}, {"id": "33","类别": "6","campus_location": "F2","title": "三角兄弟会","description": "<p>三角兄弟会</p>",经度":-87.32113",纬度":39.483659"}, {"id": "34","类别": "6","campus_location": "B3","title": "白色教堂","description": "<p>The White Chapel</p>",经度":-87.329367",纬度":39.482481"}, {"id": "35","类别": "6","campus_location": "F2","title": "妇女兄弟会之家",描述": "",图片": "",经度":-87.320753",纬度":39.482401"}, {"id": "36","类别": "3","campus_location": "E1","title": "校内场","描述": "<p></p>",经度":-87.321267",纬度":39.485934"}, {"id": "37","类别": "3","campus_location": "A3","title": "詹姆斯伦德尔足球场","描述": "<p></p>",经度":-87.332135",纬度":39.480933"}, {"id": "38","类别": "3","campus_location": "B2","title": "Art Nehf Field","description": "<p>Art Nehf Field</p>",经度":-87.330923",纬度":39.48022"}, {"id": "39","类别": "3","campus_location": "B2","title": "女子垒球场","描述": "<p></p>",经度":-87.329904",纬度":39.480278"}, {"id": "40","类别": "3","campus_location": "D1","title": "Joy Hulbert 网球场","description": "<p>The Joy Hulbert 室外网球场</p>",经度":-87.323767",纬度":39.485595"}, {"id": "41","类别": "6","campus_location": "B2","title": "极速湖",描述": "",图片": "",经度":-87.328134",纬度":39.482779"}, {"id": "42","类别": "5","campus_location": "F1","title": "回收中心",描述": "",图片": "",经度":-87.320098",纬度":39.484593"}, {"id": "43","类别": "1","campus_location": "F3","title": "陆军ROTC",描述": "",图片": "",经度":-87.321342",纬度":39.481992"}, {"id": "44","类别": "2","campus_location": " ","title": "白手起家的人",描述": "",图片": "",经度":-87.326272",纬度":39.484481"}, {"id": "P1","类别": "4","title": "Percopo 停车场",描述": "",图片": "",经度":-87.328756",纬度":39.481587"}, {"id": "P2","类别": "4","title": "快速停车",描述": "",图片": "",经度":-87.327361",纬度":39.481694"}, {"id": "P3","类别": "4","title": "主停车场",描述": "",图片": "",经度":-87.326245",纬度":39.481446"}, {"id": "P4","类别": "4","title": "湖边停车场",描述": "",图片": "",经度":-87.330848",纬度":39.483284"}, {"id": "P5","类别": "4","title": "哈特菲尔德大厅停车场",描述": "",图片": "",经度":-87.321417",纬度":39.482398"}, {"id": "P6","类别": "4","title": "妇女兄弟会停车场",描述": "",图片": "",经度":-87.320977",纬度":39.482315"}, {"id": "P7","类别": "4","title": "迈尔斯和设施停车场",描述": "",图片": "",经度":-87.322243",纬度":39.48417"}, {"id": "P8","类别": "4",标题": "",描述": "",图片": "",经度":-87.323241",纬度":39.484758"}, {"id": "P9","类别": "4",标题": "",描述": "",图片": "",经度":-87.323617",纬度":39.484311"}, {"id": "P10","类别": "4",标题": "",描述": "",图片": "",经度":-87.325714",纬度":39.484584"}, {"id": "P11","类别": "4",标题": "",描述": "",图片": "",经度":-87.32778",纬度":39.484145"}, {"id": "P12","类别": "4",标题": "",描述": "",图片": "",经度":-87.329035",纬度":39.4848"}];for (i = 0; i
html,身体,#地图 {高度:100%;宽度:100%;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script><div id="控件"><input type="button" value="Toggle All Markers" onClick="toggleMarkers()"/>
<div id="map"></div>