|
30 | 30 | import java.util.TimerTask;
|
31 | 31 | import java.util.prefs.Preferences;
|
32 | 32 | import javafx.application.Platform;
|
| 33 | +import javafx.beans.value.ChangeListener; |
| 34 | +import javafx.beans.value.ObservableValue; |
| 35 | +import javafx.concurrent.Worker; |
33 | 36 | import javafx.embed.swing.JFXPanel;
|
34 | 37 | import javafx.scene.Scene;
|
35 | 38 | import javafx.scene.layout.StackPane;
|
@@ -83,7 +86,7 @@ public LocationSimulation() {
|
83 | 86 |
|
84 | 87 | Preferences p = Preferences.userNodeForPackage(com.codename1.ui.Component.class);
|
85 | 88 | int startingZoom = p.getInt("lastZoom", 9);
|
86 |
| - final String htmlpage2 = "<!DOCTYPE html>\n" |
| 89 | + final String htmlPage = "<!DOCTYPE html>\n" |
87 | 90 | + "<html>\n"
|
88 | 91 | + " <head>\n"
|
89 | 92 | + " <title>Location Simulator</title>\n"
|
@@ -156,200 +159,120 @@ public LocationSimulation() {
|
156 | 159 | + " </body>\n"
|
157 | 160 | + "</html>";
|
158 | 161 |
|
159 |
| -// final String htmlPage = "<!DOCTYPE html>\n" |
160 |
| -// + "<html>\n" |
161 |
| -// + " <head>\n" |
162 |
| -// + " <script>\n" |
163 |
| -// + "(function() {\n" + |
164 |
| -// " var lastTime = 0;\n" + |
165 |
| -// " var vendors = ['ms', 'moz', 'webkit', 'o'];\n" + |
166 |
| -// " for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {\n" + |
167 |
| -// " window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];\n" + |
168 |
| -// " window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame'] \n" + |
169 |
| -// " || window[vendors[x]+'CancelRequestAnimationFrame'];\n" + |
170 |
| -// " }\n" + |
171 |
| -// " \n" + |
172 |
| -// " if (!window.requestAnimationFrame)\n" + |
173 |
| -// " window.requestAnimationFrame = function(callback, element) {\n" + |
174 |
| -// " var currTime = new Date().getTime();\n" + |
175 |
| -// " var timeToCall = Math.max(0, 16 - (currTime - lastTime));\n" + |
176 |
| -// " var id = window.setTimeout(function() { callback(currTime + timeToCall); }, \n" + |
177 |
| -// " timeToCall);\n" + |
178 |
| -// " lastTime = currTime + timeToCall;\n" + |
179 |
| -// " return id;\n" + |
180 |
| -// " };\n" + |
181 |
| -// " \n" + |
182 |
| -// " if (!window.cancelAnimationFrame)\n" + |
183 |
| -// " window.cancelAnimationFrame = function(id) {\n" + |
184 |
| -// " clearTimeout(id);\n" + |
185 |
| -// " };\n" + |
186 |
| -// "}());\n" |
187 |
| -// + "</script>" |
188 |
| -// + " <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />\n" |
189 |
| -// + " <style type=\"text/css\">\n" |
190 |
| -// + " html { height: 100% }\n" |
191 |
| -// + " body { height: 100%; margin: 0; padding: 0 }\n" |
192 |
| -// + " #map-canvas { height: 100% }\n" |
193 |
| -// + " .gm-style-mtc > div, .gm-style > div, .gm-style-cc > div, .gm-style {font-family:sans-serif !important;}\n" |
194 |
| -// + " </style>\n" |
195 |
| -// //+ " <script>" |
196 |
| -// //+ "if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}" |
197 |
| -// //+" </script>" |
198 |
| -// + " <script type=\"text/javascript\"\n" |
199 |
| -// + " src=\"https://maps.googleapis.com/maps/api/js?key=API_KEY\">\n" |
200 |
| -// + " </script>\n" |
201 |
| -// + " <script type=\"text/javascript\">\n" |
202 |
| -// + "function moveToLocation(lat, lng){\n" |
203 |
| -// + " var center = new google.maps.LatLng(lat, lng);\n" |
204 |
| -// + " // using global variable:\n" |
205 |
| -// + " document.map.panTo(center);\n" |
206 |
| -// + "}\n" |
207 |
| -// + "function initialize() {\n" |
208 |
| -// + "var latlng = new google.maps.LatLng(40.714353, -74.005973 );\n" |
209 |
| -// + "var myOptions = {\n" |
210 |
| -// + " zoom: "+startingZoom+",\n" |
211 |
| -// + " center: latlng,\n" |
212 |
| -// + " mapTypeControl: true,\n" |
213 |
| -// + " navigationControl: true,\n" |
214 |
| -// + " streetViewControl: true,\n" |
215 |
| -// + " backgroundColor: \"#FFFFFF\"\n" |
216 |
| -// + "};\n" |
217 |
| -// + "\n" |
218 |
| -// + "document.geocoder = new google.maps.Geocoder();\n" |
219 |
| -// + "document.map = new google.maps.Map(document.getElementById(\"map_canvas\"),myOptions);\n" |
220 |
| -// + "console.log('map', document.map);\n" |
221 |
| -// + "\n" |
222 |
| -// + "document.marker = new google.maps.Marker({\n" |
223 |
| -// + " position: document.map.getCenter(),\n" |
224 |
| -// + " icon: {\n" |
225 |
| -// + " path: google.maps.SymbolPath.CIRCLE,\n" |
226 |
| -// + " scale: 5\n" |
227 |
| -// + " },\n" |
228 |
| -// + " map: document.map\n" |
229 |
| -// + " });" |
230 |
| -// + "google.maps.event.addListener(document.map, 'drag', function() { document.marker.setPosition(document.map.getCenter()); } );" |
231 |
| -// + "}" |
232 |
| -// + "document.updateJavaFX = function updateJavaFX() {\n" |
233 |
| -// + " document.currentCenter = document.map.getCenter();\n" |
234 |
| -// + " document.currentBounds = document.map.getBounds();\n" |
235 |
| -// + " document.currentHeading = document.map.getHeading();\n" |
236 |
| -// + " document.currentZoom = document.map.getZoom();\n" |
237 |
| -// + " document.marker.setPosition(document.currentCenter);\n" |
238 |
| -// + "}" |
239 |
| -// + " </script>\n" |
240 |
| -// + " </head>\n" |
241 |
| -// + " <body onload=\"initialize()\">\n" |
242 |
| -// + " <div id=\"map_canvas\" style=\"width:100%; height:100%\"></div>\n" |
243 |
| -// + " </body>" |
244 |
| -// + "</html>"; |
245 |
| - |
246 | 162 | final JFXPanel webContainer = new JFXPanel();
|
247 | 163 | mapPanel.setLayout(new BorderLayout());
|
248 | 164 | mapPanel.add(BorderLayout.CENTER, webContainer);
|
249 | 165 |
|
250 |
| - Platform.runLater(() -> { |
251 |
| - StackPane root = new StackPane(); |
252 |
| - WebView webView = new WebView(); |
253 |
| - WebEngine webEngine = webView.getEngine(); |
254 |
| - this.webView = webView; |
255 |
| - this.webEngine = webEngine; |
256 |
| - root.getChildren().add(webView); |
257 |
| - webContainer.setScene(new Scene(root)); |
258 |
| - |
259 |
| - // Load the HTML content |
260 |
| - webEngine.loadContent(htmlpage2, "text/html"); |
261 |
| - |
262 |
| - // Add a listener for the load state |
263 |
| - webEngine.getLoadWorker().stateProperty().addListener((obs, oldState, newState) -> { |
264 |
| - if (newState == javafx.concurrent.Worker.State.SUCCEEDED) { |
265 |
| - // Script is fully loaded, you can now call moveToLocation |
266 |
| - timer = new Timer(); |
267 |
| - timer.scheduleAtFixedRate(new TimerTask() { |
268 |
| - |
269 |
| - @Override |
270 |
| - public void run() { |
271 |
| - if (!isTextFieldFocused) { |
272 |
| - Platform.runLater(() -> { |
273 |
| - try { |
274 |
| - Object result = webEngine.executeScript("document.updateJavaFX()"); |
275 |
| - |
276 |
| - |
277 |
| - if (result != null) { |
278 |
| - // Parse the coordinates more safely |
279 |
| - String jsonStr = result.toString(); |
280 |
| - // Remove the curly braces and quotes |
281 |
| - jsonStr = jsonStr.replaceAll("[{}\"]", ""); |
282 |
| - |
283 |
| - // Create a map to store our values |
284 |
| - Map<String, Double> values = new HashMap<>(); |
285 |
| - |
286 |
| - // Split by comma and process each key-value pair |
287 |
| - for (String pair : jsonStr.split(",")) { |
288 |
| - try { |
289 |
| - String[] keyValue = pair.trim().split(":"); |
290 |
| - if (keyValue.length == 2) { // Make sure we have both key and value |
291 |
| - String key = keyValue[0].trim(); |
292 |
| - double value = Double.parseDouble(keyValue[1].trim()); |
293 |
| - values.put(key, value); |
| 166 | + Platform.runLater(new Runnable() { |
| 167 | + @Override |
| 168 | + public void run() { |
| 169 | + StackPane root = new StackPane(); |
| 170 | + webView = new WebView(); |
| 171 | + webEngine = webView.getEngine(); |
| 172 | + root.getChildren().add(webView); |
| 173 | + webContainer.setScene(new Scene(root)); |
| 174 | + |
| 175 | + // Load the HTML content |
| 176 | + webEngine.loadContent(htmlPage, "text/html"); |
| 177 | + |
| 178 | + // Add a listener for the load state |
| 179 | + webEngine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() { |
| 180 | + @Override |
| 181 | + public void changed(ObservableValue<? extends Worker.State> obs, |
| 182 | + Worker.State oldState, |
| 183 | + Worker.State newState) { |
| 184 | + if (newState == javafx.concurrent.Worker.State.SUCCEEDED) { |
| 185 | + // Script is fully loaded, you can now call moveToLocation |
| 186 | + timer = new Timer(); |
| 187 | + timer.scheduleAtFixedRate(new TimerTask() { |
| 188 | + @Override |
| 189 | + public void run() { |
| 190 | + if (!isTextFieldFocused) { |
| 191 | + Platform.runLater(new Runnable() { |
| 192 | + @Override |
| 193 | + public void run() { |
| 194 | + try { |
| 195 | + Object result = webEngine.executeScript("document.updateJavaFX()"); |
| 196 | + |
| 197 | + if (result != null) { |
| 198 | + // Parse the coordinates more safely |
| 199 | + String jsonStr = result.toString(); |
| 200 | + // Remove the curly braces and quotes |
| 201 | + jsonStr = jsonStr.replaceAll("[{}\"]", ""); |
| 202 | + |
| 203 | + // Create a map to store our values |
| 204 | + Map<String, Double> values = new HashMap<>(); |
| 205 | + |
| 206 | + // Split by comma and process each key-value pair |
| 207 | + for (String pair : jsonStr.split(",")) { |
| 208 | + try { |
| 209 | + String[] keyValue = pair.trim().split(":"); |
| 210 | + if (keyValue.length == 2) { // Make sure we have both key and value |
| 211 | + String key = keyValue[0].trim(); |
| 212 | + double value = Double.parseDouble(keyValue[1].trim()); |
| 213 | + values.put(key, value); |
| 214 | + } |
| 215 | + } catch (Exception e) { |
| 216 | + // Skip this pair if there's an error |
| 217 | + continue; |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + // Only update if we have both latitude and longitude |
| 222 | + if (values.containsKey("lat") && values.containsKey("lng")) { |
| 223 | + double newLat = values.get("lat"); |
| 224 | + double newLon = values.get("lng"); |
| 225 | + int zoom = values.getOrDefault("zoom", 13.0).intValue(); |
| 226 | + |
| 227 | + // Update preferences |
| 228 | + Preferences prefs = Preferences.userNodeForPackage(com.codename1.ui.Component.class); |
| 229 | + prefs.putInt("lastZoom", zoom); |
| 230 | + prefs.putDouble("lastGoodLat", newLat); |
| 231 | + prefs.putDouble("lastGoodLon", newLon); |
| 232 | + |
| 233 | + // Update the text fields |
| 234 | + latitude.setText(String.format("%.6f", newLat)); |
| 235 | + longitude.setText(String.format("%.6f", newLon)); |
| 236 | + } |
| 237 | + } |
| 238 | + } catch (Exception e) { |
| 239 | + e.printStackTrace(); |
294 | 240 | }
|
295 |
| - } catch (Exception e) { |
296 |
| - // Skip this pair if there's an error |
297 |
| - continue; |
298 | 241 | }
|
299 |
| - } |
300 |
| - |
301 |
| - // Only update if we have both latitude and longitude |
302 |
| - if (values.containsKey("lat") && values.containsKey("lng")) { |
303 |
| - double newLat = values.get("lat"); |
304 |
| - double newLon = values.get("lng"); |
305 |
| - int zoom = values.getOrDefault("zoom", 13.0).intValue(); |
306 |
| - |
307 |
| - // Update preferences |
308 |
| - Preferences prefs = Preferences.userNodeForPackage(com.codename1.ui.Component.class); |
309 |
| - prefs.putInt("lastZoom", zoom); |
310 |
| - prefs.putDouble("lastGoodLat", newLat); |
311 |
| - prefs.putDouble("lastGoodLon", newLon); |
312 |
| - |
313 |
| - // Update the text fields |
314 |
| - latitude.setText(String.format("%.6f", newLat)); |
315 |
| - longitude.setText(String.format("%.6f", newLon)); |
316 |
| - } |
| 242 | + }); |
317 | 243 | }
|
318 |
| - } catch (Exception e) { |
319 |
| - e.printStackTrace(); |
320 | 244 | }
|
321 |
| - }); |
322 |
| - } |
323 |
| - } |
324 |
| - }, 1000, 1000); |
| 245 | + }, 1000, 1000); |
325 | 246 |
|
326 |
| - // Add focus listeners to the text fields |
327 |
| - latitude.addFocusListener(new java.awt.event.FocusListener() { |
328 |
| - @Override |
329 |
| - public void focusGained(java.awt.event.FocusEvent e) { |
330 |
| - isTextFieldFocused = true; |
331 |
| - } |
| 247 | + // Add focus listeners to the text fields |
| 248 | + latitude.addFocusListener(new java.awt.event.FocusListener() { |
| 249 | + @Override |
| 250 | + public void focusGained(java.awt.event.FocusEvent e) { |
| 251 | + isTextFieldFocused = true; |
| 252 | + } |
332 | 253 |
|
333 |
| - @Override |
334 |
| - public void focusLost(java.awt.event.FocusEvent e) { |
335 |
| - isTextFieldFocused = false; |
336 |
| - } |
337 |
| - }); |
| 254 | + @Override |
| 255 | + public void focusLost(java.awt.event.FocusEvent e) { |
| 256 | + isTextFieldFocused = false; |
| 257 | + } |
| 258 | + }); |
338 | 259 |
|
339 |
| - longitude.addFocusListener(new java.awt.event.FocusListener() { |
340 |
| - @Override |
341 |
| - public void focusGained(java.awt.event.FocusEvent e) { |
342 |
| - isTextFieldFocused = true; |
343 |
| - } |
| 260 | + longitude.addFocusListener(new java.awt.event.FocusListener() { |
| 261 | + @Override |
| 262 | + public void focusGained(java.awt.event.FocusEvent e) { |
| 263 | + isTextFieldFocused = true; |
| 264 | + } |
344 | 265 |
|
345 |
| - @Override |
346 |
| - public void focusLost(java.awt.event.FocusEvent e) { |
347 |
| - isTextFieldFocused = false; |
| 266 | + @Override |
| 267 | + public void focusLost(java.awt.event.FocusEvent e) { |
| 268 | + isTextFieldFocused = false; |
| 269 | + } |
| 270 | + }); |
348 | 271 | }
|
349 |
| - }); |
350 |
| - } |
351 |
| - }); |
| 272 | + } |
352 | 273 |
|
| 274 | + }); |
| 275 | + } |
353 | 276 | });
|
354 | 277 | }
|
355 | 278 |
|
|
0 commit comments