Skip to content

Instantly share code, notes, and snippets.

@muath-ye
Created June 22, 2023 12:14
Show Gist options
  • Save muath-ye/303470fdb6210e274f95b77e2e385473 to your computer and use it in GitHub Desktop.
Save muath-ye/303470fdb6210e274f95b77e2e385473 to your computer and use it in GitHub Desktop.
Here is an example of using google maps
<!DOCTYPE html>
<html>
<head>
<title>Google Maps Example</title>
<meta name="viewport" content="initial-scale=1.0">
<meta charset="utf-8">
<style>
#map {
height: 400px;
width: 100%;
}
</style>
</head>
<body>
<h1>Choose a location on the map</h1>
<div id="map"></div>
<script>
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 37.7749, lng: -122.4194},
zoom: 10
});
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
draggable: true
});
google.maps.event.addListener(marker, 'dragend', function() {
document.getElementById('lat').value = marker.getPosition().lat();
document.getElementById('lng').value = marker.getPosition().lng();
});
google.maps.event.addListener(map, 'click', function(event) {
marker.setPosition(event.latLng);
document.getElementById('lat').value = event.latLng.lat();
document.getElementById('lng').value = event.latLng.lng();
});
}
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"></script>
<form>
<label for="lat">Latitude:</label>
<input type="text" id="lat" name="lat">
<br>
<label for="lng">Longitude:</label>
<input type="text" id="lng" name="lng">
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment