繁体   English   中英

如何对多个字段进行地理编码?

[英]How do I geocode multiple fields?

我正在尝试创建一种将对Google地图进行地理编码的表单。 我有来自map API的基本代码,但它仅适用于一个字段:“地址”。

我使用的是“ getElementByClassName”,而不是使用“ getElementByID”,因此它可能会使用我设置的任何字段中的数据。

就是说,这没有用。 我通过使用类而不是ID朝着正确的方向前进吗?

以下是我正在使用的代码。 省略了API-KEY。

     <!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 100% }
      body { height: 100%; margin: 0; padding: 0 }
      #map-canvas { height: 100% }
    </style>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?key=mykey=false">
    </script>
    <script type="text/javascript">

var geocoder;
var map;
function initialize() {
  geocoder = new google.maps.Geocoder();
  var latlng = new google.maps.LatLng(39.861933, -83.067746);
  var mapOptions = {
    zoom: 3,
    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;
  address += " "+document.getElementById('street_add').value;
  address += " "+document.getElementById('city').value;
  address += " "+document.getElementById('state').value;
  address += " "+document.getElementById('postcode').value;
  address += " "+document.getElementById('country').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>
  </head>
  <body>
    <div id="panel">
      <form action="" name="" method="post">
         <input id="address" type="hidden" value="">
         <input id="street_add" type="textbox" value="" class="address">
         <input id="city" type="textbox" value="" class="address">
         <input id="state" type="textbox" value="" class="address">
         <input id="postcode" type="textbox" value="" class="address">
         <input id="country" type="textbox" value="" class="address">
         <input type="button" value="Geocode" onclick="codeAddress()">
       </form>
    </div>
    <div id="map-canvas" style="height:40%;top:30px;"></div>
  </body>
</html>

地理编码器采用一个表示地址的字符串。 您当前未在创建它,而是向其传递了一个未定义的值(没有getElementByClassName,它是getElementsByClassName并返回HTML元素数组)

将单独的值合并为一个字符串:

var address = document.getElementById('street_add').value;
address += " "+document.getElementById('city').value;
address += " "+document.getElementById('state').value;
address += " "+document.getElementById('postcode').value;
address += " "+document.getElementById('country').value;

工作实例

暂无
暂无

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

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