0

到目前为止,我已经运行了这段代码:https ://google-developers.appspot.com/maps/documentation/javascript/examples/places-autocomplete

我已将自动完成设置为 address1 文本字段。它工作正常。

我需要的是当我选择正确的大道时,邮政编码和城市必须自动填写 acode1 文本字段和 city1 文本字段。

示例:Avenue Joffre, Épinay-sur-Seine, France(当我选择此选项时,我希望仅在 address1 文本字段中显示“Avenue Joffre”部分。然后在城市字段中显示“Epinay-sur-seine”和acode1 文本字段中此地址的邮政编码。

如果可能的话,请帮助我编写代码,或者就如何达到此解决方案给我一些建议。我将尝试自己编写代码并将其发布在这里作为答案。

体内:

 <input name="address1" id="address1" type="text"/>

 <input name="acode1" id="acode1" type="text"/>

 <input name="city1" id="city1" type="text"/>

在 JS 中:

  function initialize() {
    var mapOptions = {
      center: new google.maps.LatLng(-33.8688, 151.2195),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById('map_canvas'),
      mapOptions);


    var input = document.getElementById('address1');


    var autocomplete = new google.maps.places.Autocomplete(input);



    autocomplete.setComponentRestrictions({'country':'fr'});



    var infowindow = new google.maps.InfoWindow();
    var marker = new google.maps.Marker({
      map: map
    });


    google.maps.event.addListener(autocomplete, 'place_changed', function() 
    {
      infowindow.close();
      var place = autocomplete.getPlace();
      if (place.geometry.viewport) {
        map.fitBounds(place.geometry.viewport);
      } else {
        map.setCenter(place.geometry.location);
        map.setZoom(17);  // Why 17? Because it looks good.
      }

      var image = new google.maps.MarkerImage(
          place.icon,
          new google.maps.Size(71, 71),
          new google.maps.Point(0, 0),
          new google.maps.Point(17, 34),
          new google.maps.Size(35, 35));
      marker.setIcon(image);
      marker.setPosition(place.geometry.location);

      var address = '';
      if (place.address_components) {
        address = [
          (place.address_components[0] && place.address_components[0].short_name || ''),
          (place.address_components[1] && place.address_components[1].short_name || ''),
          (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
      }

      infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
      infowindow.open(map, marker);
    });

    // Sets a listener on a radio button to change the filter type on Places
    // Autocomplete.
    function setupClickListener(id, types) {
      var radioButton = document.getElementById(id);
      google.maps.event.addDomListener(radioButton, 'click', function() {
        autocomplete.setTypes(types);
      });
    }

    setupClickListener('changetype-all', []);
    setupClickListener('changetype-establishment', ['establishment']);
    setupClickListener('changetype-geocode', ['geocode']);
  }
  google.maps.event.addDomListener(window, 'load', initialize);

可以在此页面中查看选项:http ://code.google.com/p/geo-autocomplete/

编辑.................................

经过长时间的研究,我现在处于这一点。它正确返回地址,但我很难检索邮政编码。

参考:https ://developers.google.com/maps/documentation/javascript/reference#PlaceResult

              var address = '';
      var code='';

     if(place.address_components){
     code = [place.address_components[0].types[0].postal_code];

          document.getElementById('acode1').value = code;
     }

      if (place.address_components) {  


        address = [
          (place.address_components[0] && place.address_components[0].short_name || ''),
          (place.address_components[1] && place.address_components[1].short_name || ''),
          (place.address_components[2] && place.address_components[2].short_name || '')
        ].join(' ');
      }
4

1 回答 1

1

看到这个类似的问题: Get zipcode with geo-autocomplete in google api v3

这个例子

要仅获取城市,请使用type locality

代码片段:

function initialize() {
  var mapOptions = {
    center: new google.maps.LatLng(-33.8688, 151.2195),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById('map_canvas'),
    mapOptions);

  var input = document.getElementById('searchTextField');
  var autocomplete = new google.maps.places.Autocomplete(input);

  autocomplete.bindTo('bounds', map);

  var infowindow = new google.maps.InfoWindow();
  var marker = new google.maps.Marker({
    map: map
  });

  google.maps.event.addListener(autocomplete, 'place_changed', function() {
    infowindow.close();
    var place = autocomplete.getPlace();
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
      map.setZoom(17); // Why 17? Because it looks good.
    }

    var image = new google.maps.MarkerImage(
      place.icon,
      new google.maps.Size(71, 71),
      new google.maps.Point(0, 0),
      new google.maps.Point(17, 34),
      new google.maps.Size(35, 35));
    marker.setIcon(image);
    marker.setPosition(place.geometry.location);

    var address = '';
    var zip_code = '';
    document.getElementById('zip_code').innerHTML = zip_code;
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
      for (var i = 0; i < place.address_components.length; i++) {
        for (var j = 0; j < place.address_components[i].types.length; j++) {
          if (place.address_components[i].types[j] == "postal_code") {
            zip_code = place.address_components[i].long_name;
            document.getElementById('zip_code').innerHTML = zip_code;
          }
        }
      }
    }

    infowindow.setContent('<div><strong>' + place.name + '</strong><br>' + address);
    infowindow.open(map, marker);
  });

  // Sets a listener on a radio button to change the filter type on Places
  // Autocomplete.
  function setupClickListener(id, types) {
    var radioButton = document.getElementById(id);
    google.maps.event.addDomListener(radioButton, 'click', function() {
      autocomplete.setTypes(types);
    });
  }

  setupClickListener('changetype-all', []);
  setupClickListener('changetype-establishment', ['establishment']);
  setupClickListener('changetype-geocode', ['geocode']);
}
google.maps.event.addDomListener(window, 'load', initialize);
body {
  font-family: sans-serif;
  font-size: 14px;
}

#map_canvas {
  height: 400px;
  width: 600px;
  margin-top: 0.6em;
}
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places"></script>
<div>
  <input id="searchTextField" type="text" size="50">
  <input type="radio" name="type" id="changetype-all" checked="checked">
  <label for="changetype-all">All</label>

  <input type="radio" name="type" id="changetype-establishment">
  <label for="changetype-establishment">Establishments</label>

  <input type="radio" name="type" id="changetype-geocode">
  <label for="changetype-geocode">Geocodes</lable>
    </div>
<div id="zip_code"></div>
    <div id="map_canvas"></div>

于 2012-09-05T10:56:25.357 回答