javascript - 谷歌地图(它没有将结果显示到我的输入 Pane 中以保存到我的数据库中)

标签 javascript php jquery google-maps

问题是当我拖动标记时,它会将结果更新到 div 中,但我不知道为什么它没有更新并在最后显示到我的 input 中我的程序。有什么解决方案吗?

<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();

function geocodePosition(pos) {
  geocoder.geocode({
latLng: pos
 }, function(responses) {
if (responses && responses.length > 0) {
  updateMarkerAddress(responses[0].formatted_address);
} else {
  updateMarkerAddress('Cannot determine address at this location.');
}
});
}

function updateMarkerStatus(str) {
  document.getElementById('markerStatus').innerHTML = str;
}

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

function updateMarkerAddress(str) {
  document.getElementById('address').innerHTML = str;
}

function initialize() {
 var latLng = new google.maps.LatLng(21.0000, 78.0000);
 var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 5 ,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
 var marker = new google.maps.Marker({
position: latLng,
title: 'Point A',
map: map,
draggable: true
});

更新当前位置信息。

   updateMarkerPosition(latLng);
   geocodePosition(latLng);

添加拖动事件监听器。

  google.maps.event.addListener(marker, 'dragstart', function() {
  updateMarkerAddress('Dragging...');
 });

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

google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());
 });
}

加载处理程序以启动应用程序。

 google.maps.event.addDomListener(window, 'load', initialize);

</script>

</head>
<body>
  <style>   
  #mapCanvas {
  width: 800px;
  height: 600px;
  float: left;
  }
  #infoPanel {
  float: left;
  margin-left: 10px;
  }
 #infoPanel div {
 margin-bottom: 5px;
 }
  </style>

 <form method="POST" action="gsave.php">
  <div id="mapCanvas"></div>
  <div id="infoPanel">
  <b>Marker status:</b>
  <div id="markerStatus"><i>Click and drag the marker.</i></div>
  <b>Current position:</b>
  <div name="lat" id="info"></div>
  <b>Closest matching address:</b>
  <div name="address" id="address"></div>
  </div>



   <div id="map_canvas" style="background-color: #ffffff"></div>
   <input type="text" id="info" name="lat" style="width:150px"> //i want to show in this input
   <input type="text" id="address" name="address" style="width:150px">//and here also
   <button type="submit" class="btn">Submit</button>

</form>
</body>
</html>

最佳答案

多个元素具有相同的 id 值是无效的 HTML。更改您的脚本:

function updateMarkerAddress(str) {
    document.getElementById('address').innerHTML = str;
    document.getElementById('address_info').innerHTML = str;
}

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

以及你的元素:

<input type="text" id="latlng_info" name="lat" style="width:150px">
<input type="text" id="address_info" name="address" style="width:150px">

对于共享相同 id 的所有其他元素也是如此。

关于javascript - 谷歌地图(它没有将结果显示到我的输入 Pane 中以保存到我的数据库中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19487023/

相关文章:

php - 使用 PHP 将 mysql 表导出到 .txt 或 .doc 文件

asp.net - 如何在页面加载之前隐藏 div?

javascript - 使用 JQuery/JS 在 click() 上存储 <td> 的 id

javascript - 返回innerHTML 作为 UL

javascript - 在给定视口(viewport)中使用叠加 div 裁剪站点显示

javascript - 尝试使用 Object.entries<T>(Enum).map() 映射 TS 中的枚举,但仅适用于基于字符串的枚举?

php - 将 IIS 重写转换为 Nginx 重写语法?

php - 在 PHP 中为多个客户端应用一个或多个数据库

javascript - 如何从 HTML 外部拖放 iframe?

javascript - jQuery 多级选择器不起作用,但 .children 起作用