ポリラインで線のスタイルを設定

広告
facebookボタン
googleplusボタン
twitterボタン
ダミーボタン
bloggerボタン

ポリラインの場合は、線のスタイルを設定することができます。Polylineクラスのコンストラクタの2番目の引数でstrokeStyleプロパティに対してStyleクラスのオブジェクトを指定します。

{strokeStyle:Styleクラスのオブジェクト}

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

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

var linearray = [
  new Y.LatLng(35.69411,139.699402),
  new Y.LatLng(35.160337,136.922607),
  new Y.LatLng(34.703235,136.505127)
];

var style = new Y.Style('ff0000', 10, 0.7);
var line = new Y.Polyline(linearray, {strokeStyle:style});

ymap.addFeature(line);

この場合、線の色を'ff0000'、線の太さを10ピクセル、透過度を0.7に設定してポリラインを表示します。

サンプルプログラム

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

code5_1.js

window.onload = function() {
  var ymap = new Y.Map("map");
  ymap.drawMap(new Y.LatLng(35.487511,137.8125), 7);

  var linearray1 = [
    new Y.LatLng(35.69411,139.699402),
    new Y.LatLng(35.005253,135.769043)
  ];

  var linearray2 = [
    new Y.LatLng(35.69411,139.699402),
    new Y.LatLng(36.611118,138.218994)
  ];

  var style1 = new Y.Style('ff0000', 5, 0.7, '', 'arrow');
  var style2 = new Y.Style('004f00', 8, 0.3);

  var line1 = new Y.Polyline(linearray1, {strokeStyle:style1});
  var line2 = new Y.Polyline(linearray2, {strokeStyle:style2});

  ymap.addFeature(line1);
  ymap.addFeature(line2);
}

sample5_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/code5_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>

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

p5-1

2本のポリラインを引き、それぞれに異なるスタイルを設定しています。

( Written by T.buzz.Ikura+ )

Social Button
Facebook Page