Reputation: 5
I use rails 5.2 and mapbox for geocoding
I try to pass latitude and longitude from js but nothing works
I can't understand what is the reason why it does not work
my code:
<%= form_for(@place, :remote => true, html: { role: 'form', multiple: true }) do |f| %>
<%= f.hidden_field :longitude %>
<%= f.hidden_field :latitude %>
<div class="margin-top-40">
<div id='map' style='height: 300px;'></div>
<script>
mapboxgl.accessToken = 'accessToken';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v11'
});
var lng;
var lat;
var lngDisplay = document.getElementById('place_longitude');
var latDisplay = document.getElementById('place_latitude');
var marker = new mapboxgl.Marker({
'color': '#314ccd'
});
map.on('click', function(e) {
marker.setLngLat(e.lngLat).addTo(map);
lng = e.lngLat.lng;
lat = e.lngLat.lat;
lngDisplay.textContent = lng;
latDisplay.textContent = lat;
});
</script>
</div>
<div class="form-group">
<%= f.submit 'Save', data: {disable_with: 'Save...'}, class: 'btn btn-s'%>
<%= link_to 'Back', :back, class: 'btn btn-d' %>
</div>
<% end %>
Upvotes: 0
Views: 398
Reputation: 5
Found what was the problem
lngDisplay.textContent = lng;
latDisplay.textContent = lat;
Need to change to:
lngDisplay.value = lng;
latDisplay.value = lat;
Upvotes: 1