情報ウィンドウにHTML文を表示

広告

情報ウィンドウでは単なるテキストだけでなくHTML文を記述してリンクを設定したり画像を表示することができます。

具体的には次のように記述します。

var ymap = new Y.Map("map");
ymap.drawMap(new Y.LatLng(35.68025,139.7693), 15);

var html = '<p>';
html += '<a href="http://www.example.com/">Text</a>';
html += '</p>';

ymap.openInfoWindow(new Y.LatLng(35.680385,139.769096), html);

表示されたリンクをクリックすれば、実際にリンク先へ移動します。

サンプルプログラム

では実際に試してみます。

code2_1.js

window.onload = function() {
  var ymap = new Y.Map("map");
  ymap.drawMap(new Y.LatLng(35.680882,139.766994), 15);

  var html = '<p>';
  html += '<img src="./img/icon.png" />';
  html += '<a href="http://www.jreast.co.jp/estation/stations/1039.html">東京駅</a>';
  html += '</p>';

  ymap.openInfoWindow(new Y.LatLng(35.680882,139.766994), html);
}

sample2_1.html

<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>

    <title>Yahoo! JavaScriptマップAPI</title>

    <script type="text/javascript" charset="utf-8"
    src="http://js.api.olp.yahooapis.jp/OpenLocalPlatform/V1/jsapi?appid=(id)">
    </script>

    <script src="./js/code2_1.js" type="text/javascript"></script>

  </head>
  <body>
    <p>
    JavaScriptマップAPIを使ったサンプルです。
    </p>

    <div id="map" style="width:400px; height:300px"></div>

    <!-- Begin Yahoo! JAPAN Web Services Attribution Snippet -->
    <a href="http://developer.yahoo.co.jp/about">
    <img src="http://i.yimg.jp/images/yjdn/yjdn_attbtn1_125_17.gif" title="Webサービス by Yahoo! JAPAN" alt="Web Services by Yahoo! JAPAN" width="125" height="17" border="0" style="margin:10px 15px 15px 15px"></a>
    <!-- End Yahoo! JAPAN Web Services Attribution Snippet -->

  </body>
</html>

ブラウザでアクセスすると次のように表示されます。

p2-1

表示されているリンクをクリックすれば、リンク先へ移動します。

p2-2

ただ全てのタグが使用できるかどうかは分かりません。

( Written by Tatsuo Ikura+ )

Facebook Page