Add a water polygon¶
Add a water polygon with Entity API
add_water_polygon.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Add a water polygon</title>
<meta property="og:description" content="Add a water polygon with Entity API" />
<meta name="mapray-version" content="0.10.1">
<meta name="mapray-api-status" content="experimental">
<script src="./v0.10.6/mapray.min.js"></script>
<script src="./v0.10.6/maprayui.min.js"></script>
<link rel="stylesheet" href="./v0.10.6/mapray.css">
<style>
body {margin: 0; padding: 0;}
html, body, div#mapray-container { height: 100%; }
.caption {
position: absolute; top: 0; left: 0; margin: 10px;
font: 13px/1.5 sans-serif; background: rgba(255,255,255,0.85);
border-radius: 3px; padding: 8px 10px; width: 16em;
}
.caption input { width: 100%; }
</style>
</head>
<body>
<div id="mapray-container"></div>
<div class="caption">
<label for="time">2026-06-21 <span id="time_value">18:40</span> JST</label>
<input type="range" id="time" min="960" max="1170" step="5" value="1120">
</div>
</body>
</html>
<script>
const cloudApi = new mapray.cloud.CloudApiV2({
tokenType: mapray.cloud.CloudApi.TokenType.ACCESS_TOKEN,
token: "<YOUR_MAPRAY_ACCESS_TOKEN>"
});
// Create viewer
const uiviewer = new maprayui.StandardUIViewer( "mapray-container", {
dem_provider: new mapray.StandardDemProvider( cloudApi.getDefaultDemAsResource() ),
sun_visualizer: new mapray.SunVisualizer( 32 ),
atmosphere: new mapray.Atmosphere()
} );
// Setup camera position
uiviewer.setCameraPosition( {
longitude: 139.025780,
latitude: 35.193548,
height: 773.081850,
} );
uiviewer.setLookAtPosition( {
longitude: 139.020532,
latitude: 35.197264,
height: 724.5,
} );
// Create WaterPolygonEntityVisualizer
const waterPolygonEntityVisualizer = new mapray.WaterPolygonEntityVisualizer();
// Create and Add WaterPolygonEntity
const waterPolygonEntity = new mapray.WaterPolygonEntity( uiviewer.viewer.scene );
waterPolygonEntity.altitude_mode = mapray.AltitudeMode.ABSOLUTE;
waterPolygonEntity.setVisualizer( waterPolygonEntityVisualizer );
waterPolygonEntity.addOuterBoundary( [
138.97781691339844, 35.235532645650316, 728.0,
138.9840645149059, 35.23800239288863, 728.0,
138.98526768243218, 35.23879309100957, 728.0,
138.98667262862796, 35.23935825011423, 728.0,
138.98773803854797, 35.23905522498528, 728.0,
138.9902448141511, 35.23825038096923, 728.0,
138.991714137299, 35.238197231358676, 728.0,
138.99319493481565, 35.237663433629244, 728.0,
138.9953599309971, 35.2373858941149, 728.0,
138.9967977385083, 35.2369396897476, 728.0,
138.99844257559621, 35.23643495614961, 728.0,
139.00124460646938, 35.23532763563798, 728.0,
139.00373780211888, 35.23419111209799, 728.0,
139.0137643012769, 35.23096761486226, 728.0,
139.03687374637494, 35.21098346424906, 728.0,
139.03692114157428, 35.19668248606167, 728.0,
139.0313799008832, 35.18467463558891, 728.0,
139.0189806978822, 35.17886221869787, 728.0,
139.00267571465494, 35.18200236548421, 728.0,
138.99251961020272, 35.19223120279554, 728.0,
138.9847557375266, 35.208946027386745, 728.0,
138.97804529082168, 35.225931671613914, 728.0,
] );
uiviewer.viewer.scene.addEntity( waterPolygonEntity );
/**
* Sun direction in GOCS (ECEF) for a UTC time.
* Meeus ch.25 low-precision solar position + GMST rotation (error < 0.02 deg).
*/
function sunDirectionGocs( date ) {
const DEG = Math.PI / 180;
const jd = date.getTime() / 86400000 + 2440587.5;
const t = ( jd - 2451545.0 ) / 36525.0;
const L0 = 280.46646 + 36000.76983 * t;
const M = ( 357.52911 + 35999.05029 * t ) * DEG;
const C = 1.914602 * Math.sin( M ) + 0.019993 * Math.sin( 2 * M ) + 0.000289 * Math.sin( 3 * M );
const lambda = ( L0 + C ) * DEG;
const eps = ( 23.4392911 - 0.0130042 * t ) * DEG;
const ra = Math.atan2( Math.cos( eps ) * Math.sin( lambda ), Math.cos( lambda ) );
const dec = Math.asin( Math.sin( eps ) * Math.sin( lambda ) );
let gmst = ( 280.46061837 + 360.98564736629 * ( jd - 2451545.0 ) ) % 360;
if ( gmst < 0 ) gmst += 360;
const lon = ra - gmst * DEG;
return [ Math.cos( dec ) * Math.cos( lon ), Math.cos( dec ) * Math.sin( lon ), Math.sin( dec ) ];
}
// The view faces a bearing of 311 deg, towards Mt. Fuji. Around the summer
// solstice the sun sets at about 298 deg near 19:00, just left of the view centre.
const time = document.getElementById( "time" );
const updateSun = function () {
const minutes = Number( time.value );
const hh = String( Math.floor( minutes / 60 ) ).padStart( 2, "0" );
const mm = String( minutes % 60 ).padStart( 2, "0" );
document.getElementById( "time_value" ).textContent = hh + ":" + mm;
uiviewer.viewer.sun.setSunDirection( sunDirectionGocs( new Date( "2026-06-21T" + hh + ":" + mm + ":00+09:00" ) ) );
};
time.addEventListener( "input", updateSun );
updateSun();
</script>
Info
このサンプルコードは、<YOUR_MAPRAY_ACCESS_TOKEN>を、あなたのMapray CloudのアカウントのOrganization Tokenに置き換えるまで、期待通りに動作しません。