Skip to content

Commit

Permalink
Added Events
Browse files Browse the repository at this point in the history
MarkerClickedEvent
MapClickedEvent
MapMovedEvent
  • Loading branch information
N1k145 committed Dec 7, 2017
1 parent 6cf7312 commit fc3725f
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 11 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<artifactId>leafletmap</artifactId>
<name>leafletmap</name>
<packaging>jar</packaging>
<version>1.1</version>
<version>1.1.2</version>

<organization>
<name>Saring</name>
Expand Down
75 changes: 66 additions & 9 deletions src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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.
Expand Down Expand Up @@ -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)
}

/**
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/de/saring/leafletmap/events/MapClickEvent.kt
Original file line number Diff line number Diff line change
@@ -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<MapClickEventListener>()

fun addListener(toAdd: MapClickEventListener) {
listeners.add(toAdd)
}

fun MapClickEvent(latLong: LatLong) {
// Notify everybody that may be interested.
for (hl in listeners)
hl.onMapClick(latLong)
}
}
27 changes: 27 additions & 0 deletions src/main/kotlin/de/saring/leafletmap/events/MapMoveEvent.kt
Original file line number Diff line number Diff line change
@@ -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<MapMoveEventListener>()

fun addListener(toAdd: MapMoveEventListener) {
listeners.add(toAdd)
}

fun MapMoveEvent(latLong: LatLong) {
// Notify everybody that may be interested.
for (hl in listeners)
hl.onMapMove(latLong)
}
}
33 changes: 33 additions & 0 deletions src/main/kotlin/de/saring/leafletmap/events/MarkerClickEvent.kt
Original file line number Diff line number Diff line change
@@ -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<MarkerClickEventListener>()
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
}
}

0 comments on commit fc3725f

Please sign in to comment.