ポリライン(連続直線)のクリックイベントを捕捉する

説明

ポリライン(連続直線)のクリックイベントを捕捉するにはgoogle.maps.event.addListener()を使ってイベントを設定します。google.maps.event.addListener()の最初のパラメータにイベントを設定したいポリラインオブジェクトを、2番目のパラメータにはイベント名であるclickの文字を指定します。3番目のパラメータにイベントが発生した際に実行する関数(イベントハンドラ)を設定します。

Google Maps APIプログラミング入門 Google Maps API プログラミング入門。Google Maps API Expertである勝又雅史氏が最新のAPI ver3やGoogle Maps for Flashなどについて解説しています。
アマゾンで購入する

サンプルコード [実行]

<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>Google Maps API ver 3 Sample/グーグルマップAPIサンプル/Google Maps API样品</title>
<link rel="stylesheet" href="css/main.css" type="text/css" media="all">
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
</head>
<body>
<div id="gmap"></div>
<div id="result">---</div>
<script type="text/javascript">
var shinjyuku = new google.maps.LatLng(35.689160610317174, 139.70083951950073);
var map = new google.maps.Map(
document.getElementById("gmap"),{
zoom : 7,
center : shinjyuku,
mapTypeId : google.maps.MapTypeId.ROADMAP
}
);
var myPath = new google.maps.Polyline({
path: [
new google.maps.LatLng(35.689160610317174, 139.70083951950073),
new google.maps.LatLng(37.689160610317174, 139.70083951950073),
new google.maps.LatLng(36.689160610317174, 138.70083951950073)
]
});
myPath.setMap(map);
google.maps.event.addListener(myPath, "click", function() {
document.getElementById("result").innerHTML += "<br>clickイベント発生:"+(new Date());
});
</script>
</body>
</html>