Created
April 30, 2024 06:23
-
-
Save CarsonSlovoka/691e5b9186b2e2c73dffacf99db9c4fb to your computer and use it in GitHub Desktop.
lng, lat, r在一個經緯度的位置,顯示半徑r的圓圈
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>顯示經緯度座標和繪製圓圈</title> | |
<script async src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&loading=async&callback=initMap"></script> | |
</head> | |
<body> | |
<div id="map" style="height: 500px; width: 100%;"></div> | |
</body> | |
<script> | |
window.initMap = () => { | |
let circle | |
const map = new google.maps.Map(document.getElementById("map"), { | |
zoom: 2, | |
center: { lat: 0, lng: 0 }, | |
}) | |
map.addListener("click", (event) => { | |
const lat = event.latLng.lat() | |
const lng = event.latLng.lng() | |
const latlng = { lat, lng } | |
if (circle) { | |
circle.setMap(null) | |
} | |
const radius = prompt("請輸入半徑(公里):") | |
if (radius) { | |
circle = new google.maps.Circle({ | |
strokeColor: "#FF0000", | |
strokeOpacity: 0.8, | |
strokeWeight: 2, | |
fillColor: "#FF0000", | |
fillOpacity: 0.35, | |
map, | |
center: latlng, | |
radius: parseFloat(radius) * 1000, // 轉換成公里 | |
}) | |
} | |
alert(`您點擊的位置經緯度為: (${lat}, ${lng})`) | |
}) | |
} | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment