SYNCERのロゴ
アイキャッチ画像

Polygon: mouseup

mouseupは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( "mouseup", function ( argument ) {
	console.log( argument ) ;
} ) ;

サンプルコード

設定例

// イベントの設定 (基本)
var listener = google.maps.event.addListener( polygon, "mouseup", callbackFunc ) ;

// イベントの設定 (ショートカット版)
var listener = polygon.addListener( "mouseup", callbackFunc ) ;

// イベントの設定 (1度だけ発火)
google.maps.event.addListenerOnce( polygon, "mouseup", callbackFunc ) ;

// トリガーで任意のタイミングで発火
google.maps.event.trigger( polygon, "mouseup" ) ;

// イベントの解除 (リスナー変数で指定)
google.maps.event.removeListener( listener ) ;

// イベントの解除 (インスタンスとイベント名で指定)
google.maps.event.clearListeners( polygon, "mouseup" ) ;

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( "mouseup", 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>

デモページを開く

  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2015年9月1日 (火)
コンテンツを公開しました。