Marker.setOptions()
Marker.setOptions()はMarkerクラスのメソッドです。マーカーの各種設定をまとめて変更できます。
構文
パラメータ
options
マーカーのインスタンスを作成する時に引数に指定するのと同じ形式の、MapOptionsオブジェクト。
返り値
返り値はありません。
デモ
メソッドを実行すると、マーカーの各種設定をまとめて変更します。
var marker = new google.maps.Marker( {
map: map ,
position: new google.maps.LatLng( 43.0611, 141.3564 ) ,
} ) ;
// メソッドを実行
var options = {
map: map ,
position: new google.maps.LatLng( 42.0611, 140.3564 ) ,
anchorPoint: new google.maps.Point( -50, 0 ) ,
animation: google.maps.Animation.BOUNCE ,
clickable: false ,
crossOnDrag: false ,
cursor: "url(./cursor.png), auto" ,
draggable: true ,
icon: "./marker.png" ,
label: "SYNCER" ,
opacity: 0.5 ,
title: "SYNCER" ,
visible: false ,
} ;
map.setOptions( options ) ;
サンプルコード
<!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( 43.0686601740126, 141.35075529999995 ) ,
zoom: 7 ,
} ) ;
// Marker
var marker = new google.maps.Marker( {
map: map ,
position: map.getCenter() ,
} ) ;
// Method
document.getElementById( "method" ).onclick = function () {
var options = {
// map: null ,
position: new google.maps.LatLng( 42.0611, 140.3564 ) ,
anchorPoint: new google.maps.Point( -50, 0 ) ,
animation: google.maps.Animation.BOUNCE ,
clickable: false ,
crossOnDrag: false ,
cursor: "https://lab.syncer.jp/Web/API/Google_Maps/JavaScript/Marker/setOptions/cursor.png" ,
draggable: true ,
icon: "https://lab.syncer.jp/Web/API/Google_Maps/JavaScript/Marker/setOptions/marker.png" ,
label: "SYNCER" ,
opacity: 0.5 ,
title: "SYNCER" ,
// visible: false ,
} ;
var response = marker.setOptions( options ) ;
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>