drx3157 2013-10-21 05:44
浏览 53
已采纳

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

The problem is when i drag the marker it will update the result into the div but i dont know why it is not updating and showing into my input at the end of my program.any solution?

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

Update current position info.

   updateMarkerPosition(latLng);
   geocodePosition(latLng);

Add dragging event listeners.

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

Onload handler to fire off the app.

 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>
  • 写回答

1条回答 默认 最新

  • douqie6454 2013-10-21 05:59
    关注

    Having multiple elements with the same id value is invalid HTML. Change your script:

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

    And your element(s):

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

    Same goes for all other elements which share the same id.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C语言设计一个简单的自动换档程序
  • ¥15 关于logstash转发日志时发生的部分内容丢失问题
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。