Get the Lat/Long of a Mouse Click in a GMapsFX Map

A new API has been created for mouse events in the GMapsFX API which will begin to make it easier to obtain information about mouse events occurring within the Google Map without having to interact with the underlying Javascript API.

So now getting the Lat/Long of a mouse click is a relatively straightforward process.


GoogleMap map = googleMapView.createMap(mapOptions, false);
map.addMouseEventHandler(UIEventType.click, (GMapMouseEvent event) -> {
LatLong latLong = event.getLatLong();
System.out.println("Latitude: " + latLong.getLatitude());
System.out.println("Longitude: " + latLong.getLongitude());
});

view raw

LatLong.java

hosted with ❤ by GitHub

We tell the map we want to add a click UI event listener and pass in an event handler which handles a GMapMouseEvent event object.  From that object the latitude and longitude of the event can be determined.

Currently the Lat/Long are the only properties available on the GMapMouseEvent object, but additional properties will be added as demand warrants.

Below is a screenshot of one of the example applications included with the GMapsFX project that illustrates how to capture the lat/long of a mouse click.

 

latlongscreenshot.png

 

2 thoughts on “Get the Lat/Long of a Mouse Click in a GMapsFX Map

  1. Hello,
    I am unable to get this functionality working properly. I tried coping and pasting your code into mine and I get the following error:
    Multiple markers at this line
    – The method addMouseEventHandler(JavascriptObject, UIEventType,
    MouseEventHandler) in the type GoogleMap is not applicable for the arguments (UIEventType,
    (GMapMouseEvent event) -> {})
    – The target type of this expression must be a functional interface.

    I also copied and pasted that into your example code, and I get the same error message. I would really love to get this functionality working in my desktop application, I am sure I have everything set up correctly. Besides this method, everything else seems to work for me.

    *waiting eagerly on a response :)*

    • (continued)…
      I supposed I am thrown off by the JavaScript portion of the library.
      I am using your library along with the Google Maps Java Client API library, I am creating a desktop map application and I would like to create markers with this onMouseEventMethod.

      Should I also be using the Google Maps Javascript API, do I need to have my application set up so I have a server running?? If I am not interacting with a browser, why the JavaScript objects?

      Thank you

Leave a Reply to AetaCancel reply