简体   繁体   English

如何获取Google Maps API v3来侦听dragend事件并在拖放时填充表单字段?

[英]How to get Google Maps API v3 to listen for dragend event and populate form field on drop?

I am trying to get the lat and lng of a clicked location on a Google map, which I will then store in a db. 我正在尝试获取Google地图上单击位置的经度和纬度,然后将其存储在数据库中。 I've gotten it to the point where I can click on the map location and the form field populates, but I'd also like to be able to drag and drop the marker to a new location and update the form field on the dragend event. 我已经找到了可以单击地图位置并填充表单字段的位置,但是我还希望能够将标记拖放到新位置并在dragend事件上更新表单字段。 Any idea how to accomplish this? 任何想法如何做到这一点?

The HTML: HTML:

<div id="container">
  <div class="row-fluid">
    <div class="span10 offset1">
      <div id="holyMapDiv"></div>
    </div>
  </div>
  <div class="row-fluid">
    <div class="span10 offset1">
      <div class="bradyRules">
        <div>Lattitude: <span id="latspan"></span></div>
        <div>Longitude: <span id="lngspan"></span></div>
        <div>Lat Lng: <span id="latlong"></span></div>
        <div>Lat Lng on click: 
        <input type="text" id="latlongclicked"></input></div>
      </div>
    </div>
  </div>
</div>

The JavaScript: JavaScript:

var map;
var marker;

function updateMarkerPosition(latLng) {
  document.getElementById('latlongclicked').innerHTML = [
    latLng.lat(),
    latLng.lng()
  ].join(', ');
}

function mapa()
{
  var opts = {'center': new google.maps.LatLng(39.73757, -104.98472), 'zoom':8, 'mapTypeId': google.maps.MapTypeId.TERRAIN, draggableCursor: 'crosshair'}
    map = new google.maps.Map(document.getElementById('holyMapDiv'),opts);

  google.maps.event.addListener(map,'click',function(event) {
    document.getElementById('latlongclicked').value = event.latLng.lat() + ', ' + event.latLng.lng();
    placeMarker(event.latLng);
  })

  google.maps.event.addListener(map,'mousemove',function(event) {
    document.getElementById('latspan').innerHTML = event.latLng.lat();
    document.getElementById('lngspan').innerHTML = event.latLng.lng();
    document.getElementById('latlong').innerHTML = event.latLng.lat() + ', ' +     event.latLng.lng();
  });

  google.maps.event.addListener(marker,'dragend', function(event) {
    updateMarkerPosition(marker.getPosition());
  });

}

function placeMarker(location) {
  if ( marker ) {
    marker.setPosition(location);
  } else {
    marker = new google.maps.Marker({
      position: location,
      map: map,
      animation: google.maps.Animation.DROP,
      draggable: true
    });
  }
}

window.onload = mapa

Thank you. 谢谢。

  1. you must add the dragend -event at the end of the else-branch in placeMarker() , at the current position marker is undefined when you add the listener 您必须在placeMarker()的else分支的末尾添加dragend -event,在添加侦听器时当前位置marker undefined
  2. in updateMarkerPosition() you set the innerHTML of a text-input(what usually will not have any effect), set the value instead updateMarkerPosition() ,设置文本输入的innerHTML (通常不会产生任何效果),而是设置value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM