언어 기본 문법
-자바 스크립트 용어 출력 (표현식, 문장, 키워드, 식별자, 주석)
-자료형, 변수
-제어문(조건문, 반복문)
=================================
-함수(실행우선순위, 선언, 호출, 매개변수)
-객체(속성, 메서드)
=================================
표현식
값을 만들어내는 간단한 코드
문장
프로그래밍 언어에서 실행할 수 있는 코드의 최소 단위
키워드
특별한 의미가 있는 단어
식별자
변수나 함수 등에 이름을 붙일 때 사용하는 단어. 키워드 사용불가. 특수문자는 언더바(_), 달러(&)만 사용가능
공백 입력불가.
- 클래스 외 생성자 함수 이름은 대문자로 시작
- 변수, 인스턴스, 함수, 메소드 이름은 소문자로 시작
- 여러 단어로 된 식별자는 각 단어의 첫글자를 대문자로 시작
주석 표현
// or /* */
요정도.
<!DOCTYPE html>
<html>
<head>
<title>Get Current Location</title>
</head>
<body>
<script>
function findAddress(lat, lon) {
// Construct the URL for the Nominatim API
const url = `https://nominatim.openstreetmap.org/reverse?format=json&lat=${lat}&lon=${lon}`;
// Use fetch API to get the address
fetch(url)
.then(response => response.json())
.then(data => {
document.body.innerHTML += `<h2>Address: ${data.display_name}</h2>`;
})
.catch(error => {
console.error("Error fetching address:", error);
});
}
function showPosition(position) {
console.log("Latitude: " + position.coords.latitude +
", Longitude: " + position.coords.longitude);
// You can also display this information on the webpage
document.body.innerHTML = `<h2>Latitude: ${position.coords.latitude}</h2>
<h2>Longitude: ${position.coords.longitude}</h2>`;
// Call the findAddress function to get the address
findAddress(position.coords.latitude, position.coords.longitude);
}
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
console.log("User denied the request for Geolocation.");
break;
case error.POSITION_UNAVAILABLE:
console.log("Location information is unavailable.");
break;
case error.TIMEOUT:
console.log("The request to get user location timed out.");
break;
case error.UNKNOWN_ERROR:
console.log("An unknown error occurred.");
break;
}
}
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition, showError);
} else {
console.log("Geolocation is not supported by this browser.");
}
}
getLocation();
</script>
</body>
</html>
현재 위치 뽑는 코드
반응형
'웹 개인공부' 카테고리의 다른 글
JavaScript - Symbol타입, 화살표 함수, for of 문, map컬렉션 (1) | 2024.02.27 |
---|---|
JavaScript - 선언자&에러 (0) | 2024.02.27 |
JavaScript - callback, 문서객체모델 (0) | 2024.02.23 |
CSS - 레이아웃, 요소배치 (1) | 2024.02.22 |
CSS - 단위, 속성 / MDN 서칭 (0) | 2024.02.21 |