1

我有一个包含客户姓名和地址的数据库,我正在尝试根据他们的地址显示地图。我查看了 google api 示例并决定使用地理编码服务 api。

正如您在下面的代码中看到的那样,有一个表单,而不是表单,我想让它在加载时自动更新,因为我将使用数据库中的数据更改值。

表格

<div id="panel">
          <input id="address" type="textbox" value="littleport">
          <input type="button" value="update" onclick="codeAddress()">
        </div>

编码

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Geocoding service</title>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(-34.397, 150.644);
  var mapOptions = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
}

function codeAddress() {
  var address = document.getElementById('address').value;
  geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
      });
    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

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

    </script>
    <style>


#map-canvas, #map_canvas {
  height: 250px;
  width: 250px;
}

@media print {
  html, body {
    height: auto;
  }

  #map-canvas, #map_canvas {
    height: 650px;
  }
}

#panel {
  position: absolute;
  top: 5px;
  left: 50%;
  margin-left: -180px;
  z-index: 5;
  background-color: #fff;
  padding: 5px;
  border: 1px solid #999;
}</style>
  </head>
  <body>
    <div id="panel">
      <input id="address" type="textbox" value="littleport">
      <input type="button" value="update" onclick="codeAddress()">
    </div>
    <div id="map-canvas"></div>
  </body>
</html>

如何才能做到这一点?

这是表格

<div id="panel">
          <input id="address" type="textbox" value="<?php echo$address; ?>">
          <input type="button" value="update" onclick="codeAddress()">
        </div>
4

0 回答 0