【发布时间】:2014-04-27 02:25:57
【问题描述】:
我需要生成 n 个地址抓取器并获取 json 格式的输出,我可以通过 ajax 单击按钮来完成。但我不知道如何使用 google api 动态生成自动完成并单独获取内容。
我需要展示与此小提琴中所附的完全相同类型的设计。
initialize();
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(
/** @type {HTMLInputElement} */(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);
autocomplete.setBounds(new google.maps.LatLngBounds(geolocation,
geolocation));
});
}
}
目前我已经添加了 3 个表单,但我需要通过“添加表单”按钮添加它,我想我可以使用 jquery append 来完成。但是如何让它在谷歌自动完成中工作呢?
谢谢
【问题讨论】:
标签: javascript jquery html google-maps autocomplete