Skip to content

Commit b46cdc5

Browse files
authored
Merge pull request #5 from N1k145/IconFactory
Added Support for external icons
2 parents a7019ec + c8c23af commit b46cdc5

31 files changed

+43
-15
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,13 @@ Both the LeafletMap component and the demo application are written in Kotlin.
5555

5656
#### Changelog
5757

58+
LeafletMap 1.1.4:
59+
60+
* Improved support for customMarkers
61+
5862
LeafletMap 1.1.3:
5963

60-
* Added Wraper Class for Marker
64+
* Added Wrapper Class for Marker
6165

6266
LeafletMap 1.1.2:
6367

@@ -69,7 +73,7 @@ LeafletMap 1.1.2:
6973

7074
LeafletMap 1.1:
7175

72-
* Added support for CustomMarkers
76+
* Added support for customMarkers
7377
* Added removeMarker function
7478
* Added changeIconOfMarker function
7579

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.3</version>
11+
<version>1.1.4</version>
1212

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

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

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import javafx.scene.web.WebView
88
import java.net.URL
99
import javafx.concurrent.Worker
1010
import netscape.javascript.JSObject
11+
import java.io.ByteArrayOutputStream
12+
import java.io.File
13+
import java.io.IOException
14+
import java.util.*
1115
import java.util.concurrent.CompletableFuture
16+
import javax.imageio.ImageIO
1217

1318

1419
/**
@@ -134,14 +139,14 @@ class LeafletMapView : StackPane() {
134139
* @param zoomLevel zoom level (0 - 19 for OpenStreetMap)
135140
*/
136141
fun setZoom(zoomLevel: Int) =
137-
execScript("myMap.setZoom([$zoomLevel]);")
142+
execScript("myMap.setZoom([$zoomLevel]);")
138143

139144
/**
140145
* Adds a Marker Object to a map
141146
*
142147
* @param marker the Marker Object
143148
*/
144-
fun addMarker(marker: Marker){
149+
fun addMarker(marker: Marker) {
145150
marker.addToMap(getNextMarkerName(), this)
146151
}
147152

@@ -160,21 +165,40 @@ class LeafletMapView : StackPane() {
160165
* @param markerName the name of the marker type
161166
* @param iconUrl the url if the marker icon
162167
*/
163-
fun addCustomMarker(markerName: String, iconUrl: String):String{
168+
fun addCustomMarker(markerName: String, iconUrl: String): String {
164169
execScript("var $markerName = L.icon({\n" +
165-
"iconUrl: '$iconUrl',\n" +
170+
"iconUrl: '${createImage(iconUrl, "png")}',\n" +
166171
"iconSize: [24, 24],\n" +
167172
"iconAnchor: [12, 12],\n" +
168-
"});");
173+
"});")
169174
return markerName
170175
}
171176

177+
private fun createImage(path: String, type: String): String {
178+
val image = ImageIO.read(File(path))
179+
var imageString: String? = null
180+
val bos = ByteArrayOutputStream()
181+
182+
try {
183+
ImageIO.write(image, type, bos)
184+
val imageBytes = bos.toByteArray()
185+
186+
val encoder = Base64.getEncoder()
187+
imageString = encoder.encodeToString(imageBytes)
188+
189+
bos.close()
190+
} catch (e: IOException) {
191+
e.printStackTrace()
192+
}
193+
return "data:image/$type;base64,$imageString"
194+
}
195+
172196
/**
173197
* Sets the onMarkerClickListener
174198
*
175199
* @param listener the onMarerClickEventListener
176200
*/
177-
fun onMarkerClick(listener: MarkerClickEventListener){
201+
fun onMarkerClick(listener: MarkerClickEventListener) {
178202
val win = execScript("document") as JSObject
179203
win.setMember("java", this)
180204
markerClickEvent.addListener(listener)
@@ -183,7 +207,7 @@ class LeafletMapView : StackPane() {
183207
/**
184208
* Handles the callback from the markerClickEvent
185209
*/
186-
fun markerClick(title: String){
210+
fun markerClick(title: String) {
187211
markerClickEvent.MarkerClickEvent(title)
188212
}
189213

@@ -192,7 +216,7 @@ class LeafletMapView : StackPane() {
192216
*
193217
* @param listener the MapMoveEventListener
194218
*/
195-
fun onMapMove(listener: MapMoveEventListener){
219+
fun onMapMove(listener: MapMoveEventListener) {
196220
val win = execScript("document") as JSObject
197221
win.setMember("java", this)
198222
execScript("myMap.on('moveend', function(e){ document.java.mapMove(myMap.getCenter().lat, myMap.getCenter().lng);});")
@@ -202,7 +226,7 @@ class LeafletMapView : StackPane() {
202226
/**
203227
* Handles the callback from the mapMoveEvent
204228
*/
205-
fun mapMove(lat: Double, lng: Double){
229+
fun mapMove(lat: Double, lng: Double) {
206230
val latlng = LatLong(lat, lng)
207231
mapMoveEvent.MapMoveEvent(latlng)
208232
}
@@ -212,7 +236,7 @@ class LeafletMapView : StackPane() {
212236
*
213237
* @param listener the onMapClickEventListener
214238
*/
215-
fun onMapClick(listener: MapClickEventListener){
239+
fun onMapClick(listener: MapClickEventListener) {
216240
val win = execScript("document") as JSObject
217241
win.setMember("java", this)
218242
execScript("myMap.on('click', function(e){ document.java.mapClick(e.latlng.lat, e.latlng.lng);});")
@@ -222,7 +246,7 @@ class LeafletMapView : StackPane() {
222246
/**
223247
* Handles the callback from the mapClickEvent
224248
*/
225-
fun mapClick(lat: Double, lng: Double){
249+
fun mapClick(lat: Double, lng: Double) {
226250
val latlng = LatLong(lat, lng)
227251
mapClickEvent.MapClickEvent(latlng)
228252
}
@@ -249,5 +273,5 @@ class LeafletMapView : StackPane() {
249273

250274
internal fun execScript(script: String) = webEngine.executeScript(script)
251275

252-
private fun getNextMarkerName() : String = "marker${varNameSuffix++}"
276+
private fun getNextMarkerName(): String = "marker${varNameSuffix++}"
253277
}
-1.04 KB
Binary file not shown.
-632 Bytes
Binary file not shown.
-4.42 KB
Binary file not shown.
-632 Bytes
Binary file not shown.
-741 Bytes
Binary file not shown.
-745 Bytes
Binary file not shown.
-998 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)