Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google maps v3 draggable marker

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.

like image 766
OHLÁLÁ Avatar asked Apr 16 '11 18:04

OHLÁLÁ


People also ask

How do I add a marker to Google Maps Android?

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.

How do I find the latitude and longitude of a marker on Google Maps?

getPosition() returns an object, so to specifically get latitude/longitude, you'll have to call lat() and lng() respectively from that.


2 Answers

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

like image 174
OHLÁLÁ Avatar answered Sep 28 '22 04:09

OHLÁLÁ


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);      });  } 
like image 45
Ahmad Vaqas Khan Avatar answered Sep 28 '22 03:09

Ahmad Vaqas Khan