Rectangle: dragend
dragendはRectangleクラスのイベントです。レクタングルのドラッグ操作を完了した時に発火します。
引数
MouseEvent
MouseEventオブジェクトが引数で渡される。
デモ
レクタングルをドラッグ操作で動かしてみて下さい。操作を完了した時にイベントが発火するのを確認しましょう。
var rectangle = new google.maps.Rectangle( {
map: map ,
bounds: new google.maps.LatLngBounds(
new google.maps.LatLng( 33.6868, 133.2624 ) ,
new google.maps.LatLng( 33.4353, 133.7123 )
) ,
draggable: true ,
} ) ;
// Event
rectangle.addListener( "dragend", function ( argument ) {
console.log( argument ) ;
} ) ;
サンプルコード
設定例
// イベントの設定 (基本)
var listener = google.maps.event.addListener( rectangle, "dragend", callbackFunc ) ;
// イベントの設定 (ショートカット版)
var listener = rectangle.addListener( "dragend", callbackFunc ) ;
// イベントの設定 (1度だけ発火)
google.maps.event.addListenerOnce( rectangle, "dragend", callbackFunc ) ;
// トリガーで任意のタイミングで発火
google.maps.event.trigger( rectangle, "dragend" ) ;
// イベントの解除 (リスナー変数で指定)
google.maps.event.removeListener( listener ) ;
// イベントの解除 (インスタンスとイベント名で指定)
google.maps.event.clearListeners( rectangle, "dragend" ) ;
HTML
<!DOCTYPE html>
<html>
<head>
<style>
#map-canvas {
width: 600px ;
height: 600px ;
}
</style>
</head>
<body>
<div id="map-canvas"></div>
<p><textarea id="count">0</textarea></p>
<p><textarea id="argument"></textarea></p>
<script src="//maps.googleapis.com/maps/api/js?key={APIキー}"></script>
<script>
var mapDiv = document.getElementById( "map-canvas" ) ;
var countElement = document.getElementById( "count" ) ;
var argumentElement = document.getElementById( "argument" ) ;
// Map
var map = new google.maps.Map( mapDiv, {
center: new google.maps.LatLng( 35, 139 ) ,
zoom: 18 ,
} ) ;
// Rectangle
var rectangle = new google.maps.Rectangle( {
map: map ,
bounds: new google.maps.LatLngBounds(
new google.maps.LatLng( 33.6868, 133.2624 ) ,
new google.maps.LatLng( 33.4353, 133.7123 )
) ,
draggable: true ,
} ) ;
// fit bounds
map.fitBounds( rectangle.getBounds() ) ;
// Event
rectangle.addListener( "dragend", function ( argument ) {
countElement.value = Number( countElement.value ) + 1 ;
try{ argument = typeof argument == "object" ? JSON.stringify( argument ) : argument ; }catch(e){}
argumentElement.value = argument ;
console.log( argument ) ;
} ) ;
</script>
</body>
</html>