【发布时间】:2017-05-23 20:21:11
【问题描述】:
- 请帮帮我,这非常重要
我有一个 autocomplete javascript input(使用谷歌地图) 我想将自动完成输入的值传输到 aspx.cs 代码,我想将其传输到名为地址的字符串,请帮助我,我在这个问题上打破了 3 天多的时间。 p>
aspx 页面:
<
div id="locationField">
**<input id="autocomplete"** placeholder="הקלד את הכתובת"
onFocus="geolocate()" type="text" dir="rtl"></input>
</div>
<script>
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 initAutocomplete() {
// Create the autocomplete object, restricting the search to geographical
// location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {!HTMLInputElement} */(document.getElementById('autocomplete')),
{ types: ['geocode'] });
var hdnfldVariable = document.getElementById("<%= hdnfldVariable.ClientID %>");
}
function somefunction() {
//set this if you have dynamic ClientID
var hdnfldVariable = document.getElementById("autocomplete").value.toString();
autocomplete.addListener('place_changed', fillInAddress);
}
function fillInAddress() {
// Get the place details from the autocomplete object.
var place = autocomplete.getPlace();
for (var component in componentForm) {
document.getElementById(component).value = '';
document.getElementById(component).disabled = false;
}
autocomplete.getPlace().address_components
// Get each component of the address from the place details
// and fill the corresponding field on the form.
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;
}
}
}
// Bias the autocomplete object to the user's geographical location,
// as supplied by the browser's 'navigator.geolocation' object.
function geolocate() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var geolocation = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
</script>
<script src="https://maps.googleapis.com/maps/api/js?key---&libraries=places&callback=initAutocomplete"
async defer></script>
<asp:HiddenField ID="hdnfldVariable" runat="server" />
<asp:Button ID="ButtonAddEvent" runat="server" Text="Add"
Font-Bold="True" Font-Names="Gisha" Font-Size="17pt" ForeColor="Blue"
onclick="ButtonAddEvent_Click"
OnClientClick="somefunction();"
style="z-index: 1; left: 497px; top: 774px; position: absolute" />
</asp:Content>
后面的代码(把输入值传到这里):
protected void ButtonAddEvent_Click(object sender, EventArgs e)
{
string Event_Address = hdnfldVariable.ToString();
}
请帮帮我,这是非常重要的项目,非常感谢:)
【问题讨论】:
-
你试过什么?我个人通常使用 AJAX 调用,但您可以使用休息服务等
标签: javascript c# html asp.net google-maps-api-3