Google 지도 멀티 레이어 유틸리티
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
- 소개
- 여러 클러스터, KML, GeoJSON 레이어 추가
- 나만의 지형지물 추가
- 클릭 이벤트 처리
- 데모 앱 보기
소개
이전 튜토리얼에서는
KML 및
GeoJSON의 지형지물뿐 아니라
마커의 클러스터를 지도에 추가하는 방법을 알아보았습니다.
하지만 동일한 지도에 이러한 레이어를 여러 개 추가하고 레이어마다 독립적인
클릭 이벤트를 가져오려면 어떻게 해야 할까요?
여러 클러스터, KML, GeoJSON 레이어 추가
라이브러리에는 여러 유형의 레이어에 대한 클릭 이벤트를 관리하는 데
도움이 되는 Manager
객체가 포함됩니다. 따라서 레이어를 설정하기 전에 먼저 이를 인스턴스화하고
GoogleMap
에 전달해야 합니다.
Kotlin
val markerManager = MarkerManager(map)
val groundOverlayManager = GroundOverlayManager(map!!)
val polygonManager = PolygonManager(map)
val polylineManager = PolylineManager(map)
Java
MarkerManager markerManager = new MarkerManager(map);
GroundOverlayManager groundOverlayManager = new GroundOverlayManager(map);
PolygonManager polygonManager = new PolygonManager(map);
PolylineManager polylineManager = new PolylineManager(map);
그런 다음, 이러한 관리자 클래스를 설정 시 다른 레이어의 생성자로 다음과
같이 전달할 수 있습니다.
Kotlin
val clusterManager =
ClusterManager<MyItem>(context, map, markerManager)
val geoJsonLineLayer = GeoJsonLayer(
map,
R.raw.geojson_file,
context,
markerManager,
polygonManager,
polylineManager,
groundOverlayManager
)
val kmlPolylineLayer = KmlLayer(
map,
R.raw.kml_file,
context,
markerManager,
polygonManager,
polylineManager,
groundOverlayManager,
null
)
Java
ClusterManager<MyItem> clusterManager = new ClusterManager<>(context, map, markerManager);
GeoJsonLayer geoJsonLineLayer = new GeoJsonLayer(map, R.raw.geojson_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager);
KmlLayer kmlPolylineLayer = new KmlLayer(map, R.raw.kml_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager, null);
나만의 지형지물 추가
이 레이어와 함께 자체 마커, 지면 오버레이, 다중선 또는 다각형을
추가하려는 경우에는 GoogleMap
객체에 직접 추가하는 대신 자체 Collection
을
만든 후 Managers
를 사용하여 지형지물을 추가합니다.
예를 들어, 새 마커를 추가하는 방법은 다음과 같습니다.
Kotlin
val markerCollection =
markerManager.newCollection()
markerCollection.addMarker(
MarkerOptions()
.position(LatLng(51.150000, -0.150032))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Unclustered marker")
)
Java
MarkerManager.Collection markerCollection = markerManager.newCollection();
markerCollection.addMarker(new MarkerOptions()
.position(new LatLng(51.150000, -0.150032))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.title("Unclustered marker"));
클릭 이벤트 처리
클러스터, KML, GeoJSON의 경우, 현재 설정 중인 레이어의 생성자에서 Manager
클래스를
전달하는 동안 클릭 리스너가 정상적으로 작동합니다.
예를 들어, KML 레이어의 클릭 리스너를 설정하는 방법은 다음과 같습니다.
Kotlin
kmlPolylineLayer.addLayerToMap()
kmlPolylineLayer.setOnFeatureClickListener { feature: Feature ->
Toast.makeText(context,
"KML polyline clicked: ${feature.getProperty("name")}",
Toast.LENGTH_SHORT
).show()
}
Java
kmlPolylineLayer.addLayerToMap();
kmlPolylineLayer.setOnFeatureClickListener(feature -> Toast.makeText(context,
"KML polyline clicked: " + feature.getProperty("name"),
Toast.LENGTH_SHORT).show());
나만의 마커, 지면 오버레이, 다중선, 다각형을 추가할 때 Collection
객체에
클릭 리스너를 꼭 추가해야 합니다. 예를 들면 markerCollection
에 마커 클릭 리스너를 설정하는 방법은
다음과 같습니다.
Kotlin
markerCollection.setOnMarkerClickListener { marker: Marker ->
Toast.makeText(
context,
"Marker clicked: ${marker.title}",
Toast.LENGTH_SHORT
).show()
false
}
Java
markerCollection.setOnMarkerClickListener(marker -> { Toast.makeText(context,
"Marker clicked: " + marker.getTitle(),
Toast.LENGTH_SHORT).show();
return false;
});
데모 앱 보기
여러 레이어를 추가하는 예를 보려면 유틸리티 라이브러리와 함께 제공되는 데모
앱에서 MultiLayerDemoActivity
를 확인하세요. 설정 가이드는 데모 앱을 실행하는 방법을
보여줍니다.
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2024-08-13(UTC)
[null,null,["최종 업데이트: 2024-08-13(UTC)"],[[["\u003cp\u003eThis guide explains how to add and manage multiple map layers like KML, GeoJSON, and Clusters on the same map using the Maps SDK for Android Utility Library.\u003c/p\u003e\n"],["\u003cp\u003eIt covers using \u003ccode\u003eManager\u003c/code\u003e objects to handle click events for these layers and how to add your own features.\u003c/p\u003e\n"],["\u003cp\u003eClick listeners function as usual for KML, GeoJSON, and Clusters when the \u003ccode\u003eManager\u003c/code\u003e classes are passed to the layer's constructor.\u003c/p\u003e\n"],["\u003cp\u003eFor custom markers and overlays, use \u003ccode\u003eCollection\u003c/code\u003e objects and add click listeners to them for event handling.\u003c/p\u003e\n"],["\u003cp\u003eA demo app, \u003ccode\u003eMultiLayerDemoActivity\u003c/code\u003e, showcases the implementation of these concepts.\u003c/p\u003e\n"]]],[],null,["# Google Maps Multilayer Utility\n\nSelect platform: [Android](/maps/documentation/android-sdk/utility/multilayer \"View this page for the Android platform docs.\") [JavaScript](/maps/documentation/javascript/layers \"View this page for the JavaScript platform docs.\")\n\n1. [Introduction](#introduction)\n2. [Adding multiple cluster, KML, and GeoJSON layers](#add-multiple)\n3. [Adding your own features](#add-features)\n4. [Handling click events](#handle-click)\n5. [See the demo app](#demo-app)\n\nIntroduction\n------------\n\n\nIn previous tutorials, you've learned how to add features from\n[KML](/maps/documentation/android-sdk/utility/kml) and\n[GeoJSON](/maps/documentation/android-sdk/utility/geojson) to your map, as well as\n[clusters](/maps/documentation/android-sdk/utility/marker-clustering) of markers.\nBut what if you want to add several of these layers on the same map and get independent click\nevents for each?\n\nAdding multiple cluster, KML, and GeoJSON layers\n------------------------------------------------\n\n\nThe library includes `Manager`objects to help manage click events for multiple types\nof layers. So, before you set up your layers you'll first need to instantiate these and pass in\nyour `GoogleMap`: \n\n### Kotlin\n\n```kotlin\nval markerManager = MarkerManager(map)\nval groundOverlayManager = GroundOverlayManager(map!!)\nval polygonManager = PolygonManager(map)\nval polylineManager = PolylineManager(map)\n\n \n```\n\n### Java\n\n```java\nMarkerManager markerManager = new MarkerManager(map);\nGroundOverlayManager groundOverlayManager = new GroundOverlayManager(map);\nPolygonManager polygonManager = new PolygonManager(map);\nPolylineManager polylineManager = new PolylineManager(map);\n\n \n```\n\n\nNext, you can pass these manager classes into the constructors of the other layers when you\nset them up: \n\n### Kotlin\n\n```kotlin\nval clusterManager =\n ClusterManager\u003cMyItem\u003e(context, map, markerManager)\nval geoJsonLineLayer = GeoJsonLayer(\n map,\n R.raw.geojson_file,\n context,\n markerManager,\n polygonManager,\n polylineManager,\n groundOverlayManager\n)\nval kmlPolylineLayer = KmlLayer(\n map,\n R.raw.kml_file,\n context,\n markerManager,\n polygonManager,\n polylineManager,\n groundOverlayManager,\n null\n)\n\n \n```\n\n### Java\n\n```java\nClusterManager\u003cMyItem\u003e clusterManager = new ClusterManager\u003c\u003e(context, map, markerManager);\nGeoJsonLayer geoJsonLineLayer = new GeoJsonLayer(map, R.raw.geojson_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager);\nKmlLayer kmlPolylineLayer = new KmlLayer(map, R.raw.kml_file, context, markerManager, polygonManager, polylineManager, groundOverlayManager, null);\n\n \n```\n\nAdding your own features\n------------------------\n\n\nIf you want to add your own markers, ground overlays, polylines, or polygons alongside these\nlayers, create your own `Collection` and then use the `Managers`\nto add the feature instead of directly adding them to the `GoogleMap` object.\n\nFor example, if you want to add a new marker: \n\n### Kotlin\n\n```kotlin\nval markerCollection =\n markerManager.newCollection()\nmarkerCollection.addMarker(\n MarkerOptions()\n .position(LatLng(51.150000, -0.150032))\n .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))\n .title(\"Unclustered marker\")\n)\n\n \n```\n\n### Java\n\n```java\nMarkerManager.Collection markerCollection = markerManager.newCollection();\nmarkerCollection.addMarker(new MarkerOptions()\n .position(new LatLng(51.150000, -0.150032))\n .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))\n .title(\"Unclustered marker\"));\n\n \n```\n\nHandling click events\n---------------------\n\n\nFor clusters, KML, and GeoJSON, click listeners work like normal - as long as you pass in the\n`Manager` classes in the constructor of the layer you're setting.\n\nFor example, here's how to set up a click listener for the KML layer: \n\n### Kotlin\n\n```kotlin\nkmlPolylineLayer.addLayerToMap()\nkmlPolylineLayer.setOnFeatureClickListener { feature: Feature -\u003e\n Toast.makeText(context,\n \"KML polyline clicked: ${feature.getProperty(\"name\")}\",\n Toast.LENGTH_SHORT\n ).show()\n}\n\n \n```\n\n### Java\n\n```java\nkmlPolylineLayer.addLayerToMap();\nkmlPolylineLayer.setOnFeatureClickListener(feature -\u003e Toast.makeText(context,\n \"KML polyline clicked: \" + feature.getProperty(\"name\"),\n Toast.LENGTH_SHORT).show());\n\n \n```\n\n\nWhen you add your own markers, ground overlays, polylines, or polygons, just be sure to add click\nlisteners to those `Collection` objects. For example, here's how to set up the marker\nclick listener on the `markerCollection`: \n\n### Kotlin\n\n```kotlin\nmarkerCollection.setOnMarkerClickListener { marker: Marker -\u003e\n Toast.makeText(\n context,\n \"Marker clicked: ${marker.title}\",\n Toast.LENGTH_SHORT\n ).show()\n false\n}\n\n \n```\n\n### Java\n\n```java\nmarkerCollection.setOnMarkerClickListener(marker -\u003e { Toast.makeText(context,\n \"Marker clicked: \" + marker.getTitle(),\n Toast.LENGTH_SHORT).show();\n return false;\n});\n\n \n```\n\nSee the demo app\n----------------\n\nFor an example of adding multiple layers, take a look at the `MultiLayerDemoActivity`\nin the demo app that ships with the utility library. The [setup guide](/maps/documentation/android-sdk/utility/setup) shows you how to run\nthe demo app."]]