简体   繁体   English

使用Google Maps标记坐标作为表单输入

[英]Use Google Maps marker coordinates as form input

From the below javascript function, I get a javascript variable marker with coordinates when the user clicks a position on the map. 从下面的javascript函数中,当用户单击地图上的某个位置时,我得到了一个带有坐标的javascript变量标记

var marker;
function placeMarker(location) {
  if ( marker ) {
    marker.setPosition(location);
  } else {
    marker = new google.maps.Marker({
      position: location,
      map: map
    });
  }
}
google.maps.event.addListener(map, 'click', function(event) {
  placeMarker(event.latLng);
});

Question How can these coordinates be used as form input? 问题如何将这些坐标用作表单输入?

<form method="POST" action="/authors" target="" enctype="multipart/form-data">
   ...
</form>

All you have to do is take the location variable and assign it to your form input like this: 您所需要做的就是获取location变量,并将其分配给表单输入,如下所示:

First, add a hidden input field 首先,添加一个隐藏的输入字段

<form method="POST" action="/authors" target="" enctype="multipart/form-data">
   <input id="location" name="location" type="hidden" value="" />
</form>

Second, make sure the location variable gets added to your input 其次,确保将位置变量添加到您的输入中

var marker;
function placeMarker(location) {
  if ( marker ) {
    marker.setPosition(location);
  } else {
    marker = new google.maps.Marker({
      position: location,
      map: map
    });
  }

  $("#location").val(location);

}
google.maps.event.addListener(map, 'click', function(event) {
  placeMarker(event.latLng);
});

Now, whenever placeMarker(location) gets called it will update the input with the id of "location" and its value is the output of location . 现在,每当placeMarker(location)被调用时,它将使用id的“ location”更新输入,其值是location的输出。

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

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