Thursday, January 31, 2013

JavaFX and Google Maps

There are already some examples on the Internet which demonstrates the use of a Google Maps view within a JavaFX application. They demonstrate how to control a WebView backedup google maps javascript.
One thing was always missing and that was the way back. For example how to read the current Lat Lon view position of the Google Maps view.

After some experiments I came to a simple timer based solution. The idea is that a timer function regularly polls the javascript document and parses the result.

First we take a look at the javascript code;



When the updateJavaFX() function is called the document variables (currentCenter for the position) are filled.

In the JavaFX code the following part (this part is called within an AnimationTimer) is used to retrieve the current lat lon position of the map.


Basically the trick is call the javascript document.updateJavaJX() function and then to read eg. the currentCenter javascript variable. I use the toString() function to convert it to a string and then parse it. The classcastexception is present when a different kind of map type is used eg. openstreetmap.

This all could result in the following application;










7 comments:

Eddie said...

Hey man, thank you very much for your post. It helped me a lot. I am really a noob when it comes to JavaScript and JavaFX. I had everything working with GMaps but I could not retrieve the information from within the JavaScript code. Thanks to you I can go to sleep! Many thanks

Eddie said...
This comment has been removed by a blog administrator.
Eddie said...

Hey man, thank you very much for your post. It helped me a lot. I am really a noob when it comes to JavaScript and JavaFX. I had everything working with GMaps but I could not retrieve the information from within the JavaScript code. Thanks to you I can go to sleep! Many thanks

Anonymous said...

why you don't use the animation timer built in method in javaFX

ahmed sebey said...

please can you tell me what is the type of mCenterlat,mCenterlng and mZoom

ahmed sebey said...

can you tell me what is the type of mZoom ,mCenterlat and mCenterlng

Waverick said...

They are all DoubleProperty