Marker.setZIndex()
Marker.setZIndex()はMarkerクラスのメソッドです。マーカーのz-orderの値を変更します。値が大きいほど、重なった時に優先的に手前に表示されます。
構文
パラメータ
zIndex
number
z-orderを数値で指定する。
返り値
返り値はありません。
デモ
メソッドを実行すると、1のマーカーのz-orderの数値を変更します。デフォルトでは、1のマーカーは1、2のマーカーは2のz-orderが設定されています。手前、奥が入れ替わるのを確認しましょう。
var marker = new google.maps.Marker( {
map: map ,
position: new google.maps.LatLng( 35.710068 , 139.8102 ) ,
label: "1" ,
zIndex: 1 ,
} ) ;
// メソッドを実行
map.setZIndex( 3 ) ;
サンプルコード
<!DOCTYPE html>
<html>
<head>
<style>
#map-canvas {
width: 600px ;
height: 600px ;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<p><button id="method">メソッドを実行</button><button id="reset">リセット</button></p>
<p><textarea id="response"></textarea></p>
<script src="//maps.googleapis.com/maps/api/js?key={APIキー}"></script>
<script>
function initialize() {
var mapDiv = document.getElementById( "map-canvas" ) ;
var responseTextarea = document.getElementById( "response" ) ;
responseTextarea.value = "" ;
// Map
var map = new google.maps.Map( mapDiv, {
center: new google.maps.LatLng( 35.710063 , 139.8106 ) ,
zoom: 17 ,
} ) ;
// Marker
var marker1 = new google.maps.Marker( {
map: map ,
position: new google.maps.LatLng( 35.710063 , 139.8106 ) ,
label: "1" ,
zIndex: 1 ,
} ) ;
var marker2 = new google.maps.Marker( {
map: map ,
position: new google.maps.LatLng( 35.710063 , 139.8107 ) ,
label: "2" ,
zIndex: 2 ,
} ) ;
// Method
document.getElementById( "method" ).onclick = function () {
var response = marker1.setZIndex( 3 ) ;
try{ response = typeof response == "object" ? JSON.stringify( response ) : response ; }catch(e){}
responseTextarea.value = response ;
console.log( response ) ;
}
}
// Reset
document.getElementById( "reset" ).onclick = initialize ;
initialize() ;
</script>
</body>
</html>