我有一个表格,可以捕获您的坐标并发送到服务器以获取邮政编码和退货。
单击获取位置后,您会看到一个弹出字段,我希望结果转到表单字段。
JAVASCRIPT
function showLocation(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
$.getJSON('http://www.uk-postcodes.com/latlng/' + position.coords.latitude + ',' + position.coords.longitude + '.json?callback=?', null, gotpostcode);
}
function errorHandler(err) {
if(err.code == 1) {
alert("Error: Access is denied!");
}else if( err.code == 2) {
alert("Error: Position is unavailable!");
}
}
function gotpostcode(result)
{
var postcode = result.postcode;
$("#postcodegoeshere").val(postcode);
}
function getLocation(){
if(navigator.geolocation){
// timeout at 60000 milliseconds (60 seconds)
var options = {timeout:60000};
navigator.geolocation.getCurrentPosition(showLocation,
errorHandler,
options);
}else{
alert("Sorry, browser does not support geolocation!");
}
}
HTML
<body>
<form>
<input type="button" onclick="getLocation();"
value="Get Location"/>
</form>
<div>
<input id='postcodegoeshere' name='xxx' type='text' value='' />
</div>
</body>