Android Studio

Kakao map 마커 찍기 [ Android Studio ]

Tennessee201 2021. 5. 30. 02:21
728x90

 

메인액티비티 onCreate안에 아래의 코드를 추가합니다.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        //지도
        // java code
        mapView = new MapView(this);
        mapViewContainer = (ViewGroup) findViewById(R.id.map_view);
        mapViewContainer.addView(mapView);
        mapView.setMapViewEventListener(this);
        mapView.setCurrentLocationTrackingMode(MapView.CurrentLocationTrackingMode.TrackingModeOnWithHeading);

        MapPOIItem marker = new MapPOIItem();

        //맵 포인트 위도경도 설정
        MapPoint mapPoint = MapPoint.mapPointWithGeoCoord(35.898054, 128.544296);
        marker.setItemName("Default Marker");
        marker.setTag(0);
        marker.setMapPoint(mapPoint);
        marker.setMarkerType(MapPOIItem.MarkerType.BluePin); // 기본으로 제공하는 BluePin 마커 모양.
        marker.setSelectedMarkerType(MapPOIItem.MarkerType.RedPin); // 마커를 클릭했을때, 기본으로 제공하는 RedPin 마커 모양.

        mapView.addPOIItem(marker);
        if (!checkLocationServicesStatus()) {
            showDialogForLocationServiceSetting();
        }else {
            checkRunTimePermission();
        }
    }

코드에 대해 간략히 설명하면 MapPOIItem이라는 객체를 생성하고 mapPoint변수에다가 MapPoint에 위도경도값을 넣어줍니다.

그리고 아까 만들었던 MapPOIIItem객체를 만들었던 maker변수에다가 마커이름, 마커태그, 맵포인트(마커포인트), 마커타입, 마커클릭타입을 시정하게 됩니다. setItemName에다가 마커이름을 넣어주면 되고 SetMapPoint에다가 mapPoint변수를 넣어주면 되고 setMakerType은 마커핀모양입니다. setSelectedMakerType은 마커를 클릭했을 때의 핀 스타일을 설정합니다.

그리고 이를 mapView객체에 추가해주면 됩니다.

728x90