我有这个表格,我想用地址,用户的地理位置自动填写地址,城市,州和邮政编码输入字段,我是javascript区域的菜鸟,我真的需要帮助。
这是我的代码,我需要帮助创建填充字段的javascript。
<form name="catcustomcontentform11679" onsubmit="return checkWholeForm11679(this)" enctype="multipart/form-data" method="post" action="/CustomContentProcess.aspx?CCID=24718&OID={module_oid}&OTYPE={module_otype}">
<table class="webforms" cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr>
<td><label for="ItemName">Item Name</label><br />
<input class="cat_textbox_small" type="text" name="ItemName" id="ItemName" maxlength="255" /> •</td>
</tr>
<tr>
<td><label for="ItemDescription">Item Description</label><br />
<textarea name="ItemDescription" id="ItemDescription" cols="10" rows="4" class="cat_listbox"></textarea></td>
</tr>
<tr>
<td><label for="ItemAddress">Address</label><br />
<input type="text" name="ItemAddress" id="ItemAddress" class="cat_textbox" maxlength="500" /></td>
</tr>
<tr>
<td><label for="ItemCity">City</label><br />
<input type="text" name="ItemCity" id="ItemCity" class="cat_textbox" maxlength="255" /></td>
</tr>
<tr>
<td><label for="ItemState">State</label><br />
<input type="text" name="ItemState" id="ItemState" class="cat_textbox" maxlength="255" /></td>
</tr>
<tr>
<td><label for="ItemZip">Zipcode/Postcode</label><br />
<input type="text" name="ItemZip" id="ItemZip" class="cat_textbox" maxlength="255" /></td>
</tr>
<tr>
<td><label for="ItemCountry">Country</label><br />
<select name="ItemCountry" id="ItemCountry" class="cat_dropdown">
<option value=" ">-- Select Country --</option>
<option value="MX" selected="selected">MEXICO</option>
</select></td>
</tr>
<tr>
<td><label for="CAT_Custom_1">Last Name</label><br />
<input type="text" maxlength="4000" name="CAT_Custom_1" id="CAT_Custom_1" class="cat_textbox" /></td>
</tr>
<tr>
<td><label for="CAT_Custom_2">Email Address</label><br />
<input type="text" maxlength="4000" name="CAT_Custom_2" id="CAT_Custom_2" class="cat_textbox" /></td>
</tr>
<tr>
<td><input class="cat_button" type="submit" value="Submit" id="catcustomcontentbutton" /></td>
</tr>
</tbody>
</table>
</form>
<script type="text/javascript" src="/CatalystScripts/ValidationFunctions.js?vs=b1566.r451189-phase1"></script>
//<![CDATA[
var submitcount11679 = 0;
function checkWholeForm11679(theForm){
var why = "";
if (theForm.ItemName) why += isEmpty(theForm.ItemName.value, "Item Name");
if (why != ""){alert(why);return false;}
if(submitcount11679 == 0){
submitcount11679++;theForm.submit();return false;
}else{
alert("Form submission is in progress.");return false;
}
}
//]]>
答案 0 :(得分:0)
您可以尝试这样做:(您可能希望为用户提供输入该地址的选项。对于这样的情况,例如,订购时不在家,有朋友家,工作等等。不想用实际的纬度和经度填写那里的地址,而是像这个例子所显示的那样帮助缩小范围。
http://jsfiddle.net/bobrierton/vbc1a5uo/
var placeSearch, autocomplete;
var componentForm = {
route: 'long_name',
locality: 'long_name',
administrative_area_level_1: 'short_name',
postal_code: 'short_name'
};
function initialize() {
// Create the autocomplete object, restricting the search
// to geographical location types.
autocomplete = new google.maps.places.Autocomplete(
/** @type {HTMLInputElement} */
(document.getElementById('autocomplete')), {
types: ['geocode']
});
// When the user selects an address from the dropdown,
// populate the address fields in the form.
google.maps.event.addListener(autocomplete, 'place_changed', function() {
fillInAddress();
});
}
// [START region_fillform]
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;
}
// 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;
}
}
//var keys=[];for (var key in place.address_components[0]) keys.push(key);
//alert(keys):
document.getElementById('autocomplete').value =
place.address_components[0]['long_name'] + ' ' +
place.address_components[1]['long_name'];
/*document.getElementById('route').value = (document.getElementById('chbSame').checked ? document.getElementById('autocomplete').value : '');*/
document.getElementById('route').value = '';
}
// [END region_fillform]
// [START region_geolocation]
// 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 = new google.maps.LatLng(
position.coords.latitude, position.coords.longitude);
var circle = new google.maps.Circle({
center: geolocation,
radius: position.coords.accuracy
});
autocomplete.setBounds(circle.getBounds());
});
}
}
// [END region_geolocation]
initialize();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
<tbody>
<tr>
<td><label for="ItemAddress">Address</label>
<input type="text" name="ItemAddress" id="autocomplete" class="cat_textbox" maxlength="500" onFocus="geolocate()" /><br></td>
</tr>
<tr>
<td><label for="ItemAddress2">Address2</label>
<input type="text" name="ItemAddress2" id="route" class="cat_textbox" maxlength="500" /><br></td>
</tr>
<tr>
<td><label for="ItemCity">City</label>
<input type="text" name="ItemCity" id="locality" class="cat_textbox" maxlength="255" /><br></td>
</tr>
<tr>
<td><label for="ItemState">State</label>
<input type="text" name="ItemState" id="administrative_area_level_1" class="cat_textbox" maxlength="255" /><br></td>
</tr>
<tr>
<td><label for="ItemZip">Zipcode/Postcode</label>
<input type="text" name="ItemZip" id="postal_code" class="cat_textbox" maxlength="255" /></td>
</tr>
</tbody>
答案 1 :(得分:0)
您可能会发现,根据用户提供的纬度/经度稍微命中或遗漏,为用户填充地址行。
在具有GPS装置的设备上,位置应该非常准确,但有时可能会错过建筑物编号。没有GPS的设备有时会给出一个大概的位置,有时可能相当远。
这可能会给您的用户带来一些摩擦,必须编辑或删除您预先填充的文字。
另外还要考虑当有些人使用他们的移动设备时,他们可能会远离家乡,因此它会使用当前位置的信息填充字段,而不是他们想要使用的地址。
如果您想加快输入地址的速度,使用邮政编码来解决查询服务可能是更好的选择。