我正在尝试创建一个Google地图标记图标,该图标显示为特定标记的警报。计划是在地图加载后动态增加图标的strokeWeight。
警报的我的想法:将外部“红色”的strokeWeight从1增加到30,然后从1重新开始到30,所以看起来像是在警告红色图标。
参考:Google maps Marker icon padding between icon-fillColor and strokeColor
我的标记代码:
// Loop through our array of markers & place each one on the map
for (i = 0; i < markers.length; i++) {
if (i % 2) {
color = "red";
} else {
color = "green";
}
var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
bounds.extend(position);
var fillColor = "#4A86FE";
var stripeColor = "white";
var outsideColor = color;
var title = "My Title";
var id = i;
var label = {
text: '' + i + '',
color: "white",
fontWeight: 'bold',
fontSize: '16px',
};
var marker = new google.maps.Marker({
zIndex: 10, // bottom
id: id,
position: position,
map: map,
title: title,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 15,
fillColor: fillColor,
fillOpacity: 1.0,
strokeWeight: 6,
strokeOpacity: 0.8,
strokeColor: outsideColor,
rotation: 30
},
});
var marker = new google.maps.Marker({
zIndex: 15, // top
id: id,
position: position,
label: label,
map: map,
title: title,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 15,
fillColor: fillColor,
fillOpacity: 1.0,
strokeWeight: 2,
strokeOpacity: 0.8,
strokeColor: stripeColor,
rotation: 30
},
});
// To Bounce the selected Marker
//if(color == "red"){
// marker.setAnimation(google.maps.Animation.BOUNCE);
//}
// Marker Zoom on Click
// Automatically center the map fitting all markers on the screen
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, 'load', initialize);
尝试1:触发器确实起作用了:(
(function (i,marker){
google.maps.event.addListener(marker, 'click' , function() {
var icon = this.getIcon();
icon.strokeWeight = i;
this.setIcon(icon);
});
});(i, marker);
document.getElementById(marker).click();
尝试2:此方法有效,但适用于循环中的最后一个标记
// To Bounce the selected Marker
if(color == "red"){
setInterval(function() {
if(on) {
marker.setMap(null);
} else {
var icon = marker.getIcon();
icon.strokeWeight = 10;
marker.setIcon(icon);
marker.setMap(map);
}
on = !on;
}, intervalSeconds * 1000);
//marker.setAnimation(google.maps.Animation.BOUNCE);
}
尝试3:这很糟糕:(
marker.addListener('click', function() {
var icon = this.getIcon();
var self = this;
j = 0;
console.log('im clicked');
for (i = 1; i < 20; i++) {
icon.strokeWeight = i;
window.setTimeout(function() {
self.setIcon(icon);
}, 3000);
console.log(i);
if (i == 19) {
i = 0;
j++
}
if (j == 10) {
break;
}
}
尝试实现以下目标:
答案 0 :(得分:1)
我建议创建一个执行“警报”功能的 third 标记,将其置于其他两个标记的下方,并更改其strokeWeight以提供“脉动”:
function alertMarker(map, latLng, color) {
var intervalSeconds = 0.025;
var direction = 1;
var strokeWeight = 10;
var maxStrokeWeight = 30;
var alertMark = new google.maps.Marker({
position: latLng,
map: map,
zIndex: 5, // very bottom
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 15,
fillColor: color,
fillOpacity: 1.0,
strokeWeight: 6,
strokeOpacity: 0.8,
strokeColor: color,
rotation: 30
},
})
setInterval(function() {
if (((direction>0) && strokeWeight <maxStrokeWeight)||((direction<0) && strokeWeight>10)) {
strokeWeight+=direction;
} else if (strokeWeight==10) {
direction=1;
} else if (strokeWeight==maxStrokeWeight) {
direction=-1;
}
var icon = alertMark.getIcon();
icon.strokeWeight = strokeWeight;
icon.strokeOpacity = 0.6
alertMark.setIcon(icon);
}, intervalSeconds * 1000);
return alertMark;
}
注意:这只是一个概念证明,如果您希望这是一项长期功能,则需要保留计时器函数的句柄并取消它们,而不是全部保留在隐藏标记上在后台运行
代码段:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {
lat: -25.363882,
lng: 131.044922
}
});
var position = map.getCenter(),
i = 10;
var mark1 = makeComplexIcon(map, position, "#4A86FE", "white", "red", "My Title", i, {
text: 'id',
color: "white",
fontWeight: 'bold',
fontSize: '16px',
});
var aM = alertMarker(map, mark1.getPosition(), "red");
var mark2 = makeComplexIcon(map, {
lat: -27.6728168,
lng: 121.6283098
}, "green", "yellow", "orange", "W. Australia", 12, {
text: 'id1',
color: "blue",
fontWeight: 'bold',
fontSize: '16px',
})
var mark3 = makeComplexIcon(map, {
lat: -30.0,
lng: 136.2
}, "black", "white", "black", "S. Australia", 14, {
text: 'id2',
color: "red",
fontWeight: 'bold',
fontSize: '16px',
});
setTimeout(function() {
aM.setMap(null);
aM = alertMarker(map, mark2.getPosition(), "orange")
}, 5000)
setTimeout(function() {
aM.setMap(null);
aM = alertMarker(map, mark3.getPosition(), "black")
}, 10000)
setTimeout(function() {
aM.setMap(null);
alertMarker(map, mark1.getPosition(), "red")
}, 15000)
}
function makeComplexIcon(map, latLng, fillColor, stripeColor, outsideColor, title, id, label) {
var bottom = new google.maps.Marker({
zIndex: 10, // bottom
id: id,
position: latLng,
map: map,
title: title,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 15,
fillColor: fillColor,
fillOpacity: 1.0,
strokeWeight: 6,
strokeOpacity: 0.8,
strokeColor: outsideColor,
rotation: 30
},
});
var top = new google.maps.Marker({
zIndex: 15, // top
id: id,
position: latLng,
label: label,
map: map,
title: title,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 15,
fillColor: fillColor,
fillOpacity: 1.0,
strokeWeight: 2,
strokeOpacity: 0.8,
strokeColor: stripeColor,
rotation: 30
},
});
return bottom;
}
function alertMarker(map, latLng, color) {
var intervalSeconds = 0.025;
var direction = 1;
var strokeWeight = 10;
var maxStrokeWeight = 30;
var alertMark = new google.maps.Marker({
position: latLng,
map: map,
zIndex: 5, // very bottom
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 15,
fillColor: color,
fillOpacity: 1.0,
strokeWeight: 6,
strokeOpacity: 0.8,
strokeColor: color,
rotation: 30
},
})
setInterval(function() {
if (((direction > 0) && strokeWeight < maxStrokeWeight) || ((direction < 0) && strokeWeight > 10)) {
strokeWeight += direction;
} else if (strokeWeight == 10) {
direction = 1;
} else if (strokeWeight == maxStrokeWeight) {
direction = -1;
}
var icon = alertMark.getIcon();
icon.strokeWeight = strokeWeight;
icon.strokeOpacity = 0.6
alertMark.setIcon(icon);
}, intervalSeconds * 1000);
return alertMark;
}
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap"></script>