提交地理编码API表单
Geocoding API form submit
所以我有一个表单,用户完成,我试图提交与表单的地理编码对象,所以谷歌地图API可以绘制它。
HTML:<form name="add" onsubmit="submits()" action="index.php" method="post">
Resource Name: <textarea name="name" type="text"></textarea><br><br>
Resource Type: <textarea name="type" type="text" ></textarea><br><br>
Street Number: <input type="number" id = "streetnum" name="streetnum"><br>
Street Name: <input type="text" id="streetnam" name="streetnam"><br>
City: <input type="text" id="city" name="city">
State: <input type="text" id="state" name= "state"> <br><br>
Resource Start Date: <input type="date" name="start" id="start"/> <br>
Resource End Date: <input type="date" name="end" id="end">
<input type="text" name="addressobject" id="addressobject" style="display:none" >
<input type= "checkbox" name="annual" id= "annual" value="annual">Annual<br><br>
<div id="new">
</div>
Details: <textarea name="details" rows=5 type="text" ></textarea>
<input name="submit" type="submit" value="Create Event"/>
<button type="button" onclick="addNew()">Add more date fields</button>
<button type="button" onclick="deleteAtt()">Delete a date field</button>
</form>
Javscript:
<script>
function submits(){
var address= document.getElementById("streetnum").value + " " + document.getElementById("streetnam").value+ ", " + document.getElementById("city").value+", " + document.getElementById("state").value;
var JSONobject;
var geocoder = new google.maps.Geocoder();
if(geocoder){
geocoder.geocode({'address':address}, function(results,status){
if(status==google.maps.GeocoderStatus.OK){
if(status!=google.maps.GeocoderStatus.ZERO_RESULTS){
JSONobject = window.JSON.stringify(results);
document.getElementById("addressobject").value = JSONobject;
}else{
alert("No results found");
}
}else{
alert("Geocode was not successful for the following reason: " +status);
}
});
}
}
</script>
出于某种原因,当我从控制台调用submits()
时,它工作,但当我实际使用按钮时,我得到错误:
GEOCODE不成功,原因如下:
-
JSON.stringify(results)的结果将不能在不同版本的API之间移植。google的"内部属性"名称("k","j","Aa","ra"等)。map对象可以并且确实会随着API的新版本而改变:
"[{"address_components":[{"long_name":"532","short_name":"532","types":["street_number"]},{"long_name":"Beacon Street","short_name":"Beacon St","types":["route"]},{"long_name":"Back Bay West","short_name":"Back Bay West","types":["neighborhood","political"]},{"long_name":"Boston","short_name":"Boston","types":["locality","political"]},{"long_name":"Suffolk County","short_name":"Suffolk County","types":["administrative_area_level_2","political"]},{"long_name":"Massachusetts","short_name":"MA","types":["administrative_area_level_1","political"]},{"long_name":"United States","short_name":"US","types":["country","political"]},{"long_name":"02215","short_name":"02215","types":["postal_code"]}],"formatted_address":"532 Beacon Street, Boston, MA 02215, USA","geometry":{"location":{"k":42.3505906,"A":-71.0911074},"location_type":"ROOFTOP","viewport":{"Aa":{"k":42.3492416197085,"j":42.3519395802915},"ra":{"j":-71.09245638029154,"k":-71.08975841970846}}},"partial_match":true,"types":["street_address"]}]"
-
地理编码器是异步的,地理编码操作的结果不会立即返回,您不想在结果从Google服务器返回之前提交表单(在回调例程中)。像这样:
function submits(){ var address= document.getElementById("streetnum").value + " " + document.getElementById("streetnam").value+ ", " + document.getElementById("city").value+", " + document.getElementById("state").value; var JSONobject; var geocoder = new google.maps.Geocoder(); if(geocoder){ geocoder.geocode({'address':address}, function(results,status){ if(status==google.maps.GeocoderStatus.OK){ if(status!=google.maps.GeocoderStatus.ZERO_RESULTS){ JSONobject = window.JSON.stringify(results); document.getElementById("addressobject").value = JSONobject; // submit the form now that all the data is available. document.getElementById("add").submit(); }else{ alert("No results found"); } }else{ alert("Geocode was not successful for the following reason: " +status); } }); } return false; }
相关文章:
- 使用gmail-api发送电子邮件错误:此api不支持解析表单编码的输入
- REST API 服务器是否应该使用 PUT 接收 JSON 数据或普通表单数据
- 如何使用JQueryUI滑块控制web音频API振荡器参数,而无需经过HTML表单参数
- 在用户提交表单后显示数据(从API调用检索),而不刷新页面
- 阻止Drupal's在表单API中单击按钮后刷新的页面
- 将表单的内容发布到 rest api
- 尝试将 Dropbox 文件上传 API 链接到基本的 HTML 表单文件输入
- 我们是否可以访问 UA 应该为 HTML5 表单 API 维护的无效控件列表
- OWIN和表单认证的WEB API 2与SPA
- Drupal 7 Field API:如何以编程方式发送表单元素属性 #ajax 指定的AJAX请求
- 带有提交表单的 Souncloud API
- 提交与 API 交互的表单问题
- 谷歌地方API从自动完成表单中获取输入文本
- Hello.js-Flickr API验证不显示登录表单,而是关闭它
- Youtube播放器api如何将getCurrentTime()绑定到表单输入
- 通过HTML表单和API自定义图像过滤器
- “ 谷歌 API 消息 ” : “ 此 API 不支持解析表单编码的输入.".
- 使用restangular将表单数据从AngularJs前端发送到NodeJS RESTful API
- Javascript query . setquery()使用Google可视化API对Google表单给出'请
- 提交地理编码API表单