📅 2024-09-17 개인프로젝트 18일차 - 지도 API
TODO
- 지도 시작지점 사용자 IP GPS 따와서 넣어주기
1. 지도 시작지점 사용자 IP GPS 따와서 넣어주기
- 브라우저의 HTML5 Geolocation API를 사용하여 위치를 추적하고, 해당 위치로 지도를 초기화하기 위해 아래의 코드를 추가했다.
// 브라우저의 Geolocation API를 사용하여 위치 가져오기
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
function(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
initializeMap(latitude, longitude); // 사용자 위치로 지도 중심 설정
},
function(error) {
console.error('Geolocation error:', error);
// 위치 정보 가져오기 실패 시 기본 좌표 설정 (서울 시청)
initializeMap(37.566826, 126.9786567);
}
);
} else {
console.error('Geolocation is not supported by this browser.');
// Geolocation 지원 안 함 시 기본 좌표 설정 (서울 시청)
initializeMap(37.566826, 126.9786567);
}
}
getLocation(); // 위치 정보 가져오기
- 해당 위치를 알맞게 가져온 지도의 모습 (원래는 서울시청이 중심이었다.)
