Javascript 谷歌地图V3-删除所有标记
我希望谷歌地图v3,如果你放大到15以上,地图会显示标记位置,但当你缩小时,我想隐藏标记。我找不到任何函数来执行此操作。到目前为止,我什么都没用 这是我的剧本:Javascript 谷歌地图V3-删除所有标记,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我希望谷歌地图v3,如果你放大到15以上,地图会显示标记位置,但当你缩小时,我想隐藏标记。我找不到任何函数来执行此操作。到目前为止,我什么都没用 这是我的剧本: <script type="text/javascript"> function initialize() { var mapOptions = { zoom: 15, center: new google.maps.LatLng
<script type="text/javascript">
function initialize() {
var mapOptions = {
zoom: 15,
center: new google.maps.LatLng(52.429236, 6.281255),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
setMarkers(map, points);
google.maps.event.addListener(map, 'zoom_changed', function()
{
if (map.getZoom() > 15) {
setMarkers(map, points);
} else {
hideMarkers(map, points);
}
});
}
var points = [
['Location 1', 52.420891, 6.280204],
['Location 2', 52.420125, 6.279131],
['Location 3', 52.420125, 6.240125]
];
function setMarkers(map, locations) {
var image = new google.maps.MarkerImage('../images/map/beachflag.png',
new google.maps.Size(20, 32),
new google.maps.Point(0,0),
new google.maps.Point(0, 32));
var shadow = new google.maps.MarkerImage('../images/map/beachflag_shadow.png',
new google.maps.Size(37, 32),
new google.maps.Point(0,0),
new google.maps.Point(0, 32));
var shape = {
coord: [1, 1, 1, 20, 18, 20, 18 , 1],
type: 'poly'
};
for (var i = 0; i < locations.length; i++) {
var point = locations[i];
var myLatLng = new google.maps.LatLng(point[1], point[2]);
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
shadow: shadow,
icon: image,
shape: shape,
title: point[0]
});
}
}
function hideMarkers(map, locations) {
/* Remove All Markers */
console.log("Remove All Markers");
}
</script>
函数初始化(){
变量映射选项={
缩放:15,
中心:新google.maps.LatLng(52.429236,6.281255),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map_canvas”),mapOptions);
设置标记(地图、点);
google.maps.event.addListener(映射'zoom_changed',函数()
{
如果(map.getZoom()>15){
设置标记(地图、点);
}否则{
隐藏标记(地图、点);
}
});
}
变量点=[
[位置1',52.420891,6.280204],
[位置2',52.420125,6.279131],
[位置3',52.420125,6.240125]
];
功能设置标记(地图、位置){
var image=new google.maps.MarkerImage('../images/map/beachflag.png',
新谷歌地图大小(20,32),
新google.maps.Point(0,0),
新的google.maps.Point(0,32));
var shadow=new google.maps.MarkerImage('../images/map/beachflag_shadow.png',
新谷歌地图大小(37,32),
新google.maps.Point(0,0),
新的google.maps.Point(0,32));
变量形状={
坐标:[1,1,1,20,18,20,18,1],
类型:“poly”
};
对于(变量i=0;i
有人能帮我做些什么吗?最简单的方法可能是稍微修改代码,使标记包含在一个数组中,该数组可供
setMarkers
和hideMarkers
函数访问。然后,您可以使用的setMap
方法从地图中添加/删除标记(将null
传递到setMap
从地图中删除标记):
var标记=[];
函数createMarkers(/*一些参数*/){
//创建标记,并将每个标记添加到“标记”数组中
}
函数setMarkers(){
对于(var i=0;i
这种方法还意味着您不必在每次需要显示时都重新创建所有的
标记实例。我修改了您的代码。我保留数组中所有标记的引用。在隐藏标记器内部,我将它们的映射设置为null,以将它们从映射中删除
function initialize() {
var mapOptions = {
zoom : 15,
center : new google.maps.LatLng(52.429236, 6.281255),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var markers = setMarkers(map, access_points);
google.maps.event.addListener(map, 'zoom_changed', function() {
if (map.getZoom() > 15) {
setMarkers(map, access_points);
}
else {
hideMarkers(map, access_points, markers);
}
});
}
var access_points = [ [ 'Location 1', 52.420891, 6.280204 ], [ 'Location 2', 52.420125, 6.279131 ], [ 'Location 3', 52.420125, 6.240125 ] ];
function setMarkers(map, locations) {
var markers= [];
var image = new google.maps.MarkerImage('../images/map/beachflag.png', new google.maps.Size(20, 32), new google.maps.Point(0, 0), new google.maps.Point(0,
32));
var shadow = new google.maps.MarkerImage('../images/map/beachflag_shadow.png', new google.maps.Size(37, 32), new google.maps.Point(0, 0),
new google.maps.Point(0, 32));
var shape = {
coord : [ 1, 1, 1, 20, 18, 20, 18, 1 ],
type : 'poly'
};
for ( var i = 0; i < locations.length; i++) {
var access_point = locations[i];
var myLatLng = new google.maps.LatLng(access_point[1], access_point[2]);
var marker = new google.maps.Marker({
position : myLatLng,
map : map,
shadow : shadow,
icon : image,
shape : shape,
title : access_point[0],
zIndex : access_point[3]
});
markers.push(marker);
}
return markers;
}
function hideMarkers(map, locations, markers) {
/* Remove All Markers */
while(markers.length){
markers.pop().setMap(null);
}
console.log("Remove All Markers");
}
函数初始化(){
变量映射选项={
缩放:15,
中心:新google.maps.LatLng(52.429236,6.281255),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById(“map_canvas”),mapOptions);
var标记=设置标记(地图、访问点);
google.maps.event.addListener(映射'zoom_changed',函数(){
如果(map.getZoom()>15){
设置标记(地图、访问点);
}
否则{
隐藏标记(地图、访问点、标记);
}
});
}
风险值访问点=[[位置1',52.420891,6.280204],“位置2',52.420125,6.279131],“位置3',52.420125,6.240125];
功能设置标记(地图、位置){
var标记=[];
var image=new google.maps.MarkerImage('../images/map/beachflag.png',new google.maps.Size(20,32),new google.maps.Point(0,0),new google.maps.Point(0,
32));
var shadow=new google.maps.MarkerImage('../images/map/beachflag_shadow.png',new google.maps.Size(37,32),new google.maps.Point(0,0),
新的google.maps.Point(0,32));
变量形状={
坐标:[1,1,1,20,18,20,18,1],
类型:“poly”
};
对于(变量i=0;i
function initialize() {
var mapOptions = {
zoom : 15,
center : new google.maps.LatLng(52.429236, 6.281255),
mapTypeId : google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var markers = setMarkers(map, access_points);
google.maps.event.addListener(map, 'zoom_changed', function() {
if (map.getZoom() > 15) {
setMarkers(map, access_points);
}
else {
hideMarkers(map, access_points, markers);
}
});
}
var access_points = [ [ 'Location 1', 52.420891, 6.280204 ], [ 'Location 2', 52.420125, 6.279131 ], [ 'Location 3', 52.420125, 6.240125 ] ];
function setMarkers(map, locations) {
var markers= [];
var image = new google.maps.MarkerImage('../images/map/beachflag.png', new google.maps.Size(20, 32), new google.maps.Point(0, 0), new google.maps.Point(0,
32));
var shadow = new google.maps.MarkerImage('../images/map/beachflag_shadow.png', new google.maps.Size(37, 32), new google.maps.Point(0, 0),
new google.maps.Point(0, 32));
var shape = {
coord : [ 1, 1, 1, 20, 18, 20, 18, 1 ],
type : 'poly'
};
for ( var i = 0; i < locations.length; i++) {
var access_point = locations[i];
var myLatLng = new google.maps.LatLng(access_point[1], access_point[2]);
var marker = new google.maps.Marker({
position : myLatLng,
map : map,
shadow : shadow,
icon : image,
shape : shape,
title : access_point[0],
zIndex : access_point[3]
});
markers.push(marker);
}
return markers;
}
function hideMarkers(map, locations, markers) {
/* Remove All Markers */
while(markers.length){
markers.pop().setMap(null);
}
console.log("Remove All Markers");
}