到目前为止,我已经运行了这段代码: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(' ');
}