【发布时间】:2013-01-15 13:15:01
【问题描述】:
我想在我的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>
在这里,用户将输入 street TextBox 他将获得相对结果并选择之后所有 TextBox 将被填充。
【问题讨论】:
-
这绝不意味着你应该接受任何的答案。如果没有对您的问题有帮助的答案,请不要仅仅为了它而接受任何答案。
-
我会在以后发布我的问题时这样做。但问题是,当我发布一个问题时,人们会投票给那个和那个减去声誉
-
投反对票的人应该被投反对票。好问题。很好的尝试自己解决。
标签: c# asp.net google-maps