WEB GL JS

웹 개발, 어플리케이션에서 활용될 수 있도록 Javascript로 제공되는 지도 플랫폼 입니다.

좌표간 거리측정


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);