【问题标题】:Lat, Lng doesn't update when I drag marker当我拖动标记时,Lat,Lng 不更新
【发布时间】:2016-02-02 12:08:57
【问题描述】:

实际上,我有一张带有可拖动标记的地图。这很好用,但我无法更新拖动标记的 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>

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

【问题讨论】:

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


【解决方案1】:

你需要获取标记的位置,并在拖动标记时更新表单域:

google.maps.event.addListener(marker,'dragend', function(evt) {
    document.getElementById('lat').value = this.getPosition().lat();
    document.getElementById('lng').value = this.getPosition().lng();
});

代码 sn-p:

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%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input type="button" id="geolocate" value="geolocate" />
<input id="street" value="" />
<input id="city" value="Saltzburg" />
<br/>
<input id="zip" value="" />
<br/>
<input id="lat" />
<input id="lng" />

<div id="map"></div>

【讨论】:

  • 谢谢。效果很好!
  • @geocodezip:我们可以更新 marker.setPostion(latlang) JS 调用的纬度字段吗??
【解决方案2】:

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

    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());
}

【讨论】:

  • 这无济于事,因为我想从我拖动的标记中获取 lat 和 lng(此标记来自地理位置)。
  • 你是对的。请参阅上面的编辑。我现在正在使用 dragend 事件并将其添加到标记中,然后检查 marker.getPosition() 以调用将其保存到我拥有的隐藏字段的函数。
猜你喜欢
  • 2016-11-26
  • 1970-01-01
  • 2012-03-13
  • 1970-01-01
  • 2015-12-17
  • 2017-10-23
  • 1970-01-01
  • 2019-08-11
  • 2020-08-13
相关资源
最近更新 更多