From fc3725f3573e12be26980f13afe917f2010504d6 Mon Sep 17 00:00:00 2001 From: N1k145 Date: Thu, 7 Dec 2017 21:37:27 +0100 Subject: [PATCH] Added Events MarkerClickedEvent MapClickedEvent MapMovedEvent --- README.md | 10 ++- pom.xml | 2 +- .../de/saring/leafletmap/LeafletMapView.kt | 75 ++++++++++++++++--- .../saring/leafletmap/events/MapClickEvent.kt | 26 +++++++ .../saring/leafletmap/events/MapMoveEvent.kt | 27 +++++++ .../leafletmap/events/MarkerClickEvent.kt | 33 ++++++++ 6 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/de/saring/leafletmap/events/MapClickEvent.kt create mode 100644 src/main/kotlin/de/saring/leafletmap/events/MapMoveEvent.kt create mode 100644 src/main/kotlin/de/saring/leafletmap/events/MarkerClickEvent.kt diff --git a/README.md b/README.md index 655c339..d2e5cbe 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,15 @@ Both the LeafletMap component and the demo application are written in Kotlin. #### Changelog -LeafletMap 1.1; +LeafletMap 1.1.2: + +* Added Events + * MarkerClickEvent + * MapClickedEvent + * MapMovedEvent + + +LeafletMap 1.1: * Added support for CustomMarkers * Added removeMarker function diff --git a/pom.xml b/pom.xml index a212702..58080e0 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ leafletmap leafletmap jar - 1.1 + 1.1.2 Saring diff --git a/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt b/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt index 35dabdf..fe423f2 100644 --- a/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt +++ b/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt @@ -1,11 +1,13 @@ package de.saring.leafletmap +import de.saring.leafletmap.events.* import javafx.scene.layout.StackPane import javafx.scene.web.WebEngine import javafx.scene.web.WebView import java.net.URL import javafx.concurrent.Worker +import netscape.javascript.JSObject import java.util.concurrent.CompletableFuture @@ -24,6 +26,9 @@ class LeafletMapView : StackPane() { private val webEngine: WebEngine = webView.engine private var varNameSuffix: Int = 1 + private val mapClickEvent = MapClickEventMaker() + private val markerClickEvent = MarkerClickEventMaker() + private val mapMoveEvent = MapMoveEventMaker() /** * Creates the LeafletMapView component, it does not show any map yet. @@ -220,24 +225,76 @@ class LeafletMapView : StackPane() { } /** - * Activates the on marker click alert + * Sets an marker clickable + * + * @param markerName the name of the marker + * @return is the marker clickable + */ + fun setMarkerClickable(markerName: String):Boolean{ + return if(markerClickEvent.isListenerSet()) { + execScript("$markerName.on('click', function(e){ document.java.markerClick($markerName.options.title)})") + true + } else { + false + } + } + + /** + * Sets the onMarkerClickListener + * + * @param listener the onMarerClickEventListener + */ + fun onMarkerClick(listener: MarkerClickEventListener){ + val win = execScript("document") as JSObject + win.setMember("java", this) + markerClickEvent.addListener(listener) + } + + /** + * Handles the callback from the markerClickEvent + */ + fun markerClick(title: String){ + markerClickEvent.MarkerClickEvent(title) + } + + /** + * Sets the onMapMoveListener + * + * @param listener the MapMoveEventListener */ - fun addOnMarkerClick(markerName: String, icao: String){ - execScript("$markerName.on('click', function(e){ alert('plane,$icao')})") + fun onMapMove(listener: MapMoveEventListener){ + val win = execScript("document") as JSObject + win.setMember("java", this) + execScript("myMap.on('moveend', function(e){ document.java.mapMove(myMap.getCenter().lat, myMap.getCenter().lng);});") + mapMoveEvent.addListener(listener) } /** - * Activates the move alert + * Handles the callback from the mapMoveEvent + */ + fun mapMove(lat: Double, lng: Double){ + val latlng = LatLong(lat, lng) + mapMoveEvent.MapMoveEvent(latlng) + } + + /** + * Sets the onMapClickListener + * + * @param listener the onMapClickEventListener */ - fun initMapDrag(){ - execScript("myMap.on('moveend', function (e) { alert('move,' + myMap.getCenter()); });"); + fun onMapClick(listener: MapClickEventListener){ + val win = execScript("document") as JSObject + win.setMember("java", this) + execScript("myMap.on('click', function(e){ document.java.mapClick(e.latlng.lat, e.latlng.lng);});") + mapClickEvent.addListener(listener) } /** - * Activates the click alert + * Handles the callback from the mapClickEvent */ - fun initMapClick(){ - execScript("myMap.on('click', function (e) { alert('click,' + e.latlng); });"); + fun mapClick(lat: Double, lng: Double){ + val latlng = LatLong(lat, lng) + mapClickEvent.MapClickEvent(latlng) } /** diff --git a/src/main/kotlin/de/saring/leafletmap/events/MapClickEvent.kt b/src/main/kotlin/de/saring/leafletmap/events/MapClickEvent.kt new file mode 100644 index 0000000..2b67df2 --- /dev/null +++ b/src/main/kotlin/de/saring/leafletmap/events/MapClickEvent.kt @@ -0,0 +1,26 @@ +package de.saring.leafletmap.events + +import de.saring.leafletmap.LatLong +import java.util.* + +/** + * Handles the MapClickEvent + * @author Niklas Kellner + */ +interface MapClickEventListener { + fun onMapClick(latLong: LatLong) +} + +internal class MapClickEventMaker { + private val listeners = ArrayList() + + fun addListener(toAdd: MapClickEventListener) { + listeners.add(toAdd) + } + + fun MapClickEvent(latLong: LatLong) { + // Notify everybody that may be interested. + for (hl in listeners) + hl.onMapClick(latLong) + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/saring/leafletmap/events/MapMoveEvent.kt b/src/main/kotlin/de/saring/leafletmap/events/MapMoveEvent.kt new file mode 100644 index 0000000..0735349 --- /dev/null +++ b/src/main/kotlin/de/saring/leafletmap/events/MapMoveEvent.kt @@ -0,0 +1,27 @@ +package de.saring.leafletmap.events + +import de.saring.leafletmap.LatLong +import java.util.* + +/** + * Handles the MapMoveEvent + * + * @author Niklas Kellner + */ +interface MapMoveEventListener { + fun onMapMove(center: LatLong) +} + +internal class MapMoveEventMaker { + private val listeners = ArrayList() + + fun addListener(toAdd: MapMoveEventListener) { + listeners.add(toAdd) + } + + fun MapMoveEvent(latLong: LatLong) { + // Notify everybody that may be interested. + for (hl in listeners) + hl.onMapMove(latLong) + } +} \ No newline at end of file diff --git a/src/main/kotlin/de/saring/leafletmap/events/MarkerClickEvent.kt b/src/main/kotlin/de/saring/leafletmap/events/MarkerClickEvent.kt new file mode 100644 index 0000000..83f1ff8 --- /dev/null +++ b/src/main/kotlin/de/saring/leafletmap/events/MarkerClickEvent.kt @@ -0,0 +1,33 @@ +package de.saring.leafletmap.events + +import java.util.* + +/** + * Handles the MarkerClickEvent + * + * @author Niklas Kellner + */ +interface MarkerClickEventListener { + fun onMarkerClick(title: String) +} + +internal class MarkerClickEventMaker { + private val listeners = ArrayList() + private var listenerSet = false + + fun addListener(toAdd: MarkerClickEventListener) { + listeners.add(toAdd) + listenerSet = true + } + + fun MarkerClickEvent(title: String){ + // Notify everybody that may be interested. + for (hl in listeners) + hl.onMarkerClick(title) + } + + + fun isListenerSet(): Boolean{ + return listenerSet + } +} \ No newline at end of file