我试图将经度和经度从javascript添加到隐藏的表单值,所以当我提交它反向goeocdes你的地址,但我不知道如何实现这一点。有人可以帮忙吗?
<p>Click the button to get your coordinates. Copy paste them in the fields to get your address</p>
</address></p>
<button onclick="logLocation()">Try It</button>
<script>
var successCallback = function(data) {
document.getElementById('de').innerHTML=('latitude: ' + data.coords.latitude + ' longitude: ' + data.coords.longitude);
var lat = data.coords.latitude;
var long = data.coords.longitude;
var lat= document.getElementById("demo").value
var long = document.getElementById("lat").value
};
var failureCallback = function() {
console.log('location failure :(');
};
var logLocation = function() {
//determine if the handset has client side geo location capabilities
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(successCallback, failureCallback);
}
else{
alert("Functionality not available");
}
};
</script>
<p id="demo"><p/>
<h4>Enter either address or coordinates</h4>
<%= form_for @place do |f| %>
<small class="text-muted">You can also enter IP. Your IP is <%= request.ip %></small>
<%= hidden_field_tag 'latitude', {:id =>'lat', value = ''}%>
<%= hidden_field_tag 'longitude', {:id =>'demo', value = ''}%>
<%= f.submit 'Add!', class: 'btn btn-primary' %>
<% end %>
<div class="card">
<div class="card-block">
<div id="map"></div>
</div>
</div>
答案 0 :(得分:0)
非常确定您需要将这些字段的value
设置为您的纬度/经度
var successCallback = function(data) {
document.getElementById('de').innerHTML=('latitude: ' +
data.coords.latitude + ' longitude: ' + data.coords.longitude);
var lat = data.coords.latitude;
var long = data.coords.longitude;
document.getElementById("demo").value = long;
document.getElementById("lat").value = lat;
};