Skip to main content
cuerpo editado
Origen Enlace
ffflabs
  • 23k
  • 28
  • 50

Dentro del evento, la variable this es el marker.

var map;
function initialize() {
   map = new google.maps.Map(document.getElementById('map-canvas'), {
     zoom: 3,
     center: {lat: -10, lng: -60}
   });
   
   var marker=new google.maps.Marker({
      position:map.getCenter(), 
      map:map, 
      draggable:true
   });
            
   google.maps.event.addListener(marker,'dragend',function(event) {
      document.getElementById("coords").value = this.getPosition().toString();
   });
}
google.maps.event.addDomListener(window, 'load', initialize);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvBQ1Q9FjYfxdWJF6KPmO822RMMbY2w2o"><key=AIzaSyBRqo4qwqHHrtbY9aL6vYYDw1GOhkK97MQ"></script>

<div class="col-md-12 col-sm-12">
   <input type="text" style="width:400px" id="coords" />
<div>
<div class="col-md-12 col-sm-12" id="map-canvas" style="height:200px;"></div>

Revisa el plunkr que te hice

Dentro del evento, la variable this es el marker.

var map;
function initialize() {
   map = new google.maps.Map(document.getElementById('map-canvas'), {
     zoom: 3,
     center: {lat: -10, lng: -60}
   });
   
   var marker=new google.maps.Marker({
      position:map.getCenter(), 
      map:map, 
      draggable:true
   });
            
   google.maps.event.addListener(marker,'dragend',function(event) {
      document.getElementById("coords").value = this.getPosition().toString();
   });
}
google.maps.event.addDomListener(window, 'load', initialize);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvBQ1Q9FjYfxdWJF6KPmO822RMMbY2w2o"></script>

<div class="col-md-12 col-sm-12">
   <input type="text" style="width:400px" id="coords" />
<div>
<div class="col-md-12 col-sm-12" id="map-canvas" style="height:200px;"></div>

Revisa el plunkr que te hice

Dentro del evento, la variable this es el marker.

var map;
function initialize() {
   map = new google.maps.Map(document.getElementById('map-canvas'), {
     zoom: 3,
     center: {lat: -10, lng: -60}
   });
   
   var marker=new google.maps.Marker({
      position:map.getCenter(), 
      map:map, 
      draggable:true
   });
            
   google.maps.event.addListener(marker,'dragend',function(event) {
      document.getElementById("coords").value = this.getPosition().toString();
   });
}
google.maps.event.addDomListener(window, 'load', initialize);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBRqo4qwqHHrtbY9aL6vYYDw1GOhkK97MQ"></script>

<div class="col-md-12 col-sm-12">
   <input type="text" style="width:400px" id="coords" />
<div>
<div class="col-md-12 col-sm-12" id="map-canvas" style="height:200px;"></div>

Revisa el plunkr que te hice

Origen Enlace
ffflabs
  • 23k
  • 28
  • 50

Dentro del evento, la variable this es el marker.

var map;
function initialize() {
   map = new google.maps.Map(document.getElementById('map-canvas'), {
     zoom: 3,
     center: {lat: -10, lng: -60}
   });
   
   var marker=new google.maps.Marker({
      position:map.getCenter(), 
      map:map, 
      draggable:true
   });
            
   google.maps.event.addListener(marker,'dragend',function(event) {
      document.getElementById("coords").value = this.getPosition().toString();
   });
}
google.maps.event.addDomListener(window, 'load', initialize);
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAvBQ1Q9FjYfxdWJF6KPmO822RMMbY2w2o"></script>

<div class="col-md-12 col-sm-12">
   <input type="text" style="width:400px" id="coords" />
<div>
<div class="col-md-12 col-sm-12" id="map-canvas" style="height:200px;"></div>

Revisa el plunkr que te hice