This repository has been archived by the owner on Mar 8, 2023. It is now read-only.
/
getting-started_orbiting-view.ts
122 lines (109 loc) · 4.13 KB
/
getting-started_orbiting-view.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* Copyright (C) 2017-2019 HERE Europe B.V.
* Licensed under Apache 2.0, see full license in LICENSE
* SPDX-License-Identifier: Apache-2.0
*/
import { GeoCoordinates, mercatorProjection, sphereProjection } from "@here/harp-geoutils";
import {
CopyrightElementHandler,
MapView,
MapViewEventNames,
MapViewUtils
} from "@here/harp-mapview";
import { APIFormat, OmvDataSource } from "@here/harp-omv-datasource";
import { GUI } from "dat.gui";
import { accessToken, copyrightInfo } from "../config";
/**
* In this example we simply use the `lookAt` method to make the camera orbit around a geolocation.
*
* First we create the map.
* ```typescript
* [[include:harp_gl_camera_orbit_example_0.ts]]
* ```
*
* Then we listen to render events to trigger new `lookAt` calls with progressing yaw angle offsets:
* ```typescript
* [[include:harp_gl_camera_orbit_example_1.ts]]
* ```
*
* Here a GUI is also set up so as to fiddle with the tilt and distance from the page.
*/
export namespace CameraOrbitExample {
// snippet:harp_gl_camera_orbit_example_0.ts
const map = createBaseMap();
// end:harp_gl_camera_orbit_example_0.ts
// Be sure to see the buildings when starting the example: a zoom level does not translate into
// the same distance depending on the viewport's height.
const minDistanceForBuildings =
Math.ceil(MapViewUtils.calculateDistanceToGroundFromZoomLevel(map, 16.0)) - 500;
// snippet:harp_gl_camera_orbit_example_1.ts
const options = { tilt: 25, distance: minDistanceForBuildings, globe: true };
const dubai = new GeoCoordinates(25.19705, 55.27419);
let heading = 0;
map.addEventListener(MapViewEventNames.AfterRender, () => {
map.lookAt(dubai, options.distance, options.tilt, (heading = (heading + 0.1) % 360));
map.update();
updateHTML();
});
// end:harp_gl_camera_orbit_example_1.ts
const gui = new GUI({ width: 300 });
gui.add(options, "tilt", 0, 80, 0.1);
gui.add(options, "distance", 300, 60000, 1);
gui.add(options, "globe").onChange(() => {
map.projection = options.globe ? sphereProjection : mercatorProjection;
});
function createBaseMap(): MapView {
document.body.innerHTML += getExampleHTML();
const canvas = document.getElementById("mapCanvas") as HTMLCanvasElement;
const mapView = new MapView({
canvas,
projection: sphereProjection,
theme: "resources/berlin_tilezen_base_globe.json"
});
canvas.addEventListener("contextmenu", e => e.preventDefault());
CopyrightElementHandler.install("copyrightNotice", mapView);
mapView.resize(window.innerWidth, window.innerHeight);
window.addEventListener("resize", () => {
mapView.resize(window.innerWidth, window.innerHeight);
});
const omvDataSource = new OmvDataSource({
baseUrl: "https://xyz.api.here.com/tiles/herebase.02",
apiFormat: APIFormat.XYZOMV,
styleSetName: "tilezen",
maxZoomLevel: 17,
authenticationCode: accessToken,
copyrightInfo
});
mapView.addDataSource(omvDataSource);
return mapView;
}
function updateHTML() {
const infoElement = document.getElementById("info") as HTMLParagraphElement;
infoElement.innerHTML =
`This view is set through the lookAt method: map.lookAt(dubai, ` +
`${options.distance.toFixed(0)}, ${options.tilt.toFixed(1)}, ${heading.toFixed(1)});`;
}
function getExampleHTML() {
return `
<style>
#mapCanvas{
top:0
}
#info{
color: #fff;
width: 80%;
left: 50%;
position: relative;
margin: 10px 0 0 -40%;
font-size: 15px;
}
@media screen and (max-width: 700px) {
#info{
font-size:11px;
}
}
</style>
<p id=info></p>
`;
}
}