diff --git a/README.md b/README.md index 017765d..2e3c96b 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,10 @@ Both the LeafletMap component and the demo application are written in Kotlin. #### Changelog +LeafletMap 1.1.3: + +* Added Wraper Class for Marker + LeafletMap 1.1.2: * Added Events diff --git a/pom.xml b/pom.xml index 58080e0..d169810 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ leafletmap leafletmap jar - 1.1.2 + 1.1.3 Saring diff --git a/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt b/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt index c05b9f4..6ff2741 100644 --- a/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt +++ b/src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt @@ -137,56 +137,21 @@ class LeafletMapView : StackPane() { execScript("myMap.setZoom([$zoomLevel]);") /** - * Sets a marker at the specified geographical position. + * Adds a Marker Object to a map * - * @param position marker position - * @param title marker title shown in tooltip (pass empty string when tooltip not needed) - * @param marker marker color - * @param zIndexOffset zIndexOffset (higher number means on top) - * @return variable name of the created marker + * @param marker the Marker Object */ - fun addMarker(position: LatLong, title: String, marker: ColorMarker, zIndexOffset: Int): String { - val varName = "marker${varNameSuffix++}" - - execScript("var $varName = L.marker([${position.latitude}, ${position.longitude}], " - + "{title: '$title', icon: ${marker.iconName}, zIndexOffset: $zIndexOffset}).addTo(myMap);") - return varName; - } - - /** - * Sets a custom marker at the specified geographical position. - * - * @param position marker position - * @param title marker title shown in tooltip (pass empty string when tooltip not needed) - * @param marker customMarkerDesign - * @param zIndexOffset zIndexOffset (higher number means on top) - * @return variable name of the created marker - */ - fun addMarker(position: LatLong, title: String, marker: String, zIndexOffset: Int): String { - val varName = "marker${varNameSuffix++}" - - execScript("var $varName = L.marker([${position.latitude}, ${position.longitude}], " - + "{title: '$title', icon: $marker, zIndexOffset: $zIndexOffset}).addTo(myMap);") - return varName; - } - - /** - * Moves the existing marker specified by the variable name to the new geographical position. - * - * @param markerName variable name of the marker - * @param position new marker position - */ - fun moveMarker(markerName: String, position: LatLong) { - execScript("$markerName.setLatLng([${position.latitude}, ${position.longitude}]);") + fun addMarker(marker: Marker){ + marker.addToMap(getNextMarkerName(), this) } /** * Removes an existing marker from the map * - * @param markerName variable name of the marker + * @param marker the Marker object */ - fun removeMarker(markerName: String) { - execScript("myMap.removeLayer($markerName);") + fun removeMarker(marker: Marker) { + execScript("myMap.removeLayer(${marker.getName()});") } /** @@ -204,41 +169,6 @@ class LeafletMapView : StackPane() { return markerName } - /** - * Changes the icon of the marker - * - * @param markerName the name of the marker - * @param newIcon the name of the new icon - */ - fun changeIconOfMarker(markerName: String, newIcon: String){ - execScript("$markerName.setIcon($newIcon);") - } - - /** - * Changes the icon of the marker - * - * @param markerName the name of the marker - * @param newMarker the new ColorMarker - */ - fun changeIconOfMarker(markerName: String, newMarker: ColorMarker){ - execScript("$markerName.setIcon(${newMarker.iconName});") - } - - /** - * 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 * @@ -317,5 +247,7 @@ class LeafletMapView : StackPane() { |myMap.fitBounds(polyline.getBounds());""".trimMargin()) } - private fun execScript(script: String) = webEngine.executeScript(script) + internal fun execScript(script: String) = webEngine.executeScript(script) + + private fun getNextMarkerName() : String = "marker${varNameSuffix++}" } diff --git a/src/main/kotlin/de/saring/leafletmap/Marker.kt b/src/main/kotlin/de/saring/leafletmap/Marker.kt new file mode 100644 index 0000000..0433cee --- /dev/null +++ b/src/main/kotlin/de/saring/leafletmap/Marker.kt @@ -0,0 +1,93 @@ +package de.saring.leafletmap + +/** + * Creates a marker at the specified geographical position. + * + * @author Niklas Kellner + * + * @param position marker position + * @param title marker title shown in tooltip (pass empty string when tooltip not needed) + * @param zIndexOffset zIndexOffset (higher number means on top) + * + */ +class Marker private constructor(private var position: LatLong, private var title: String, private var zIndexOffset: Int) { + private var marker = ColorMarker.RED_MARKER.iconName + private lateinit var map : LeafletMapView + + private var name = "" + + /** + * Creates a marker at the specified geographical position. + * + * @param position marker position + * @param title marker title shown in tooltip (pass empty string when tooltip not needed) + * @param marker marker color + * @param zIndexOffset zIndexOffset (higher number means on top) + * @return variable name of the created marker + */ + constructor(position: LatLong, title: String, marker: ColorMarker, zIndexOffset: Int) : this(position, title, zIndexOffset) { + this.marker = marker.iconName + } + + /** + * Creates a marker at the specified geographical position. + * + * @param position marker position + * @param title marker title shown in tooltip (pass empty string when tooltip not needed) + * @param marker marker color + * @param zIndexOffset zIndexOffset (higher number means on top) + * @return variable name of the created marker + */ + constructor(position: LatLong, title: String, marker: String, zIndexOffset: Int) : this(position, title, zIndexOffset) { + this.marker = marker + } + + /** + * Adds the marker to a map, gets called from the mapAddMarker + * + * @param nextMarkerName the variable name of the marker + * @param map the LeafetMapView + */ + internal fun addToMap(nextMarkerName: String, map: LeafletMapView) { + this.name = nextMarkerName + this.map = map + map.execScript("var $name = L.marker([${position.latitude}, ${position.longitude}], " + + "{title: '$title', icon: ${marker}, zIndexOffset: $zIndexOffset}).addTo(myMap);") + } + + /** + * Changes the icon of the marker + * + * @param newIcon the name of the new icon + */ + fun changeIcon(newIcon: String) { + map.execScript("$name.setIcon($newIcon);") + } + + /** + * Changes the icon of the marker + * + * @param newIcon the new ColorMarker + */ + fun changeIcon(newIcon: ColorMarker) { + map.execScript("$name.setIcon(${newIcon.iconName});") + } + + /** + * Moves the existing marker specified by the variable name to the new geographical position. + * + * @param position new marker position + */ + fun move(position: LatLong) { + map.execScript("$name.setLatLng([${position.latitude}, ${position.longitude}]);") + } + + /** + * Sets the marker clickable + */ + fun setClickable() { + map.execScript("$name.on('click', function(e){ document.java.markerClick($name.options.title)})") + } + + internal fun getName(): String = this.name +} \ No newline at end of file diff --git a/src/test/kotlin/de/saring/leafletmapdemo/Controller.kt b/src/test/kotlin/de/saring/leafletmapdemo/Controller.kt index 284971f..f883cd4 100644 --- a/src/test/kotlin/de/saring/leafletmapdemo/Controller.kt +++ b/src/test/kotlin/de/saring/leafletmapdemo/Controller.kt @@ -22,7 +22,7 @@ class Controller { private val positionTooltip = Tooltip() - private var positionMarkerName: String? = null + private var positionMarker: Marker? = null @FXML fun initialize() { @@ -54,11 +54,11 @@ class Controller { // display lap markers first, start and end needs to be displayed on top for (i in 0 until track.lapsPositions.size) { - mapView.addMarker(track.lapsPositions[i], "Lap ${i + 1}", ColorMarker.GREY_MARKER, 0) + mapView.addMarker(Marker(track.lapsPositions[i], "Lap ${i + 1}", ColorMarker.GREY_MARKER, 0)) } - mapView.addMarker(track.positions.first(), "Start", ColorMarker.GREEN_MARKER, 1000) - mapView.addMarker(track.positions.last(), "End", ColorMarker.RED_MARKER, 2000) + mapView.addMarker(Marker(track.positions.first(), "Start", ColorMarker.GREEN_MARKER, 1000)) + mapView.addMarker(Marker(track.positions.last(), "End", ColorMarker.RED_MARKER, 2000)) mapView.addTrack(track.positions) @@ -75,10 +75,11 @@ class Controller { val positionIndex = slPosition.value.toInt() val position = track.positions[positionIndex] - if (positionMarkerName == null) { - positionMarkerName = mapView.addMarker(position, "", ColorMarker.BLUE_MARKER, 0) + if (positionMarker == null) { + positionMarker = Marker(position, "", ColorMarker.BLUE_MARKER, 0) + mapView.addMarker(positionMarker!!) } else { - mapView.moveMarker(positionMarkerName!!, position) + positionMarker!!.move(position) } displayPositionTooltip(positionIndex)