Javascript 删除标记谷歌地图api。缩放时为什么再次显示标记?
删除标记时出现问题 我正确显示所有位置,但如果我使用删除标记功能单击按钮,在第一次删除时,但如果我移动地图或放大/缩小旧标记,则会再次出现。。。。? 我只想把它搬走Javascript 删除标记谷歌地图api。缩放时为什么再次显示标记?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,删除标记时出现问题 我正确显示所有位置,但如果我使用删除标记功能单击按钮,在第一次删除时,但如果我移动地图或放大/缩小旧标记,则会再次出现。。。。? 我只想把它搬走 //show location var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/'; var icons = { parking: { icon: iconBase + 'parking_lot_maps.png'
//show location
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};
var map;
var markers = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: -15.7942357,
lng: -47.8821945
}
});
var infoWin = new google.maps.InfoWindow();
markers = locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: icons[location.type].icon,
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
return marker;
});
// Add a marker clusterer to manage the markers.
var markerCluster = new MarkerClusterer(map, markers, {
imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
var locations = [{
lat: -19.9286,
lng: -43.93888,
info: "marker 1",
type: 'info',
category: 'cat1'
}, {
lat: -19.85758,
lng: -43.9668,
info: "<strong>marker 2</strong><br>ciaociao",
type: 'library',
category: 'cat1'
}
, {
lat: -18.85758,
lng: -42.9668,
info: "<strong>marker 3</strong><br>ciaociao",
type: 'library',
category: 'cat2'
} ];
//显示位置
iconBase变量https://maps.google.com/mapfiles/kml/shapes/';
变量图标={
停车场:{
图标:iconBase+“parking\u lot\u maps.png”
},
图书馆:{
图标:iconBase+“library_maps.png”
},
信息:{
图标:iconBase+“info-i_maps.png”
}
};
var映射;
var标记=[];
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
缩放:5,
中心:{
纬度:-15.7942357,
液化天然气:-47.8821945
}
});
var infoWin=new google.maps.InfoWindow();
标记=位置.map(函数(位置,i){
var marker=new google.maps.marker({
位置:位置,,
图标:图标[位置.类型]。图标,
});
google.maps.event.addListener(标记,'click',函数(evt){
infoWin.setContent(location.info);
打开(地图、标记);
})
返回标记;
});
//添加标记群集器以管理标记。
var markerCluster=新的MarkerClusterer(地图、标记、{
imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
});
}
//在阵列中的所有标记上设置贴图。
函数setMapOnAll(映射){
对于(var i=0;i标记2
ciaociao”,
键入:“库”,
类别:'cat1'
}
, {
lat:-18.85758,
液化天然气:-42.9668,
信息:“标记3
ciaociao”,
键入:“库”,
类别:'cat2'
} ];
试试这个:
function clearMarkers() {
setMapOnAll(null);
for(i=0; i<markers.length; i++){
markers[i].setMap(null);
}
}
函数clearMarkers(){
setMapOnAll(空);
对于(i=0;i试试这个:
function clearMarkers() {
setMapOnAll(null);
for(i=0; i<markers.length; i++){
markers[i].setMap(null);
}
}
函数clearMarkers(){
setMapOnAll(空);
对于(i=0;i而言,问题是我必须首先填充数组
在删除/隐藏之后
使用标记。按(标记);
而且不回来
locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: icons[location.type].icon,
map: map
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
markers.push(marker);
});
完整代码在这里
//show location
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};
var map;
var markers = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: -15.7942357,
lng: -47.8821945
}
});
var infoWin = new google.maps.InfoWindow();
locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: icons[location.type].icon,
map: map
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
markers.push(marker);
});
// Add a marker clusterer to manage the markers.
//var markerCluster = new MarkerClusterer(map, markers, {
// imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
//});
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
var locations = [{
lat: -19.9286,
lng: -43.93888,
info: "marker 1",
type: 'info',
category: 'cat1'
}, {
lat: -19.85758,
lng: -43.9668,
info: "<strong>marker 2</strong><br>ciaociao",
type: 'library',
category: 'cat1'
}
, {
lat: -18.85758,
lng: -42.9668,
info: "<strong>marker 3</strong><br>ciaociao",
type: 'library',
category: 'cat2'
} ];
//显示位置
iconBase变量https://maps.google.com/mapfiles/kml/shapes/';
变量图标={
停车场:{
图标:iconBase+“parking\u lot\u maps.png”
},
图书馆:{
图标:iconBase+“library_maps.png”
},
信息:{
图标:iconBase+“info-i_maps.png”
}
};
var映射;
var标记=[];
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
缩放:5,
中心:{
纬度:-15.7942357,
液化天然气:-47.8821945
}
});
var infoWin=new google.maps.InfoWindow();
位置。地图(功能(位置,i){
var marker=new google.maps.marker({
位置:位置,,
图标:图标[位置.类型]。图标,
地图:地图
});
google.maps.event.addListener(标记,'click',函数(evt){
infoWin.setContent(location.info);
打开(地图、标记);
})
标记器。推(标记器);
});
//添加标记群集器以管理标记。
//var markerCluster=新的MarkerClusterer(地图、标记、{
//imagePath:'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
//});
}
//在阵列中的所有标记上设置贴图。
函数setMapOnAll(映射){
对于(var i=0;i标记2
ciaociao”,
键入:“库”,
类别:'cat1'
}
, {
lat:-18.85758,
液化天然气:-42.9668,
信息:“标记3
ciaociao”,
键入:“库”,
类别:'cat2'
} ];
问题是我必须首先填充数组
在删除/隐藏之后
使用标记。按(标记);
而且不回来
locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: icons[location.type].icon,
map: map
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
markers.push(marker);
});
完整代码在这里
//show location
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var icons = {
parking: {
icon: iconBase + 'parking_lot_maps.png'
},
library: {
icon: iconBase + 'library_maps.png'
},
info: {
icon: iconBase + 'info-i_maps.png'
}
};
var map;
var markers = [];
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 5,
center: {
lat: -15.7942357,
lng: -47.8821945
}
});
var infoWin = new google.maps.InfoWindow();
locations.map(function(location, i) {
var marker = new google.maps.Marker({
position: location,
icon: icons[location.type].icon,
map: map
});
google.maps.event.addListener(marker, 'click', function(evt) {
infoWin.setContent(location.info);
infoWin.open(map, marker);
})
markers.push(marker);
});
// Add a marker clusterer to manage the markers.
//var markerCluster = new MarkerClusterer(map, markers, {
// imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'
//});
}
// Sets the map on all markers in the array.
function setMapOnAll(map) {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(map);
}
}
// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
setMapOnAll(null);
}
// Shows any markers currently in the array.
function showMarkers() {
setMapOnAll(map);
}
// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
clearMarkers();
markers = [];
}
var locations = [{
lat: -19.9286,
lng: -43.93888,
info: "marker 1",
type: 'info',
category: 'cat1'
}, {
lat: -19.85758,
lng: -43.9668,
info: "<strong>marker 2</strong><br>ciaociao",
type: 'library',
category: 'cat1'
}
, {
lat: -18.85758,
lng: -42.9668,
info: "<strong>marker 3</strong><br>ciaociao",
type: 'library',
category: 'cat2'
} ];
//显示位置
iconBase变量https://maps.google.com/mapfiles/kml/shapes/';
变量图标={
停车场:{
图标:iconBase+“parking\u lot\u maps.png”
},
图书馆:{
图标:iconBase+“library_maps.png”
},
信息:{
图标:iconBase+“info-i_maps.png”
}
};
var映射;
var标记=[];
函数initMap(){
map=new google.maps.map(document.getElementById('map'){
缩放:5,
中心:{
纬度:-15.7942357,
液化天然气:-47.8821945
}
});
var infoWin=new google.maps.InfoWindow();
位置。地图(功能(位置,i){
var marker=new google.maps.marker({
位置:位置,,
图标:图标[位置.类型]。图标,
地图:地图
});
google.maps.event.addListener(标记,'click',函数(evt){
infoWin.setContent(location.info);