Loading...
Searching...
No Matches
LocationWindowCameraExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
3import com.navigine.idl.java.LocationWindow;
4import com.navigine.idl.java.Camera;
5import com.navigine.idl.java.CameraListener;
6import com.navigine.idl.java.CameraCallback;
7import com.navigine.idl.java.AnimationType;
8import com.navigine.idl.java.CameraUpdateReason;
9import com.navigine.idl.java.Point;
10
11import java.util.ArrayList;
12
19 private LocationWindow locationWindow;
20 private CameraListener cameraListener;
21
23 demonstrateLocationWindowCameraMethods();
24 }
25
29 private void demonstrateLocationWindowCameraMethods() {
30 System.out.println("=== LocationWindowCamera Methods ===");
31
32 demonstrateCameraProperties();
33 demonstrateGetSetCamera();
34 demonstrateCameraListeners();
35 demonstrateFlyToMethod();
36 demonstrateMoveToMethod();
37 demonstrateZoomProperties();
38 demonstrateCameraScenarios();
39 }
40
44 private void demonstrateCameraProperties() {
45 System.out.println("--- Camera Properties Setup ---");
46
47 if (locationWindow == null) {
48 System.out.println("LocationWindow not available yet");
49 return;
50 }
51
52 // [java_LocationWindow_minZoomFactor]
53 // Set minimum zoom factor
54 locationWindow.setMinZoomFactor(10.0f);
55 System.out.println("Set minimum zoom factor to 10.0 pixels per meter");
56 // [java_LocationWindow_minZoomFactor]
57
58 // [java_LocationWindow_maxZoomFactor]
59 // Set maximum zoom factor
60 locationWindow.setMaxZoomFactor(1000.0f);
61 System.out.println("Set maximum zoom factor to 1000.0 pixels per meter");
62 // [java_LocationWindow_maxZoomFactor]
63 }
64
68 private void demonstrateGetSetCamera() {
69 System.out.println("--- getCamera and setCamera Methods ---");
70
71 if (locationWindow == null) {
72 System.out.println("LocationWindow not available yet");
73 return;
74 }
75
76 // [java_LocationWindow_getCamera]
77 // Get current camera position
78 Camera currentCamera = locationWindow.getCamera();
79 System.out.println("Current camera position:");
80 System.out.println(" Point: (" + currentCamera.point.x + ", " + currentCamera.point.y + ")");
81 System.out.println(" Zoom: " + currentCamera.zoom);
82 System.out.println(" Rotation: " + currentCamera.rotation + "°");
83 System.out.println(" Tilt: " + currentCamera.tilt + "° (0° = straight downward / map from above)");
84 // [java_LocationWindow_getCamera]
85
86 // [java_Camera_constructor]
87 // Create camera with constructor
88 Point newPoint = new Point(100.0, 200.0);
89 Camera newCamera = new Camera(newPoint, 50.0, 0.0, 0.0);
90 System.out.println("Created camera with point (" + newPoint.x + ", " + newPoint.y + "), zoom 50.0, rotation 0°, tilt 0°");
91 // [java_Camera_constructor]
92
93 // [java_Camera_access]
94 // Access camera properties
95 System.out.println("Camera properties:");
96 System.out.println(" Point: (" + newCamera.point.x + ", " + newCamera.point.y + ")");
97 System.out.println(" Zoom: " + newCamera.zoom);
98 System.out.println(" Rotation: " + newCamera.rotation + "°");
99 System.out.println(" Tilt: " + newCamera.tilt + "°");
100 // [java_Camera_access]
101
102 // [java_LocationWindow_setCamera]
103 // Set camera position without animation
104 locationWindow.setCamera(newCamera);
105 System.out.println("Set camera position to (" + newPoint.x + ", " + newPoint.y + ") with zoom 50.0, rotation 0°, tilt 0°");
106 // [java_LocationWindow_setCamera]
107
108 // [java_LocationWindow_setCamera_2]
109 // Set camera position with rotation
110 Point rotatedPoint = new Point(150.0, 250.0);
111 Camera rotatedCamera = new Camera(rotatedPoint, 75.0, 45.0, 0.0);
112 locationWindow.setCamera(rotatedCamera);
113 System.out.println("Set camera position to (" + rotatedPoint.x + ", " + rotatedPoint.y + ") with zoom 75.0 and rotation 45°");
114 // [java_LocationWindow_setCamera_2]
115
116 Camera tiltedCamera = new Camera(rotatedPoint, 75.0, 45.0, 40.0);
117 locationWindow.setCamera(tiltedCamera);
118 System.out.println("Set camera with tilt " + tiltedCamera.tilt + "° for a perspective view");
119
120 // Test different camera positions
121 Camera[] testCameras = {
122 new Camera(new Point(0.0, 0.0), 10.0, 0.0, 0.0), // Overview camera
123 new Camera(new Point(50.0, 75.0), 500.0, 0.0, 0.0), // Detail camera
124 new Camera(new Point(100.0, 150.0), 100.0, 30.0, 0.0), // Angled camera
125 };
126
127 for (int i = 0; i < testCameras.length; i++) {
128 Camera camera = testCameras[i];
129 locationWindow.setCamera(camera);
130 System.out.println("Test camera " + i + ": Point (" + camera.point.x + ", " + camera.point.y + "), Zoom " + camera.zoom + ", Rotation " + camera.rotation + "°, Tilt " + camera.tilt + "°");
131 }
132 }
133
137 private void demonstrateCameraListeners() {
138 System.out.println("--- Camera Listener Methods ---");
139
140 if (locationWindow == null) {
141 System.out.println("LocationWindow not available yet");
142 return;
143 }
144
145 // [java_LocationWindow_addCameraListener]
146 // Add camera listener
147 cameraListener = new CameraListenerImpl();
148 locationWindow.addCameraListener(cameraListener);
149 System.out.println("Added camera listener");
150 // [java_LocationWindow_addCameraListener]
151
152 // [java_LocationWindow_removeCameraListener]
153 // Remove camera listener
154 locationWindow.removeCameraListener(cameraListener);
155 cameraListener = null;
156 System.out.println("Removed camera listener");
157 // [java_LocationWindow_removeCameraListener]
158
159 // Test multiple listeners
160 CameraListener[] listeners = {
161 new CameraListenerImpl(),
162 new CameraListenerImpl(),
163 new CameraListenerImpl(),
164 };
165
166 for (int i = 0; i < listeners.length; i++) {
167 locationWindow.addCameraListener(listeners[i]);
168 System.out.println("Added camera listener " + i);
169 }
170
171 for (int i = 0; i < listeners.length; i++) {
172 locationWindow.removeCameraListener(listeners[i]);
173 System.out.println("Removed camera listener " + i);
174 }
175 }
176
180 private void demonstrateFlyToMethod() {
181 System.out.println("--- flyTo Method ---");
182
183 if (locationWindow == null) {
184 System.out.println("LocationWindow not available yet");
185 return;
186 }
187
188 // [java_LocationWindow_flyTo]
189 // Fly to position with smooth animation
190 Point targetPoint = new Point(150.0, 250.0);
191 Camera targetCamera = new Camera(targetPoint, 75.0, 45.0, 30.0);
192 CameraCallback callback = new CameraCallbackImpl();
193 locationWindow.flyTo(targetCamera, 2000, callback);
194 System.out.println("Started fly to animation to point (" + targetPoint.x + ", " + targetPoint.y + ")");
195 // [java_LocationWindow_flyTo]
196
197 // [java_LocationWindow_flyTo_2]
198 // Fly to another position with different duration
199 Point targetPoint2 = new Point(300.0, 400.0);
200 Camera targetCamera2 = new Camera(targetPoint2, 25.0, 180.0, 0.0);
201 CameraCallback callback2 = new CameraCallbackImpl();
202 locationWindow.flyTo(targetCamera2, 3000, callback2);
203 System.out.println("Started fly to animation to point (" + targetPoint2.x + ", " + targetPoint2.y + ") with 3 second duration");
204 // [java_LocationWindow_flyTo_2]
205
206 // Test fly to with different parameters
207 Object[][] flyToTests = {
208 {new Point(50.0, 50.0), 100.0, 0.0, 1000},
209 {new Point(200.0, 300.0), 200.0, 90.0, 1500},
210 {new Point(400.0, 100.0), 50.0, 270.0, 2500},
211 };
212
213 for (int i = 0; i < flyToTests.length; i++) {
214 Point point = (Point) flyToTests[i][0];
215 double zoom = (Double) flyToTests[i][1];
216 double rotation = (Double) flyToTests[i][2];
217 int duration = (Integer) flyToTests[i][3];
218
219 Camera camera = new Camera(point, zoom, rotation, 0.0);
220 CameraCallback testCallback = new CameraCallbackImpl();
221 locationWindow.flyTo(camera, duration, testCallback);
222 System.out.println("Fly to test " + i + ": Point (" + point.x + ", " + point.y + "), Zoom " + zoom + ", Rotation " + rotation + "°, Duration " + duration + "ms");
223 }
224 }
225
229 private void demonstrateMoveToMethod() {
230 System.out.println("--- moveTo Method ---");
231
232 if (locationWindow == null) {
233 System.out.println("LocationWindow not available yet");
234 return;
235 }
236
237 // [java_LocationWindow_moveTo]
238 // Move to position with linear animation
239 Point targetPoint = new Point(200.0, 300.0);
240 Camera targetCamera = new Camera(targetPoint, 100.0, 90.0, 0.0);
241 CameraCallback callback = new CameraCallbackImpl();
242 locationWindow.moveTo(targetCamera, 1500, AnimationType.LINEAR, callback);
243 System.out.println("Started move to with linear animation");
244 // [java_LocationWindow_moveTo]
245
246 // [java_LocationWindow_moveTo_2]
247 // Move to position with cubic animation
248 CameraCallback callback2 = new CameraCallbackImpl();
249 locationWindow.moveTo(targetCamera, 1500, AnimationType.CUBIC, callback2);
250 System.out.println("Started move to with cubic animation");
251 // [java_LocationWindow_moveTo_2]
252
253 // [java_LocationWindow_moveTo_3]
254 // Move to position with sine animation
255 CameraCallback callback3 = new CameraCallbackImpl();
256 locationWindow.moveTo(targetCamera, 1500, AnimationType.SINE, callback3);
257 System.out.println("Started move to with sine animation");
258 // [java_LocationWindow_moveTo_3]
259
260 // [java_LocationWindow_moveTo_4]
261 // Move to position without animation
262 Point instantPoint = new Point(300.0, 400.0);
263 Camera instantCamera = new Camera(instantPoint, 25.0, 180.0, 0.0);
264 CameraCallback instantCallback = new CameraCallbackImpl();
265 locationWindow.moveTo(instantCamera, 0, AnimationType.NONE, instantCallback);
266 System.out.println("Executed instant move to position (" + instantPoint.x + ", " + instantPoint.y + ")");
267 // [java_LocationWindow_moveTo_4]
268
269 // Test move to with different animation types
270 // [java_AnimationType_enum]
271 AnimationType[] animationTypes = {
272 AnimationType.LINEAR,
273 AnimationType.CUBIC,
274 AnimationType.QUINT,
275 AnimationType.SINE,
276 AnimationType.NONE,
277 };
278 // [java_AnimationType_enum]
279
280 for (int i = 0; i < animationTypes.length; i++) {
281 AnimationType animationType = animationTypes[i];
282 Point testPoint = new Point(100.0 + i * 50.0, 200.0 + i * 50.0);
283 Camera testCamera = new Camera(testPoint, 50.0 + i * 10.0, i * 30.0, 0.0);
284 CameraCallback testCallback = new CameraCallbackImpl();
285 locationWindow.moveTo(testCamera, 1000, animationType, testCallback);
286 System.out.println("Move to test " + i + ": Animation " + animationType.name() + ", Point (" + testPoint.x + ", " + testPoint.y + ")");
287 }
288 }
289
293 private void demonstrateZoomProperties() {
294 System.out.println("--- Zoom Properties ---");
295
296 if (locationWindow == null) {
297 System.out.println("LocationWindow not available yet");
298 return;
299 }
300
301 // [java_LocationWindow_getZoomFactor]
302 // Get current zoom factor
303 float currentZoom = locationWindow.getZoomFactor();
304 System.out.println("Current zoom factor: " + currentZoom + " pixels per meter");
305 // [java_LocationWindow_getZoomFactor]
306
307 // [java_LocationWindow_setZoomFactor]
308 // Set new zoom factor
309 locationWindow.setZoomFactor(200.0f);
310 System.out.println("Set zoom factor to 200.0 pixels per meter");
311 // [java_LocationWindow_setZoomFactor]
312
313 // [java_LocationWindow_getMinZoomFactor]
314 // Get minimum zoom factor
315 float minZoom = locationWindow.getMinZoomFactor();
316 System.out.println("Minimum zoom factor: " + minZoom + " pixels per meter");
317 // [java_LocationWindow_getMinZoomFactor]
318
319 // [java_LocationWindow_getMaxZoomFactor]
320 // Get maximum zoom factor
321 float maxZoom = locationWindow.getMaxZoomFactor();
322 System.out.println("Maximum zoom factor: " + maxZoom + " pixels per meter");
323 // [java_LocationWindow_getMaxZoomFactor]
324
325 // Test zoom factor changes
326 float[] zoomLevels = {50.0f, 100.0f, 150.0f, 300.0f, 500.0f, 800.0f};
327
328 for (int i = 0; i < zoomLevels.length; i++) {
329 float zoom = zoomLevels[i];
330 locationWindow.setZoomFactor(zoom);
331 System.out.println("Set zoom factor to " + zoom + " pixels per meter");
332
333 // Verify the change
334 float actualZoom = locationWindow.getZoomFactor();
335 System.out.println("Actual zoom factor: " + actualZoom + " pixels per meter");
336 }
337 }
338
342 private void demonstrateCameraScenarios() {
343 System.out.println("--- Camera Scenarios ---");
344
345 if (locationWindow == null) {
346 System.out.println("LocationWindow not available yet");
347 return;
348 }
349
350 // [java_LocationWindow_camera_scenarios]
351 // Create cameras for different scenarios
352 Camera overviewCamera = new Camera(new Point(0.0, 0.0), 10.0, 0.0, 0.0);
353 Camera detailCamera = new Camera(new Point(50.0, 75.0), 500.0, 0.0, 0.0);
354 Camera angledCamera = new Camera(new Point(100.0, 150.0), 100.0, 30.0, 25.0);
355
356 System.out.println("Created cameras for different scenarios:");
357 System.out.println(" Overview camera: zoom " + overviewCamera.zoom);
358 System.out.println(" Detail camera: zoom " + detailCamera.zoom);
359 System.out.println(" Angled camera: rotation " + angledCamera.rotation + "°, tilt " + angledCamera.tilt + "°");
360 // [java_LocationWindow_camera_scenarios]
361
362 // Test scenario transitions
363 Camera[] scenarios = {overviewCamera, detailCamera, angledCamera};
364 String[] scenarioNames = {"Overview", "Detail", "Angled"};
365
366 for (int i = 0; i < scenarios.length; i++) {
367 Camera scenario = scenarios[i];
368 String name = scenarioNames[i];
369
370 CameraCallback scenarioCallback = new CameraCallbackImpl();
371 locationWindow.moveTo(scenario, 2000, AnimationType.CUBIC, scenarioCallback);
372 System.out.println("Transitioning to " + name + " scenario");
373 }
374 }
375
379 public void cleanup() {
380 if (cameraListener != null && locationWindow != null) {
381 locationWindow.removeCameraListener(cameraListener);
382 cameraListener = null;
383 System.out.println("Camera listener removed");
384 }
385 }
386}
387
391class CameraListenerImpl implements CameraListener {
392 // [java_CameraListener_onCameraPositionChanged]
393 @Override
394 public void onCameraPositionChanged(CameraUpdateReason reason, boolean finished) {
395 String reasonText = (reason == CameraUpdateReason.GESTURES) ? "user gestures" : "application";
396 String statusText = finished ? "finished" : "in progress";
397
398 System.out.println("Camera position changed: " + reasonText + ", status: " + statusText);
399
400 if (finished) {
401 System.out.println("Camera movement completed");
402 }
403 }
404 // [java_CameraListener_onCameraPositionChanged]
405}
406
410class CameraCallbackImpl implements CameraCallback {
411 // [java_CameraCallback_onMoveFinished]
412 @Override
413 public void onMoveFinished(boolean completed) {
414 if (completed) {
415 System.out.println("Camera movement completed successfully");
416 } else {
417 System.out.println("Camera movement was cancelled");
418 }
419 }
420 // [java_CameraCallback_onMoveFinished]
421}
422
426class CameraUtils {
430 public static Camera createCenteredCamera(double x, double y, double zoom) {
431 return new Camera(new Point(x, y), zoom, 0.0, 0.0);
432 }
433
437 public static Camera createRotatedCamera(double x, double y, double zoom, double rotation) {
438 return new Camera(new Point(x, y), zoom, rotation, 0.0);
439 }
440
444 public static Camera createTiltedCamera(double x, double y, double zoom, double rotation, double tilt) {
445 return new Camera(new Point(x, y), zoom, rotation, tilt);
446 }
447
451 public static boolean isValidCamera(Camera camera) {
452 return camera.zoom > 0 &&
453 camera.rotation >= 0 && camera.rotation < 360 &&
454 camera.tilt >= 0;
455 }
456
460 public static Camera createAreaCamera(double centerX, double centerY, double width, double height) {
461 // Calculate zoom based on area size
462 double zoom = 100.0 / Math.max(width, height);
463 return new Camera(new Point(centerX, centerY), zoom, 0.0, 0.0);
464 }
465
469 public static Camera createPathCamera(ArrayList<Point> points) {
470 if (points.isEmpty()) {
471 return new Camera(new Point(0.0, 0.0), 100.0, 0.0, 0.0);
472 }
473
474 // Find path center
475 double centerX = 0.0, centerY = 0.0;
476 for (Point point : points) {
477 centerX += point.x;
478 centerY += point.y;
479 }
480 centerX /= points.size();
481 centerY /= points.size();
482
483 // Calculate area size
484 double minX = Double.MAX_VALUE, minY = Double.MAX_VALUE;
485 double maxX = Double.MIN_VALUE, maxY = Double.MIN_VALUE;
486
487 for (Point point : points) {
488 minX = Math.min(minX, point.x);
489 minY = Math.min(minY, point.y);
490 maxX = Math.max(maxX, point.x);
491 maxY = Math.max(maxY, point.y);
492 }
493
494 double width = maxX - minX;
495 double height = maxY - minY;
496
497 return createAreaCamera(centerX, centerY, width, height);
498 }
499}
500
504class Main {
505 public static void main(String[] args) {
506 // Create example instance
507 LocationWindowCameraExample example = new LocationWindowCameraExample();
508
509 // Assume we have LocationWindow
510 // LocationWindow locationWindow = getLocationWindow();
511 // example.locationWindow = locationWindow;
512
513 // Demonstrate methods
514 System.out.println("=== LocationWindowCamera Examples ===");
515
516 // Cleanup
517 example.cleanup();
518
519 System.out.println("Examples ready to use!");
520 }
521}