좌표간 거리측정
routogl 지도에 표시된 2개의 마커 좌표간의 거리를 측정하는 예제입니다.
const map = new routogl.Map({
container: 'map', // container ID
style: routogl.RoutoStyle.LIGHT,
center: [127.0586339, 37.507009], // 초기 위치 [lng, lat]
zoom: 13, // 초기 줌 레벨
});
// 마커 생성 1
const marker1 = new routogl.Marker({
color: 'blue'
})
.setLngLat([127.0586339, 37.507009])
.addTo(map);
// 마커 생성 2
const marker2 = new routogl.Marker({
color: 'red'
})
.setLngLat([127.036390, 37.501009])
.addTo(map);
// 마커 좌표간의 거리측정
const getDistance = (marker1, marker2) => {
// turf.js 라이브러리의 distance 함수를 이용한 거리 계산
const from = turf.point(marker1.getLngLat().toArray());
const to = turf.point(marker2.getLngLat().toArray());
const distance = turf.distance(from, to);
const distanceElement = document.getElementById('distance');
distanceElement.textContent = '두 마커 좌표간 거리: ' + distance + 'km';
};
getDistance(marker1, marker2);
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"
/>
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>routo Developers - 지도 - 좌표간 거리측정</title>
<link href="https://api.mapbox.com/mapbox-gl-js/v3.7.0/mapbox-gl.css" rel="stylesheet">
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css"
/>
<style>html, body { margin: 0; padding: 0; } div#map { width: 100%; height: 50vh; }</style>
</head>
<body>
<main>
<div class="container">
<header
class="d-flex flex-wrap justify-content-center py-3 mb-4 border-bottom"
>
<a
href="/"
class="d-flex align-items-center mb-3 mb-md-0 me-md-auto text-dark text-decoration-none"
>
<span class="fs-4">routo Map Examples</span>
</a>
<nav aria-label="breadcrumb">
<ol class="breadcrumb p-3 bg-body-tertiary rounded-3"></ol>
</nav>
</header>
<examplecontent>
<h2 id="title" class="mb-5">좌표간 거리측정</h2>
<hr/>
<p id="desc" class="text-start text-break fs-5">
routogl 지도에 표시된 2개의 마커 좌표간의 거리를 측정하는 예제입니다.
</p>
<!-- 지도 -->
<div id="map" class="mb-3 map"></div>
<p id="distance"></p>
</examplecontent>
</div>
</main>
</body>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://stg-api.routo.com/v3/maps/map?key=c914fb43-0d44-4fa7-838a-84619e3486c3"></script>
<script src="https://unpkg.com/@turf/turf@6/turf.min.js"></script>
<script>
const map = new routogl.Map({
container: 'map', // container ID
style: routogl.RoutoStyle.LIGHT,
center: [127.0586339, 37.507009], // 초기 위치 [lng, lat]
zoom: 13, // 초기 줌 레벨
});
// 마커 생성 1
const marker1 = new routogl.Marker({
color: 'blue'
})
.setLngLat([127.0586339, 37.507009])
.addTo(map);
// 마커 생성 2
const marker2 = new routogl.Marker({
color: 'red'
})
.setLngLat([127.036390, 37.501009])
.addTo(map);
// 마커 좌표간의 거리측정
const getDistance = (marker1, marker2) => {
// turf.js 라이브러리의 distance 함수를 이용한 거리 계산
const from = turf.point(marker1.getLngLat().toArray());
const to = turf.point(marker2.getLngLat().toArray());
const distance = turf.distance(from, to);
const distanceElement = document.getElementById('distance');
distanceElement.textContent = '두 마커 좌표간 거리: ' + distance + 'km';
};
getDistance(marker1, marker2);</script>
</html>