我想在我的TextBox
中實現功能,其中用戶將輸入一些地址,並從Google API返回地址,該用戶將選擇正確的地址。他會選擇我的街道,郵編,國家,城市的地址都會自動填寫。如何填寫TextBox返回來自Google地理編碼API
我想這一點,但沒有獲得成功(我的aspx頁面的小部分)
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=places"></script>
<script type="text/javascript">
var placeSearch,autocomplete;
var component_form = {
'route': 'short_name',
'route': 'long_name',
'locality': 'long_name',
'administrative_area_level_1': 'short_name',
'country': 'long_name',
'postal_code': 'postal_code'
};
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 component_form) {
document.getElementById(component).value = "";
document.getElementById(component).disabled = false;
}
for (var j = 0; j < place.address_components.length; j++) {
var att = place.address_components[j].types[0];
if (component_form[att]) {
var val = place.address_components[j][component_form[att]];
document.getElementById(att).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));
});
}
}
</script>
我的.aspx頁面中是這樣
<div onload="initialize();">
<asp:UpdatePanel ID="UpdatePanelTabContainer" runat="server">
<ContentTemplate>
<table width="100%">
<tr>
<td>
street
</td>
<td>
<asp:TextBox ID="txtPickupStreet" runat="server" MaxLength="100" Width="162px" placeholder="Enter your address" AutoPostBack="true" onFocus="javascript:geolocate()"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Postal Code
</td>
<td>
<asp:TextBox ID="txtPickupPC" runat="server" MaxLength="11" Width="90px" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
這裏用戶將輸入街道TextBox
他會得到相對的結果,然後選擇全部TextBox
將被填充。
這樣,**無法**表示您應該接受任何**答案。如果你的問題沒有答案可以幫助你,那麼請不要僅僅因爲它而接受任何答案。 –
好吧,我明白了 –
我會在將來當我發佈我的問題。但問題是,當我發佈一個問題的人投票,並從聲譽減去 –