【发布时间】:2012-12-11 15:21:37
【问题描述】:
我有一个谷歌地图,里面有很多使用 websockets 添加的标记。我正在使用基于数据可用性的自定义标记图像。我想确保最新的标记位于地图中所有其他元素的顶部。
我该怎么做?
代码:
circleIcon = {
path: google.maps.SymbolPath.CIRCLE,
fillColor: "blue",
fillOpacity: 1,
scale: 2,
strokeColor: "red",
strokeWeight: 10
};
coordinates = image[2].split(',');
pin=new google.maps.LatLng(coordinates[0], coordinates[1]);
if(marker) {
marker.setMap(null);
}
if(image[0] != "NOP") {
var markerIcon = circleIcon;
} else {
var markerIcon = image_car_icon;
}
marker=new google.maps.Marker({
position:pin,
icon:markerIcon
});
marker.setMap(map);
map.setCenter(marker.getPosition());
更新
我正在使用 jquery 来更新 InfoWindow 的 z-index,例如:
popup = new google.maps.InfoWindow({
content:'<image id="pin_' + pin_count + '" src="data:image/png;base64,' + image[1] +'"/>',
});
popup.open(map, marker);
google.maps.event.addListener(popup, 'domready', function() {
console.log("DOM READY...........................");
// Bring latest Image to top
e = $('#pin_' + pin_count).parent().parent().parent().parent();
e.css({
'z-index' : 99999 + pin_count,
});
});
也许这就是标记显示在信息窗口后面的原因。我尝试使用 zIndex 进行更新,但标记仍显示在 InfoWindow 后面:
marker=new google.maps.Marker({
position:pin,
zIndex: 9999999 + pin_count,
icon:markerIcon
});
更新
示例代码。我希望“绿色圆圈”图标标记位于“图钉”图标标记后面。
<!DOCTYPE html>
<html>
<head>
<script src="http://maps.googleapis.com/maps/api/js?key=&sensor=false"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
var london = new google.maps.LatLng(51.508742,-0.120850);
function initialize()
{
var pin1=new google.maps.LatLng(51.508742, -0.120850);
var pin2=new google.maps.LatLng(51.508642, -0.120850);
var mapProp = {
center:pin1,
zoom:17,
disableDefaultUI:true,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker=new google.maps.Marker({
position:pin2,
zIndex:99999999
});
marker.setMap(map);
var infowindow = new google.maps.InfoWindow({
content:"Hello World!"
});
infowindow.open(map,marker);
circleIcon = {
path: google.maps.SymbolPath.CIRCLE,
fillColor: "blue",
fillOpacity: 1,
scale: 2,
strokeColor: "green",
strokeWeight: 10
};
var marker2=new google.maps.Marker({
position:pin1,
icon:circleIcon,
zIndex:99999
});
marker2.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="googleMap" style="width:500px;height:380px;"></div>
</body>
</html>
【问题讨论】:
-
你查看过 API 中的
MAX_ZINDEX吗?添加标记时应该很容易增加它@987654321@ -
我可能添加的 InfoWindows 怎么样? MAX_ZINDEX 是否也将标记放在上面?
-
我试过 mark.setZIndex(google.maps.Marker.MAX_ZINDEX + 1);但即使我设置了更大的 z-index,MAX_ZINDEX 的值也不会改变。
-
自从我使用 API 以来已经有一段时间了:查看标记选项之一是:
zIndex。增加 -
@charlietfl 它不起作用。标记出现在 InfoWINdow 后面。
标签: javascript jquery html google-maps