ポリラインの線の色を後から設定する

説明

ポリラインの線の色を後から設定するにはポリラインオブジェクトのset()メソッドを使います。色を設定する場合はget("strokeColor", "色名やカラーコード")とします。

Google API Expertが解説する Google Maps APIプログラミングガイド Google API Expertが解説する Google Maps APIプログラミングガイド。Google Maps API Expertが最新のGoogle Maps API等について解説しています。
アマゾンで購入する

サンプルコード [実行]

<!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>
<script type="text/javascript">
var map = new google.maps.Map(
document.getElementById("gmap"),{
zoom : 7,
center : new google.maps.LatLng(35.689160610317174, 139.70083951950073),
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)
],
strokeOpacity: 0.5,
strokeWeight: 20,
strokeColor: "black"
});
myPath.setMap(map);
// Polylineの色を定期的に変更する処理
var colorList = ["red","blue","green","yellow","#fff","#0ff"];
setInterval(function(){
myPath.set("strokeColor", colorList[0]); // ポリラインの色を設定
colorList.push(colorList.shift()); // 配列を回転
}, 250);
</script>
</body>
</html>