Displaying coordinates
You can display the KATEC coordinates of the current location.
private fun showWGS84() {
activity?.apply {
mapView.getMapToCenter().let {pos ->
val point = KATECToWGS84(pos.x.roundToInt(),pos.y.roundToInt())
val alerts = AlertDialog.Builder(this)
.setTitle("현재 위치")
.setMessage("longitude: ${point.x}\nlatitude: ${point.y}")
alerts.setCancelable(true)
alerts.setNegativeButton("close")
{ dialog, _ ->
dialog.dismiss()
}
alerts.create().show()
}
}
}Adding a marker
You can configure a marker using the functions of the KNMapMarker (KO) class. You can change the marker image yourself, and if you do not set one, the marker is displayed with the default pin image.
private fun addMarker() {
KNMapMarker(mapView.coordinate).apply {
tag = basicLocationMarkerId
priority = basicLocationMarkerPriority
// setVisibleRange(.1f, 30f)
markerMap[tag] = this
mapView.addMarker(this)
}
}Removing markers
You can delete the markers you added using the [removeMarkersAll](/reference/android-ref-kotlin/class-KNMapView#:~:text=removeMarker-,removeMarkersAll() (KO),-getMarkers()%3A) function of the KNMapView (KO) class.
private fun removeMarker() {
mapView.removeMarkersAll()
}Moving a marker
You can move the position of a marker with an animation effect using the functions of the KNMapMarker (KO) class.
private fun moveCoordinateMarker() {
markerMap[basicLocationMarkerId]?.apply {
val x = rand.nextInt(-50,50)
val y = rand.nextInt(-50,50)
animate(FloatPoint(this.coordinate.x + x, this.coordinate.y + y), 1000L)
}
}Hiding and showing a marker
You can hide a marker or show it again using the functions of the KNMapMarker (KO) class.
private fun showOrHideMarker() {
markerMap[basicLocationMarkerId]?.apply {
this.isVisible = !this.isVisible
}
}Adding a custom marker
You can change the marker image yourself using the functions of the KNMapMarker (KO) class.
private fun addCustomMarker() {
if (markerMap.containsKey(customLocationMarkerId)) return
markerMap[customLocationMarkerId] = KNMapMarker(FloatPoint(mapView.coordinate.x /*+ rand.nextInt(-50, 50)*/, mapView.coordinate.y /*+ rand.nextInt(-50, 50)*/)).apply {
icon = goalImage
tag = customLocationMarkerId
pixelOffset = IntPoint(0, goalImage.height /2)
mapView.addMarker(this)
}
}Fitting the screen to markers
Fits the screen to the markers you have set.
The following is sample code that creates a fitInMarkers() function to fit the markers displayed on the map to the screen. It creates several markers around the current coordinates using random numbers, and then uses the KNMapCameraUpdate.fitTo() (KO) function to fit the initialized region on the screen with an animated camera.
private fun fitInMarkers() {
val pos = mapView.coordinate
val markerList = arrayListOf<KNMapMarker>().apply {
for (i in 0 until 10) {
val pos2 = FloatPoint(pos.x + rand.nextInt(-60, 60),
pos.y + rand.nextInt(-60, 60))
add(KNMapMarker(pos2))
}
mapView.addMarkers(this)
}
mapView.animateCamera(KNMapCameraUpdate.fitTo(KNMapCoordinateRegion.initWithMarkers(markerList)),
1000L, false)
}Moving the user location
Moves the current user location to an arbitrary place and displays it.
The following is sample code that creates a moveUserLocation() function to move the user location on the map. After making the user location visible, it calculates a new position using the map coordinates and random numbers, and then applies that position and angle to the map as an animation. Depending on the value of the withCamera variable, it performs either a camera animation or a position and angle animation.
private fun moveUserLocation() {
mapView.userLocation?.apply {
isVisible = true
val pos = FloatPoint(mapView.coordinate.x + rand.nextInt(-50, 50), mapView.coordinate.y + rand.nextInt(-50, 50))
val angles = (angle + rand.nextInt(0, 50)) % 360
if (withCamera) {
mapView.animateCamera(KNMapCameraUpdate.targetTo(pos).bearingTo(angles),500L, true)
} else {
animate(pos, angles)
}
withCamera = !withCamera
}
}Displaying a callout bubble on a marker
You can create a callout bubble to be displayed on a marker using the KNMapCalloutBubbleUpdate (KO) class. You can enter the title and subtitle of the callout bubble, and you can also set the font, font size, and font color.
fun markersWithBubbleAdd(mapView: KNBaseMapView, title: String, subString: String? = null): KNMapMarker {
clearAllObject(mapView)
val pos = mapView.coordinate// ?: FloatPoint(321397.72f, 532790.94f)
return KNMapMarker(FloatPoint(pos.x + rand.nextInt(-1000, 1000),
pos.y + rand.nextInt(-1000, 1000))).apply {
callOutBubbleUpdate = KNMapMarker.KNMapCalloutBubbleUpdate.Builder()
.setTitle(title)
.setSubTitle(subString)
.setTitleFontColor(Color.RED)
.setSubTitleFontColor(Color.MAGENTA)
.build()
isVisibleCalloutBubble = true
mapView.addMarker(this)
}
}