title: google maps api get lat and lng and replace marker?
body: <p>So at the moment when user enter address it adds the marker which is fine, user can also move the marker to a new location and retrieve it's lat and lng. However what it doesn't do is:</p>
  
  <ol>
  <li>Get lat and lng from the address (when pin is added to page)</li>
  <li>If address changes it adds a new marker instead of replacing existing one.</li>
  </ol>
  
  <p>Here's the code:</p>
  
  <pre><code>var map;
  function initMap() {
      map = new google.maps.Map(document.getElementById('map'), {
      zoom: 8,
      center: {lat: -34.397, lng: 150.644}
      });
      var geocoder = new google.maps.Geocoder();
      document.getElementById('postcode').addEventListener('keyup', function() {
          geocodeAddress(geocoder, map);
      });
  }
  function geocodeAddress(geocoder, resultsMap) {
      var address = document.getElementById('address1').value + '\xa0' + document.getElementById('address2').value +','+ document.getElementById('postcode').value;
      console.log(address);
      geocoder.geocode({'address': address}, function(results, status) {
          if (status === 'OK') {
              //if marker exist, replace it if not create it.
              map.setCenter(results[0].geometry.location);
              var marker = new google.maps.Marker({
                  map: map,
                  position: results[0].geometry.location,
                  title: address,
                  draggable:true,
                  //get lng and lat and save it to 2 seperate variables?
              });
          google.maps.event.addListener(marker, 'dragend', function(marker){
          var latLng = marker.latLng; 
          currentLatitude = latLng.lat();
          currentLongitude = latLng.lng();
          console.log(currentLatitude);
          console.log(currentLongitude);
          $("#latitude").val(currentLatitude);
          $("#longitude").val(currentLongitude);
                  });
            } else {
              $('#error').append('&lt;div class="alert alert-danger"&gt;Address not found please try again&lt;/div&gt;');
              $(".alert-danger").fadeOut(5000);  
            }
          });
        }
  </code></pre>