【问题标题】:Google Maps API get location and id using JavaScriptGoogle Maps API 使用 JavaScript 获取位置和 ID
【发布时间】:2015-07-22 09:16:58
【问题描述】:

您好,我从 Google 地图开发者网站获得此代码。在此代码中,它将使用 Google API 自动填写地址并完成地址表单。现在我想从地址中获取 'location' 值和 'placeId'。我怎么才能得到它?

  <head>
   <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
    <script>
var placeSearch, autocomplete;
var componentForm = {
  street_number: 'short_name',
  route: 'long_name',
  locality: 'long_name',
  administrative_area_level_1: 'short_name',
  country: 'long_name',
  postal_code: 'short_name'
};
function initialize() {
autocomplete = new google.maps.places.Autocomplete(
      (document.getElementById('autocomplete')),
      { types: ['geocode'] });
  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    fillInAddress();
  });
}
function fillInAddress() {
  var place = autocomplete.getPlace();
 for (var component in componentForm) {
    document.getElementById(component).value = '';
    document.getElementById(component).disabled = false;
  }
for (var i = 0; i < place.address_components.length; i++) {
    var addressType = place.address_components[i].types[0];
    if (componentForm[addressType]) {
      var val = place.address_components[i][componentForm[addressType]];
      document.getElementById(addressType).value = val;
    } } }
function geolocate() {
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var geolocation = new google.maps.LatLng(
          position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
        center: geolocation,
        radius: position.coords.accuracy
      });
      autocomplete.setBounds(circle.getBounds());
    });
  }
}
    </script>


  </head>

  <body onload="initialize()">
    <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address"
             onFocus="geolocate()" type="text"></input>
    </div>

    <table id="address">
      <tr>
        <td class="label">Street address</td>
        <td class="slimField"><input class="field" id="street_number"
              disabled="true"></input></td>
       .......
        <td class="label">Country</td>
        <td class="wideField" colspan="3"><input class="field"
              id="country" disabled="true"></input></td>
      </tr>
    </table>
  </body>
</html>

【问题讨论】:

  • 拼写和语法。

标签: javascript php html google-maps


【解决方案1】:

查看第 27 行。

同时查看第 33 和 42 行: if(document.getElementById(component)) ... 否则,如果缺少组件,脚本将抛出错误(您没有发布全部内容;我认为您拥有所有组件,但这仍然是个好主意)。

<html>
  <head>
   <link type="text/css" rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500">
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true&libraries=places"></script>
    <script>
      var placeSearch, autocomplete;
      var componentForm = {
        street_number: 'short_name',
        route: 'long_name',
        locality: 'long_name',
        administrative_area_level_1: 'short_name',
        country: 'long_name',
        postal_code: 'short_name'
      };
      function initialize() {
        autocomplete = new google.maps.places.Autocomplete(
          document.getElementById('autocomplete'),
          { types: ['geocode']}
        );
        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          fillInAddress();
        });
      }
      function fillInAddress() {
        var place = autocomplete.getPlace();

        // location & place_id
        var location = place.geometry.location;
        var place_id = place.place_id;
        document.getElementById('log').innerHTML += 'location: ' + location.lat() + ',' + location.lng() + ' - place ID: ' + place_id + '<br>';

        for (var component in componentForm) {
          if(document.getElementById(component)) {
            document.getElementById(component).value = '';
            document.getElementById(component).disabled = false;
          }
        }
        for (var i = 0; i < place.address_components.length; i++) {
          var addressType = place.address_components[i].types[0];
          if (componentForm[addressType]) {
            var val = place.address_components[i][componentForm[addressType]];
            if(document.getElementById(addressType)) {
              document.getElementById(addressType).value = val;
            }
          }
        }
      }
      function geolocate() {
        if (navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var geolocation = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var circle = new google.maps.Circle({
              center: geolocation,
              radius: position.coords.accuracy
            });
            autocomplete.setBounds(circle.getBounds());
          });
        }
      }
    </script>
  </head>
  <body onload="initialize()">
    <div id="locationField">
      <input id="autocomplete" placeholder="Enter your address" onFocus="geolocate()" type="text"></input>
    </div>
    <table id="address">
      <tr>
        <td class="label">Street address</td>
        <td class="slimField"><input class="field" id="street_number" disabled="true"></input></td>
        <td class="label">Country</td>
        <td class="wideField" colspan="3"><input class="field" id="country" disabled="true"></input></td>
      </tr>
    </table>
    <div id="log"></div>
  </body>
</html>

【讨论】:

  • 我如何从这个编码构建自动填充表单?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-19
  • 2020-12-21
  • 2015-06-10
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多