Polygon: mousemove
mousemoveはPolygonクラスのイベントです。ポリゴン上でマウスポインタを動かしている間、何度も発火します。
引数
PolyMouseEvent
コールバック関数には引数でPolyMouseEventオブジェクトが渡される。
デモ
ポリゴン上でマウスポインタを動かしてみて下さい。その間、繰り返しイベントが発火するのを確認して下さい。
var polygon = new google.maps.Polygon(
map: map ,
paths: [
[
new google.maps.LatLng( 35.5530, 134.2513 ) ,
new google.maps.LatLng( 35.5544, 134.2465 ) ,
new google.maps.LatLng( 35.5321, 134.2454 ) ,
new google.maps.LatLng( 35.5396, 134.2609 ) ,
new google.maps.LatLng( 35.5460, 134.2622 ) ,
] ,
[
new google.maps.LatLng( 35.5331, 134.2444 ) ,
new google.maps.LatLng( 35.5344, 134.2265 ) ,
new google.maps.LatLng( 35.5121, 134.2294 ) ,
new google.maps.LatLng( 35.5296, 134.2509 ) ,
] ,
] ,
editable: true ,
} ) ;
// Event
polygon.addListener( "mousemove", function ( argument ) {
console.log( argument ) ;
} ) ;
サンプルコード
設定例
// イベントの設定 (基本)
var listener = google.maps.event.addListener( polygon, "mousemove", callbackFunc ) ;
// イベントの設定 (ショートカット版)
var listener = polygon.addListener( "mousemove", callbackFunc ) ;
// イベントの設定 (1度だけ発火)
google.maps.event.addListenerOnce( polygon, "mousemove", callbackFunc ) ;
// トリガーで任意のタイミングで発火
google.maps.event.trigger( polygon, "mousemove" ) ;
// イベントの解除 (リスナー変数で指定)
google.maps.event.removeListener( listener ) ;
// イベントの解除 (インスタンスとイベント名で指定)
google.maps.event.clearListeners( polygon, "mousemove" ) ;
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 ,
} ) ;
// Polygon
var polygon = new google.maps.Polygon( {
map: map ,
paths: [
[
new google.maps.LatLng( 35.5530, 134.2513 ) ,
new google.maps.LatLng( 35.5544, 134.2465 ) ,
new google.maps.LatLng( 35.5321, 134.2454 ) ,
new google.maps.LatLng( 35.5396, 134.2609 ) ,
new google.maps.LatLng( 35.5460, 134.2622 ) ,
] ,
[
new google.maps.LatLng( 35.5331, 134.2444 ) ,
new google.maps.LatLng( 35.5344, 134.2265 ) ,
new google.maps.LatLng( 35.5121, 134.2294 ) ,
new google.maps.LatLng( 35.5296, 134.2509 ) ,
] ,
] ,
editable: true ,
} ) ;
// fit bounds
var latLngBounds = new google.maps.LatLngBounds() ;
polygon.getPaths().forEach( function ( latLngs ) {
latLngs.forEach( function ( latLng ) {
latLngBounds.extend( latLng ) ;
} ) ;
} ) ;
map.fitBounds( latLngBounds ) ;
// Event
polygon.addListener( "mousemove", 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>