Skip to content

Commit fc3725f

Browse files
committed
Added Events
MarkerClickedEvent MapClickedEvent MapMovedEvent
1 parent 6cf7312 commit fc3725f

File tree

6 files changed

+162
-11
lines changed

6 files changed

+162
-11
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,15 @@ Both the LeafletMap component and the demo application are written in Kotlin.
5555

5656
#### Changelog
5757

58-
LeafletMap 1.1;
58+
LeafletMap 1.1.2:
59+
60+
* Added Events
61+
* MarkerClickEvent
62+
* MapClickedEvent
63+
* MapMovedEvent
64+
65+
66+
LeafletMap 1.1:
5967

6068
* Added support for CustomMarkers
6169
* Added removeMarker function

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<artifactId>leafletmap</artifactId>
99
<name>leafletmap</name>
1010
<packaging>jar</packaging>
11-
<version>1.1</version>
11+
<version>1.1.2</version>
1212

1313
<organization>
1414
<name>Saring</name>

src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package de.saring.leafletmap
22

3+
import de.saring.leafletmap.events.*
34
import javafx.scene.layout.StackPane
45
import javafx.scene.web.WebEngine
56
import javafx.scene.web.WebView
67

78
import java.net.URL
89
import javafx.concurrent.Worker
10+
import netscape.javascript.JSObject
911
import java.util.concurrent.CompletableFuture
1012

1113

@@ -24,6 +26,9 @@ class LeafletMapView : StackPane() {
2426
private val webEngine: WebEngine = webView.engine
2527

2628
private var varNameSuffix: Int = 1
29+
private val mapClickEvent = MapClickEventMaker()
30+
private val markerClickEvent = MarkerClickEventMaker()
31+
private val mapMoveEvent = MapMoveEventMaker()
2732

2833
/**
2934
* Creates the LeafletMapView component, it does not show any map yet.
@@ -220,24 +225,76 @@ class LeafletMapView : StackPane() {
220225
}
221226

222227
/**
223-
* Activates the on marker click alert
228+
* Sets an marker clickable
229+
*
230+
* @param markerName the name of the marker
231+
* @return is the marker clickable
232+
*/
233+
fun setMarkerClickable(markerName: String):Boolean{
234+
return if(markerClickEvent.isListenerSet()) {
235+
execScript("$markerName.on('click', function(e){ document.java.markerClick($markerName.options.title)})")
236+
true
237+
} else {
238+
false
239+
}
240+
}
241+
242+
/**
243+
* Sets the onMarkerClickListener
244+
*
245+
* @param listener the onMarerClickEventListener
246+
*/
247+
fun onMarkerClick(listener: MarkerClickEventListener){
248+
val win = execScript("document") as JSObject
249+
win.setMember("java", this)
250+
markerClickEvent.addListener(listener)
251+
}
252+
253+
/**
254+
* Handles the callback from the markerClickEvent
255+
*/
256+
fun markerClick(title: String){
257+
markerClickEvent.MarkerClickEvent(title)
258+
}
259+
260+
/**
261+
* Sets the onMapMoveListener
262+
*
263+
* @param listener the MapMoveEventListener
224264
*/
225-
fun addOnMarkerClick(markerName: String, icao: String){
226-
execScript("$markerName.on('click', function(e){ alert('plane,$icao')})")
265+
fun onMapMove(listener: MapMoveEventListener){
266+
val win = execScript("document") as JSObject
267+
win.setMember("java", this)
268+
execScript("myMap.on('moveend', function(e){ document.java.mapMove(myMap.getCenter().lat, myMap.getCenter().lng);});")
269+
mapMoveEvent.addListener(listener)
227270
}
228271

229272
/**
230-
* Activates the move alert
273+
* Handles the callback from the mapMoveEvent
274+
*/
275+
fun mapMove(lat: Double, lng: Double){
276+
val latlng = LatLong(lat, lng)
277+
mapMoveEvent.MapMoveEvent(latlng)
278+
}
279+
280+
/**
281+
* Sets the onMapClickListener
282+
*
283+
* @param listener the onMapClickEventListener
231284
*/
232-
fun initMapDrag(){
233-
execScript("myMap.on('moveend', function (e) { alert('move,' + myMap.getCenter()); });");
285+
fun onMapClick(listener: MapClickEventListener){
286+
val win = execScript("document") as JSObject
287+
win.setMember("java", this)
288+
execScript("myMap.on('click', function(e){ document.java.mapClick(e.latlng.lat, e.latlng.lng);});")
289+
mapClickEvent.addListener(listener)
234290
}
235291

236292
/**
237-
* Activates the click alert
293+
* Handles the callback from the mapClickEvent
238294
*/
239-
fun initMapClick(){
240-
execScript("myMap.on('click', function (e) { alert('click,' + e.latlng); });");
295+
fun mapClick(lat: Double, lng: Double){
296+
val latlng = LatLong(lat, lng)
297+
mapClickEvent.MapClickEvent(latlng)
241298
}
242299

243300
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package de.saring.leafletmap.events
2+
3+
import de.saring.leafletmap.LatLong
4+
import java.util.*
5+
6+
/**
7+
* Handles the MapClickEvent
8+
* @author Niklas Kellner
9+
*/
10+
interface MapClickEventListener {
11+
fun onMapClick(latLong: LatLong)
12+
}
13+
14+
internal class MapClickEventMaker {
15+
private val listeners = ArrayList<MapClickEventListener>()
16+
17+
fun addListener(toAdd: MapClickEventListener) {
18+
listeners.add(toAdd)
19+
}
20+
21+
fun MapClickEvent(latLong: LatLong) {
22+
// Notify everybody that may be interested.
23+
for (hl in listeners)
24+
hl.onMapClick(latLong)
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package de.saring.leafletmap.events
2+
3+
import de.saring.leafletmap.LatLong
4+
import java.util.*
5+
6+
/**
7+
* Handles the MapMoveEvent
8+
*
9+
* @author Niklas Kellner
10+
*/
11+
interface MapMoveEventListener {
12+
fun onMapMove(center: LatLong)
13+
}
14+
15+
internal class MapMoveEventMaker {
16+
private val listeners = ArrayList<MapMoveEventListener>()
17+
18+
fun addListener(toAdd: MapMoveEventListener) {
19+
listeners.add(toAdd)
20+
}
21+
22+
fun MapMoveEvent(latLong: LatLong) {
23+
// Notify everybody that may be interested.
24+
for (hl in listeners)
25+
hl.onMapMove(latLong)
26+
}
27+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package de.saring.leafletmap.events
2+
3+
import java.util.*
4+
5+
/**
6+
* Handles the MarkerClickEvent
7+
*
8+
* @author Niklas Kellner
9+
*/
10+
interface MarkerClickEventListener {
11+
fun onMarkerClick(title: String)
12+
}
13+
14+
internal class MarkerClickEventMaker {
15+
private val listeners = ArrayList<MarkerClickEventListener>()
16+
private var listenerSet = false
17+
18+
fun addListener(toAdd: MarkerClickEventListener) {
19+
listeners.add(toAdd)
20+
listenerSet = true
21+
}
22+
23+
fun MarkerClickEvent(title: String){
24+
// Notify everybody that may be interested.
25+
for (hl in listeners)
26+
hl.onMarkerClick(title)
27+
}
28+
29+
30+
fun isListenerSet(): Boolean{
31+
return listenerSet
32+
}
33+
}

0 commit comments

Comments
 (0)