๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | ๋„ค์ด๋ฒ„ ์ง€๋„์œ„์— ๋งˆ์ปค ๋„์šฐ๊ธฐ

hyihyiยท2024๋…„ 3์›” 5์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
58/70
post-thumbnail

๐Ÿ“– ๋„ค์ด๋ฒ„ ์ง€๋„ ๋งˆ์ปค

AndroidManifest.xml

<meta-data
            android:name="com.naver.maps.map.CLIENT_ID"
            android:value="ootzd3244r" />

์ข…์†์„ฑ ์ถ”๊ฐ€

//Naver Map
implementation("com.naver.maps:map-sdk:3.17.0")
implementation("com.google.android.gms:play-services-location:17.0.0")

์ฝ”๋“œ

class RecordDetailMapFragment : Fragment(), OnMapReadyCallback {
    private lateinit var naverMap: NaverMap
    private lateinit var mapView: MapView
...
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    // mapView ๋ณ€์ˆ˜ ์ดˆ๊ธฐํ™”
    mapView = binding.mvRecordDetailFullMap
    mapView.onCreate(savedInstanceState)
    mapView.getMapAsync(this)
}
override fun onMapReady(naverMap: NaverMap) {
    // ๋งˆ์ปค ๊ฐ์ฒด ์ƒ์„ฑ
    val marker = Marker()

    // ๋งˆ์ปค ์œ„์น˜ ์„ค์ • (์˜ˆ: ์„œ์šธ ์‹œ์ฒญ)
    marker.position = LatLng(37.5663, 126.9779)

    // ๋งˆ์ปค ํƒ€์ดํ‹€ ์„ค์ • (์„ ํƒ ์‚ฌํ•ญ)
    marker.captionText = "์„œ์šธ ์‹œ์ฒญ"

    // ๋งˆ์ปค ์•„์ด์ฝ˜ ์„ค์ • (์„ ํƒ ์‚ฌํ•ญ)
    marker.icon = OverlayImage.fromResource(R.drawable.marker_icon)

    // ๋งˆ์ปค ์ง€๋„์— ์ถ”๊ฐ€
    marker.map = naverMap

    // ์นด๋ฉ”๋ผ ์œ„์น˜์™€ ์คŒ ๋ ˆ๋ฒจ ์กฐ์ • (์„ ํƒ ์‚ฌํ•ญ)
    val cameraUpdate = CameraUpdate.scrollTo(marker.position).animate(CameraAnimation.Easing)
    naverMap.moveCamera(cameraUpdate)
    naverMap.moveCamera(CameraUpdate.zoomTo(10.0))
}

๋งต๋ทฐ๊ฐ€ ์•ˆ๋“œ๋กœ์ด๋“œ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ์— ๋งž์ถฐ ์ ์ ˆํ•˜๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ธฐ ์œ„ํ•ด ๋งต๋ทฐ(MapView)์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ๋ฅผ ํ”„๋ž˜๊ทธ๋จผํŠธ๋‚˜ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.
๋”ฐ๋ผ์„œ ๋งต๋ทฐ์˜ onCreate, onResume, onPause, onDestroy, onLowMemory, onStart, onStop ๋“ฑ์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ๊ฐ ํ”„๋ž˜๊ทธ๋จผํŠธ๋‚˜ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ๋™์ผํ•œ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ํ˜ธ์ถœํ•˜์—ฌ, ๋งต๋ทฐ๊ฐ€ ์•ˆ๋“œ๋กœ์ด๋“œ ์ƒ๋ช…์ฃผ๊ธฐ ์ด๋ฒคํŠธ์— ๋งž์ถฐ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋™์ž‘ํ•˜๊ณ  ์ž์›์„ ํšจ์œจ์ ์œผ๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์•ผ ํ•œ๋‹ค.

override fun onStart() {
    super.onStart()
    mapView.onStart()
}
override fun onResume() {
    super.onResume()
    mapView.onResume()
}
override fun onPause() {
    mapView.onPause()
    super.onPause()
}
override fun onStop() {
    mapView.onStop()
    super.onStop()
}
override fun onLowMemory() {
    super.onLowMemory()
    mapView.onLowMemory()
}
override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    mapView.onSaveInstanceState(outState)
}
override fun onDestroyView() {
    super.onDestroyView()
    mapView.onDestroy()
    _binding = null
}

๐Ÿ“– ์‘์šฉ

๐Ÿ“ ๋งˆ์ปค๋ฐ‘์— ์บก์…˜ ๋„์šฐ๊ธฐ

markers.forEach { marker ->
    marker.captionText = "์žฅ์†Œ ์ด๋ฆ„" // ์‹ค์ œ ์žฅ์†Œ ์ด๋ฆ„์œผ๋กœ ๋Œ€์ฒดํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    marker.setCaptionAligns(Align.Bottom)
    marker.captionColor = Color.BLACK
    marker.captionHaloColor = Color.WHITE
    marker.captionTextSize = 16f
}

๐Ÿ“ ํ™•๋Œ€ ๋น„์œจ์— ๋”ฐ๋ฅธ ์บก์…˜ ๊ฐ€์‹œ์„ฑ ์กฐ์ ˆ

if (zoom > 14.0) {
    // ํ™•๋Œ€ ๋ ˆ๋ฒจ์ด ์ž„๊ณ„๊ฐ’ ์ด์ƒ์ผ ๋•Œ ๋งˆ์ปค ์บก์…˜ ๋ณด์ด๊ธฐ
    markers.forEach { marker ->
        marker.captionText = "์žฅ์†Œ ์ด๋ฆ„"
        marker.setCaptionAligns(Align.Bottom)
        marker.captionColor = Color.BLACK
        marker.captionHaloColor = Color.WHITE
        marker.captionTextSize = 16f
    }
} else {
    // ํ™•๋Œ€ ๋ ˆ๋ฒจ์ด ์ž„๊ณ„๊ฐ’ ๋ฏธ๋งŒ์ผ ๋•Œ ๋งˆ์ปค ์บก์…˜ ์ˆจ๊ธฐ๊ธฐ
    markers.forEach { marker ->
        marker.captionText = ""
    }
}

๐Ÿ“ ๊ธฐ๋ณธ์ ์œผ๋กœ ๋‚˜์˜ค๋Š” ํ™•๋Œ€/์ถ•์†Œ ๋ฒ„ํŠผ ์ œ๊ฑฐ

์˜ค๋ฅธ์ชฝ์— ๋ณด์ด๋Š” -/+๋ฒ„ํŠผ์„ ์•„๋ž˜์˜ ์ฝ”๋“œ ํ•œ ์ค„์„ onMapReady ํ•จ์ˆ˜ ์•ˆ์— ์ ์–ด์คŒ์œผ๋กœ์จ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.
naverMap.uiSettings.isZoomControlEnabled = false
profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€