Skip to content

Commit

Permalink
Merge pull request #4 from N1k145/MarkerWraper
Browse files Browse the repository at this point in the history
Added Marker wraper
  • Loading branch information
N1k145 authored Dec 13, 2017
2 parents 1465672 + b844ca5 commit a7019ec
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 86 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
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.2</version>
<version>1.1.3</version>

<organization>
<name>Saring</name>
Expand Down
88 changes: 10 additions & 78 deletions src/main/kotlin/de/saring/leafletmap/LeafletMapView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()});")
}

/**
Expand All @@ -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
*
Expand Down Expand Up @@ -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++}"
}
93 changes: 93 additions & 0 deletions src/main/kotlin/de/saring/leafletmap/Marker.kt
Original file line number Diff line number Diff line change
@@ -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
}
15 changes: 8 additions & 7 deletions src/test/kotlin/de/saring/leafletmapdemo/Controller.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Controller {

private val positionTooltip = Tooltip()

private var positionMarkerName: String? = null
private var positionMarker: Marker? = null

@FXML
fun initialize() {
Expand Down Expand Up @@ -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)

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

0 comments on commit a7019ec

Please sign in to comment.