I'm new in google maps, and I'm trying to learn it.
marker = new google.maps.Marker(
{
map:map,
draggable:true,
animation: google.maps.Animation.DROP,
position: results[0].geometry.location
});
This is my marker position, when I'm initialising the marker position than I know the place name (for example: XY street, New York,), but because of the draggable option it is changing, and my question is how can I get the new place name, what event handler do I need.
Please follow below three steps to get drag-able marker with position in text box.
CSS
<style> #map { height: 400px; width: 100%; } </style>
HTML
<div id="map"></div> Latitude <input type="text" id="la" name="latitude" value="28.39" > Longitude<input type="text" id="lo" name="longitude" value="84.12" >
JavaScript
<script> /** *Receiving the value of text box and type done conversion by Number() */ var latitude = Number(document.getElementById("la").value); var longitude = Number(document.getElementById("lo").value); function initMap() { /** *Passing the value of variable received from text box **/ var uluru = {lat: latitude, lng: longitude}; var map = new google.maps.Map(document.getElementById('map'), {zoom: 7,center: uluru} ); marker = new google.maps.Marker( { map:map, draggable:true, animation: google.maps.Animation.DROP, position: uluru } ); google.maps.event.addListener(marker, 'dragend', function(marker){ var latLng = marker.latLng; currentLatitude = latLng.lat(); currentLongitude = latLng.lng(); $("#la").val(currentLatitude); $("#lo").val(currentLongitude); } ); } </script>
<!--Google Map API Link--> <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOURAPIKEYHERE&callback=initMap"> </script>
NOTE: Please Use Jquery at the head
Finally I found the answer:
Set a Position on Map using lat/lang and make the marker draggable
Using lat/lang initially sets a marker at the given point on the map
The address variable is used for title purpose. It can be ignored.
draggable:true makes the marker draggable.
Use event listener google.maps.event.addListener(marker, 'dragend', function(marker) To listen for changes in the marker position