Loading...
Searching...
No Matches
LocationListManagerExample.java
Go to the documentation of this file.
1package com.navigine.examples;
2
3import java.util.Map;
4
10 private NavigineSdk sdk;
11 private LocationListManager locationListManager;
12 private LocationListListener locationListListener;
13
15 initializeSdk();
16 setupLocationListListener();
17 }
18
22 private void initializeSdk() {
23 try {
24 // [java_NavigineSdk_getInstance]
25 // Get SDK instance
26 sdk = NavigineSdk.getInstance();
27 // [java_NavigineSdk_getInstance]
28
29 // [java_NavigineSdk_setUserHash]
30 // Set user hash
31 sdk.setUserHash("USER-HASH-HERE");
32 // [java_NavigineSdk_setUserHash]
33
34 // [java_NavigineSdk_setServer]
35 // Set server URL (optional)
36 sdk.setServer("https://custom.navigine.com");
37 // [java_NavigineSdk_setServer]
38
39 // [java_NavigineSdk_getLocationListManager]
40 // Get LocationListManager for working with location lists
41 locationListManager = sdk.getLocationListManager();
42 // [java_NavigineSdk_getLocationListManager]
43
44 if (locationListManager != null) {
45 System.out.println("LocationListManager successfully initialized");
46 }
47 } catch (Exception e) {
48 System.err.println("Error initializing SDK: " + e.getMessage());
49 }
50 }
51
55 private void setupLocationListListener() {
56 locationListListener = new LocationListListener() {
57 @Override
58 // [java_LocationListListener_onLocationListLoaded]
59 public void onLocationListLoaded(Map<Integer, LocationInfo> locationInfos) {
60 System.out.println("Location list loaded");
61 demonstrateLocationList(locationInfos);
62 }
63 // [java_LocationListListener_onLocationListLoaded]
64
65 @Override
66 // [java_LocationListListener_onLocationListFailed]
67 public void onLocationListFailed(java.lang.Error error) {
68 System.out.println("Location list failed: " + error.getMessage());
70 }
71 // [java_LocationListListener_onLocationListFailed]
72 };
73 }
74
79 if (locationListManager == null) {
80 System.err.println("LocationListManager not initialized");
81 return;
82 }
83
84 // [java_LocationListManager_addLocationListListener]
85 // Add location list listener
86 locationListManager.addLocationListListener(locationListListener);
87 System.out.println("Added location list listener");
88 // [java_LocationListManager_addLocationListListener]
89
90 // [java_LocationListManager_updateLocationList]
91 // Force reload location list
92 locationListManager.updateLocationList();
93 System.out.println("Requested location list update");
94 // [java_LocationListManager_updateLocationList]
95
96 // [java_LocationListManager_getLocationList]
97 // Get current location list
98 Map<Integer, LocationInfo> currentLocationList = locationListManager.getLocationList();
99 System.out.println("Current location list contains " + currentLocationList.size() + " locations");
100 demonstrateLocationList(currentLocationList);
101 // [java_LocationListManager_getLocationList]
102
103 // Simulate some time passing for location list updates
104 new Thread(() -> {
105 try {
106 Thread.sleep(2000);
107 } catch (InterruptedException e) {
108 Thread.currentThread().interrupt();
109 }
110
111 // [java_LocationListManager_removeLocationListListener]
112 // Remove location list listener
113 locationListManager.removeLocationListListener(locationListListener);
114 System.out.println("Removed location list listener");
115 // [java_LocationListManager_removeLocationListListener]
116 }).start();
117 }
118
122 public void demonstrateLocationList(Map<Integer, LocationInfo> locationInfos) {
123 if (locationInfos.isEmpty()) {
124 System.out.println("Location list is empty");
125 return;
126 }
127
128 System.out.println("=== Location List ===");
129 locationInfos.forEach((locationId, locationInfo) -> {
130 demonstrateLocationInfo(locationInfo);
131 });
132 System.out.println("====================");
133 }
134
138 public void demonstrateLocationInfo(LocationInfo locationInfo) {
139 // [java_LocationInfo_getId]
140 // Get location ID
141 int id = locationInfo.getId();
142 System.out.println("Location ID: " + id);
143 // [java_LocationInfo_getId]
144
145 // [java_LocationInfo_getVersion]
146 // Get location version
147 int version = locationInfo.getVersion();
148 System.out.println("Location version: " + version);
149 // [java_LocationInfo_getVersion]
150
151 // [java_LocationInfo_getName]
152 // Get location name
153 String name = locationInfo.getName();
154 System.out.println("Location name: " + name);
155 // [java_LocationInfo_getName]
156
157 System.out.println("---");
158 }
159
163 public void demonstrateErrorHandling(java.lang.Error error) {
164 System.out.println("Handling location list error:");
165 System.out.println(" Error message: " + error.getMessage());
166 System.out.println(" Error type: " + error.getClass().getSimpleName());
167 }
168
173 System.out.println("=== Advanced Location List Features ===");
174
175 if (locationListManager == null) {
176 return;
177 }
178
179 // Add multiple listeners
180 LocationListListener listener1 = new LocationListListener() {
181 @Override
182 public void onLocationListLoaded(Map<Integer, LocationInfo> locationInfos) {
183 System.out.println("Listener 1: Location list loaded - " + locationInfos.size() + " locations");
184 }
185
186 @Override
187 public void onLocationListFailed(java.lang.Error error) {
188 System.out.println("Listener 1: Location list failed - " + error.getMessage());
189 }
190 };
191
192 LocationListListener listener2 = new LocationListListener() {
193 @Override
194 public void onLocationListLoaded(Map<Integer, LocationInfo> locationInfos) {
195 System.out.println("Listener 2: Location list loaded - " + locationInfos.size() + " locations");
196 }
197
198 @Override
199 public void onLocationListFailed(java.lang.Error error) {
200 System.out.println("Listener 2: Location list failed - " + error.getMessage());
201 }
202 };
203
204 // [java_LocationListManager_addLocationListListener_1]
205 // Add first location list listener
206 locationListManager.addLocationListListener(listener1);
207 System.out.println("Added first location list listener");
208 // [java_LocationListManager_addLocationListListener_1]
209
210 try {
211 Thread.sleep(1000);
212 } catch (InterruptedException e) {
213 Thread.currentThread().interrupt();
214 }
215
216 // [java_LocationListManager_addLocationListListener_2]
217 // Add second location list listener
218 locationListManager.addLocationListListener(listener2);
219 System.out.println("Added second location list listener");
220 // [java_LocationListManager_addLocationListListener_2]
221
222 // Force update to trigger listeners
223 locationListManager.updateLocationList();
224
225 // Wait for location list updates
226 try {
227 Thread.sleep(3000);
228 } catch (InterruptedException e) {
229 Thread.currentThread().interrupt();
230 }
231
232 // Remove listeners
233 // [java_LocationListManager_removeLocationListListener_1]
234 // Remove first location list listener
235 locationListManager.removeLocationListListener(listener1);
236 System.out.println("Removed first location list listener");
237 // [java_LocationListManager_removeLocationListListener_1]
238
239 try {
240 Thread.sleep(1000);
241 } catch (InterruptedException e) {
242 Thread.currentThread().interrupt();
243 }
244
245 // [java_LocationListManager_removeLocationListListener_2]
246 // Remove second location list listener
247 locationListManager.removeLocationListListener(listener2);
248 System.out.println("Removed second location list listener");
249 // [java_LocationListManager_removeLocationListListener_2]
250 }
251
256 System.out.println("=== Location List Simulation ===");
257
258 // Simulate different types of locations
259 Map<Integer, LocationInfo> simulatedLocations = Map.of(
260 1001, new LocationInfo(1001, 1, "Shopping Mall"),
261 1002, new LocationInfo(1002, 2, "Office Building"),
262 1003, new LocationInfo(1003, 1, "Airport Terminal"),
263 1004, new LocationInfo(1004, 3, "University Campus")
264 );
265
266 // Simulate location list loading
267 new Thread(() -> {
268 try {
269 Thread.sleep(1000);
270 } catch (InterruptedException e) {
271 Thread.currentThread().interrupt();
272 }
273 System.out.println("Simulated location list loaded");
274 demonstrateLocationList(simulatedLocations);
275 }).start();
276
277 // Simulate location list update
278 new Thread(() -> {
279 try {
280 Thread.sleep(3000);
281 } catch (InterruptedException e) {
282 Thread.currentThread().interrupt();
283 }
284 Map<Integer, LocationInfo> updatedLocations = Map.of(
285 1001, new LocationInfo(1001, 2, "Shopping Mall - Updated"),
286 1002, new LocationInfo(1002, 3, "Office Building - Updated"),
287 1003, new LocationInfo(1003, 2, "Airport Terminal - Updated"),
288 1004, new LocationInfo(1004, 4, "University Campus - Updated"),
289 1005, new LocationInfo(1005, 1, "New Hospital")
290 );
291 System.out.println("Simulated location list updated");
292 demonstrateLocationList(updatedLocations);
293 }).start();
294 }
295
300 System.out.println("=== Location List Listener Management ===");
301
302 if (locationListManager == null) {
303 return;
304 }
305
306 // Create a test listener
307 LocationListListener testListener = new LocationListListener() {
308 @Override
309 public void onLocationListLoaded(Map<Integer, LocationInfo> locationInfos) {
310 System.out.println("Test listener received location list: " + locationInfos.size() + " locations");
311 }
312
313 @Override
314 public void onLocationListFailed(java.lang.Error error) {
315 System.out.println("Test listener received error: " + error.getMessage());
316 }
317 };
318
319 // Add listener
320 locationListManager.addLocationListListener(testListener);
321 System.out.println("Added test location list listener");
322
323 // Force update to trigger listener
324 locationListManager.updateLocationList();
325
326 // Simulate some time passing
327 new Thread(() -> {
328 try {
329 Thread.sleep(2000);
330 } catch (InterruptedException e) {
331 Thread.currentThread().interrupt();
332 }
333
334 // Remove listener
335 locationListManager.removeLocationListListener(testListener);
336 System.out.println("Removed test location list listener");
337 }).start();
338 }
339
343 public void runExample() {
344 System.out.println("=== LocationListManager Example ===");
345
350
351 // Wait a bit for location list operations
352 try {
353 Thread.sleep(5000);
354 } catch (InterruptedException e) {
355 Thread.currentThread().interrupt();
356 }
357
358 System.out.println("=== Example completed ===");
359 }
360
364 public static void main(String[] args) {
366 example.runExample();
367 }
368}