線を表示する
実行結果
次の例では、線を描画します。
ソースコードと解説
線を描画するには、ZDC.Polylineクラスと、ZDC.MapクラスのaddWidget(widget)を利用します。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- ローダーのURLをscriptでロードする -->
<script src="https://{DOMAIN}/zma_loader.js?key=[APIキー]&auth=[認証方式]"></script>
<script type="text/javascript">
// オブジェクト
var map;
// 中心点の緯度経度(東京駅)
const lat = 35.681406, lng = 139.767132;
// ZMALoader
ZMALoader.setOnLoad(function (mapOptions, error) {
if (error) {
console.error(error)
return
}
// マップオプションをデフォルト値から変更する
mapOptions.center = new ZDC.LatLng(lat, lng); // 中心点の緯度経度を設定
mapOptions.zoom = 10;
// 地図を生成
map = new ZDC.Map(
document.getElementById('ZMap'),
mapOptions,
function() {
// Success callback
/* 線の頂点を作成 */
var latlngs = [];
latlngs.push(new ZDC.LatLng(35.70, 139.63));
latlngs.push(new ZDC.LatLng(35.70, 139.72));
latlngs.push(new ZDC.LatLng(35.66, 139.65));
/* ★線を作成 */
var pli = new ZDC.Polyline(
latlngs,
{
color: '#FF0000',
width: 2
}
);
/* 線を地図に追加 */
map.addWidget(pli);
},
function() {
// Failure callback
}
);
});
</script>
</head>
<body>
<div id="ZMap" style="position: relative; max-width: calc(510px * 16 / 9); height:510px; border:1px solid #777777;"></div>
</body>
</html>