我拖动标记时Lat,Lng不会更新

时间:2015-11-02 14:56:43

标签: javascript jquery google-maps-api-3 google-maps-markers

实际上,我有一个带有可拖动标记的地图。这工作正常,但我无法更新拖动标记的lat和lng。我已经尝试了几个不同的事件监听器,但没有它们工作。现在我想知道是否有人可以帮助我?

我的代码(我删除了您不会感到困惑的脚本):

<script>
function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        zoom: 7,
        center: {
            lat: 47.532446,
            lng: 14.623283
        }
    });
    var geocoder = new google.maps.Geocoder();

    document.getElementById('geolocate').addEventListener('click', function () {
        geocodeAddress(geocoder, map);
    });
    }

function geocodeAddress(geocoder, resultsMap) {
    var address = document.getElementById('street').value + ' ' +
        document.getElementById('zip').value + ' ' +
        document.getElementById('city').value;

geocoder.geocode({
    'address': address
}, function (results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
        resultsMap.setCenter(results[0].geometry.location);

        document.getElementById('lat').value = results[0].geometry.location.lat();
        document.getElementById('lng').value = results[0].geometry.location.lng();

        var marker = new google.maps.Marker({
            map: resultsMap,
            position: results[0].geometry.location,
            draggable: true
        });

        resultsMap.setZoom(17);
        resultsMap.panTo(marker.position);
    } else {
        alert('Geocode was not successful for the following reason: ' + status);
    }
});
} 
</script>

如果有人可以帮助我,我会很高兴的。

2 个答案:

答案 0 :(得分:1)

您需要获取标记的位置并在拖动标记时更新表单字段:

If

代码段

google.maps.event.addListener(marker,'dragend', function(evt) {
    document.getElementById('lat').value = this.getPosition().lat();
    document.getElementById('lng').value = this.getPosition().lng();
});
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 7,
    center: {
      lat: 47.532446,
      lng: 14.623283
    }
  });
  var geocoder = new google.maps.Geocoder();

  document.getElementById('geolocate').addEventListener('click', function() {
    geocodeAddress(geocoder, map);
  });
}

function geocodeAddress(geocoder, resultsMap) {
  var address = document.getElementById('street').value + ' ' +
    document.getElementById('zip').value + ' ' +
    document.getElementById('city').value;

  geocoder.geocode({
    'address': address
  }, function(results, status) {
    if (status === google.maps.GeocoderStatus.OK) {
      resultsMap.setCenter(results[0].geometry.location);

      document.getElementById('lat').value = results[0].geometry.location.lat();
      document.getElementById('lng').value = results[0].geometry.location.lng();

      var marker = new google.maps.Marker({
        map: resultsMap,
        position: results[0].geometry.location,
        draggable: true
      });
      google.maps.event.addListener(marker, 'dragend', function(evt) {
        document.getElementById('lat').value = this.getPosition().lat();
        document.getElementById('lng').value = this.getPosition().lng();
      });

      resultsMap.setZoom(17);
      resultsMap.panTo(marker.position);
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}
google.maps.event.addDomListener(window, 'load', initMap);
html,
body,
#map {
  width: 100%;
  height: 100%;
}

答案 1 :(得分:0)

我在拖动/点击时创建了一个新标记:

    myListener = google.maps.event.addListener(map, 'click', function (event) {
        placeMarker(event.latLng);
    });
function placeMarker(location) {
    if (marker) {
        marker.setPosition(location);
    } else {
        marker = new google.maps.Marker({
            position: location,
            map: map,
            draggable: true
        });
        google.maps.event.addListener(marker, "dragend", function (mEvent) {
            saveLocation(mEvent.latLng);
        });
    }
    saveLocation(marker.getPosition());
}
function saveLocation(pos) {
    $("#hfLatitude").val(pos.lat());
    $("#hfLogitude").val(pos.lng());
}