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.
For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.
getPosition() returns an object, so to specifically get latitude/longitude, you'll have to call lat() and lng() respectively from that.
Finally I found the answer:
marker = new google.maps.Marker( { map:map, draggable:true, animation: google.maps.Animation.DROP, position: results[0].geometry.location }); google.maps.event.addListener(marker, 'dragend', function() { geocodePosition(marker.getPosition()); }); function geocodePosition(pos) { geocoder = new google.maps.Geocoder(); geocoder.geocode ({ latLng: pos }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { $("#mapSearchInput").val(results[0].formatted_address); $("#mapErrorMsg").hide(100); } else { $("#mapErrorMsg").html('Cannot determine address at this location.'+status).show(100); } } ); }
Source The Wizz.art
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
function showMap(lat,lang,address) { var myLatLng = {lat: lat, lng: lang}; 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(); jQ("#latitude").val(currentLatitude); jQ("#longitude").val(currentLongitude); }); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With