title: get current position of marker in google maps
body: <p>I am trying to get the current position that mean longitude and latitude of the marker. First a marker becomes created at the users location and when the user click on the map the previous one becomes deleted and a new one becomes created at the users clickpoint. I tried it by my own with <code>var lat1 = markers.position.lat(); var lng1 = markers.position.lng();</code> but that havent work and I get with this the error message <code>Uncaught TypeError: Cannot read property 'lat' of undefined</code>. How can I get the current position of the marker and save it in a variable?</p>
  
  <pre class="lang-js prettyprint-override"><code>var markers = [];
  // This event listener will call addMarker() when the map is clicked.
  map.addListener('click', function(event) {
    addMarkers(event.latLng);
  });
  //draw a marker at the position of the user
  addMarkers(pos);
  // Adds a marker to the map and push to the array.
  function addMarkers(location) {
    var marker = new google.maps.Marker({
      position: location,
      map: map
    });
    markers.push(marker);
  }
  // Sets the map on all markers in the array.
  function setMapOnAll(map) {
    for (var i = 0; i &lt; markers.length; i++) {
      markers[i].setMap(map);
    }
  }
  // Removes the markers from the map, but keeps them in the array.
  function clearMarkers() {
    setMapOnAll(null);
  }
  // Deletes all markers in the array by removing references to them.
  function deleteMarkers() {
    clearMarkers();
    marker = [];
  }
  </code></pre>