InfoWindow.open()
InfoWindow.open()はInfoWindowクラスのメソッドです。情報ウィンドウを開きます。
構文
パラメータ
map
第1引数には設置したい地図、またはストリートビューをインスタンスで指定する。マーカーに紐付けたい場合は、第2引数でマーカーのインスタンスを指定する。
anchor
*
地図上ではなく、マーカーなどのオブジェクトに乗せたい場合は、そのインスタンスを指定する。
返り値
返り値はありません。
デモ
メソッドを実行すると、情報ウィンドウを開きます。
var infoWindow = new google.maps.InfoWindow( {
position: new google.maps.LatLng( 35.708194, 139.808565 ) ,
content: "SYNCER" ,
} ) ;
// メソッドを実行
infoWindow.open( map ) ;
サンプルコード
<!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, 139 ) ,
zoom: 15 ,
} ) ;
// InfoWindow
var infoWindow = new google.maps.InfoWindow( {
position: new google.maps.LatLng( 35, 139 ) ,
content: "SYNCER" ,
} ) ;
// set center
map.setCenter( infoWindow.getPosition() ) ;
// Method
document.getElementById( "method" ).onclick = function () {
var response = infoWindow.open( map ) ;
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>