使用google map api v3自动填充字段(france- avenue,zipcode,city)

时间:2012-09-05 08:20:14

标签: javascript google-maps-api-3

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

我已将自动填充设置为address1文本字段。工作正常。

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

示例:Avenue Joffre,Épinay-sur-Seine,法国(当我选择这个时,我只希望'Avenue Joffre'部分出现在地址1文本字段中。然后'Epinay-sur-seine'在这个城市字段和acode1文本字段中此地址的zipcode。

如果可能,请帮我解释一下代码,或者就如何达成此解决方案给我一些建议。我会尝试自己编写代码并将其作为答案发布在此处。

身体:

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

1 个答案:

答案 0 :(得分:2)