Skip to content

Instantly share code, notes, and snippets.

@9paradox
Last active May 5, 2018 16:24
Show Gist options
  • Save 9paradox/7360da616d9940994a8d49778ea2dc8d to your computer and use it in GitHub Desktop.
Save 9paradox/7360da616d9940994a8d49778ea2dc8d to your computer and use it in GitHub Desktop.
Get Position on Map using lat/lang with a draggable marker (Google Map API v3)
<style>
#map-canvas{
height: 500px;
width: 500px;
border: 3px solid black;
}
</style>
<br>
<br>
<input type="text" id="latitude" value="15.6002">
<input type="text" id="longitude" value="73.8125">
<br>
<br>
<div id="map-canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false" type="text/javascript"></script>
<script>
var latInput = document.getElementById('latitude');
var lonInput = document.getElementById('longitude');
if (document.getElementById('map-canvas')){
var address ="my map"
var myLatLng = { lat: 15.6002, lng: 73.8125 };
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 17,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: address,
draggable: true,
});
google.maps.event.addListener(marker, 'dragend', function (marker) {
var latLng = marker.latLng;
currentLatitude = latLng.lat();
currentLongitude = latLng.lng();
latInput.value=currentLatitude;
lonInput.value=currentLongitude;
});
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment