반응형

지도 위에 마커 생성


<!DOCTYPE html>
<html>
	<head>
        <title>오픈레이어스 클릭 이벤트</title>
        <script src="https://cdn.jsdelivr.net/npm/ol@v9.2.4/dist/ol.js"></script>
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ol@v9.2.4/ol.css">

        <style>
            .map {
                height: 960px;
                width: 100%;
            }
        </style>
	</head>
    <body>
        <div id="map" class="map"></div>
    	<script>
            let map = new ol.Map({
                target: 'map',
                layers: [
                    new ol.layer.Tile({
                        source: new ol.source.OSM({
                            url: 'https://maps.wikimedia.org/osm-intl/{z}/{x}/{y}.png'
                        })
                    })
                ],
                view: new ol.View({
                    center: ol.proj.fromLonLat([128.4, 35.7]),
                    zoom: 7
                })
            });
            
            // 클릭 이벤트부분
            map.on('click', function(evt){
            	var feature = new ol.Feature({
                    geometry: new ol.geom.Point(evt.coordinate)
                });
         
                // 마커 스타일 설정
                var style = new ol.style.Style({
                    image: new ol.style.Icon({
                        src: 'http://map.vworld.kr/images/ol3/marker_blue.png'
                    }),
                });
                
                // feature에 스타일 설정
                feature.setStyle(style);
         
                // 마커 레이어에 들어갈 소스 생성
                var source = new ol.source.Vector({
                    features: [feature]
                });
         
                // 마커 레이어 생성
                var layer = new ol.layer.Vector({
                    source: source,
                    name: 'MARKER'
                });
         
                // 지도에 마커 추가
                map.addLayer(layer);
            })
            
        </script>
    </body>
</html>

 

지도 위에 V-World에서 제공하는 마커 생성

반응형