「GoogleMaps API V3」 の使い方その11(さらにInfowindow)
今回はInfowindowの中にリンクを設定する方法について説明します。
また、最後にはInfowindowと同時にDIVタグを使ったレイヤーを重ねる様な別ウインドウ表示について説明します。
■Infowindowの大きさ設定
Infowindowにはオプションとして指定できる大きさの引数としてはmaxWidthしかなく、高さ指定がありません。
Infowindowに表示する内容としてのcontentに縦に長いものを渡すと 縦方向は適当に内部で処理してくれているようです。以下にその例を示します。
(尚、縦に長すぎてmapの縦に収まらない場合はスクロールバーが表示されます)
02 | <title>Google Map API v3-11</title> |
03 | <script type= "text/javascript" src= "http://maps.google.com/maps/api/js?key=MyKeyCode" charset= "utf-8" ></script> |
04 | <script type= "text/javascript" > |
05 | function initialize() { |
07 | var latlng = new google.maps.LatLng(36.062092, 136.223323); |
10 | mapTypeId: google.maps.MapTypeId.ROADMAP, |
14 | var gmap = new google.maps.Map(document.getElementById( "map" ), mapOptions); |
16 | var gmarker = new google.maps.Marker({ |
22 | var infoWindow = new google.maps.InfoWindow({ |
23 | content: "InfoWindow - test<br />" + |
24 | "<a href='http://www.google.co.jp/'>google検索</a><br />" + |
38 | google.maps.event.addListener(gmarker, 'click' , function (event) { |
40 | infoWindow.open(gmap, gmarker); |
45 | <body onload= "initialize()" > |
46 | <div id= "map" style= "width: 300px; height: 400px;" ></div> |
■DIVタグによるInfowindowの大きさ設定
Infowindowのcontentの内容をDIVタグを使って囲み、DIVのCSS宣言でwidthとheightを設定すれば表示ウインドウの大きさが指定できます。
02 | <title>Google Map API v3-11-2</title> |
03 | <style type= "text/css" > |
09 | <script type= "text/javascript" src= "http://maps.google.com/maps/api/js?key=MyKeyCode" charset= "utf-8" ></script> |
10 | <script type= "text/javascript" > |
11 | function initialize() { |
13 | var latlng = new google.maps.LatLng(36.062092, 136.223323); |
16 | mapTypeId: google.maps.MapTypeId.ROADMAP, |
20 | var gmap = new google.maps.Map(document.getElementById( "map" ), mapOptions); |
22 | var gmarker = new google.maps.Marker({ |
28 | var infoWindow = new google.maps.InfoWindow({ |
29 | content: "<div id='infowindowclass'>" + |
30 | "InfoWindow - test<br />" + |
31 | "<a href='http://www.google.co.jp/'>google検索</a>" + |
35 | google.maps.event.addListener(gmarker, 'click' , function (event) { |
37 | infoWindow.open(gmap, gmarker); |
42 | <body onload= "initialize()" > |
43 | <div id= "map" style= "width: 700px; height: 300px;" ></div> |
■DIVタグによる別レイヤーの様なウインドウ表示
マップ上にいろいろな情報を固定位置で別のウインドウで表示したくなることはよくあります。
GoogleMapの関数を使うわけではありませんが、以前GoogleMapを使ったページで組み込んだことがありますので付随情報として説明します。
マップ上のマーカーをクリックしてください。InfoWindowと同時に別ウインドウを表示します。
別ウインドウの右上の「X」マークをクリックするとウインドウを非表示にします。
マップを表示するDIVタグmapの親要素DIVを導入し、そのpositionをrelativeに設定します。
ウインドウのDIVタグであるinfo_cmdのpositionをabsoluteに設定し、topとrightで位置決めします。
更にbackground-imageでウインドウ風の画像を背景に表示することで、ウインドウの様に見えます。
このウインドウの表示・非表示はinfo_cmdのdisplayをblock・noneに切り替えることで行います。
TEST TITLE
これはテストです。
1111111111111111
222222222222222222
02 | <title>Google Map API v3-11-3</title> |
03 | <style type= "text/css" > |
14 | background-image:url(./images/area_map_cmd.gif); |
15 | background-repeat:no-repeat; |
18 | #info_cmd .info_cmd_close{ |
38 | <script type= "text/javascript" src= "http://maps.google.com/maps/api/js?key=MyKeyCode" charset= "utf-8" ></script> |
39 | <script type= "text/javascript" > |
40 | function initialize() { |
42 | var latlng = new google.maps.LatLng(36.062092, 136.223323); |
45 | mapTypeId: google.maps.MapTypeId.ROADMAP, |
49 | var gmap = new google.maps.Map(document.getElementById( "map" ), mapOptions); |
51 | var gmarker = new google.maps.Marker({ |
57 | var infoWindow = new google.maps.InfoWindow({ |
58 | content: "<div id='infowindowclass'>" + |
59 | "InfoWindow - test<br />" + |
60 | "<a href='http://www.google.co.jp/'>google検索</a>" + |
64 | google.maps.event.addListener(gmarker, 'click' , function (event) { |
66 | infoWindow.open(gmap, gmarker); |
73 | document.getElementById( 'info_cmd' ).style.display = "block" ; |
77 | document.getElementById( 'info_cmd' ).style.display = "none" ; |
81 | <body onload= "initialize()" > |
82 | <div id= "map_main" style= "width: 700px; position: relative;" > |
83 | <div id= "map" style= "width: 700px; height: 300px;" ></div> |
85 | <div class= "info_cmd_close" ><a href= "javascript:;" onClick= "closeCmd()" style= "color:#000;text-decoration:none" >×</a></div> |
86 | <div id= "info_cmd_in" > |
90 | 1111111111111111<br /> |
■関連記事
⇒
「GoogleMaps API V3」の使い方その1
⇒
「GoogleMaps API V3」の使い方その2
⇒
「GoogleMaps API V3」の使い方その3(マーカーの表示)
⇒
「GoogleMaps API V3」の使い方その4(イベント)
⇒
「GoogleMaps API V3」の使い方その5(続イベント)
⇒
「GoogleMaps API V3」の使い方その6(続々イベント)
⇒
「GoogleMaps API V3」の使い方その7(ポリライン・ポリゴン)
⇒
「GoogleMaps API V3」の使い方その8(ジオコーディング)
⇒
「GoogleMaps API V3」の使い方その9(ルート検索)
⇒
「GoogleMaps API V3」の使い方その10(ルート検索2)