0

Hello guys i am creating a form that works really well but when i want to set the localisation of the place but now i have to set it manually (with latitude and logitude), so i create a map with dragable marker and when i drag the marker the value lat and lng changes (in the console) but now i want to set it in the form (because the values are changing but not save) and i would like to change vlaues of latitude and longitude when i change the position of mark (so i want to set latitude with the value lat and longitude with the value lng)

My form html (in twig with symfony) :

{% extends 'base.html.twig' %}

{% block body %}
<!-- Left and right buttons -->
<div class="col-8 mx-auto">
    <br/><br/><br/><br/>
    <div class="card">
        <div class="card-header header-elements-inline">
            <h6 class="card-title">Create a place</h6>
        </div>
        <br/>
        <div class="card-body">
            {{ form_start(form) }}
            <div class="form-group">
                <label for="form_username">Content :</label>
                {{ form_widget(form.content) }}
            </div>
            <div class="form-group">
                <label for="form_username">Title:</label>
                {{ form_widget(form.title) }}
            </div>
            <div class="form-group">
                <label for="form_username">Theme:</label>
                {{ form_widget(form.theme) }}
            </div>
            <div class="form-group">
                <label for="form_username">Max users:</label>
                {{ form_widget(form.maxUser) }}
            </div>
            <div class="form-group">
                <label for="form_username">timeLenght:</label>
                {{ form_widget(form.timeLength) }}
            </div>
            <div class="form-group">
                <label for="form_username">Longitude:</label>
                {{ form_widget(form.longitude) }}
            </div>
            <div class="form-group">
                <label for="form_username">Latitude:</label>
                {{ form_widget(form.latitude) }}
            </div>

            <div class="card px-3">
                <br/>
                <!-- Basic map -->
                <div class="map-container" id="map_marker_simple"></div>
                    {% block javascripts_map %}
                    {% endblock %}
                <br/>
                <!-- /basic map -->
            </div>
            <!-- /custom handle -->
        </div>


            <div class="d-flex justify-content-end align-items-center">
                    <button type="submit" class="btn bg-blue" id="create">Submit<i class="icon-paperplane ml-2"></i></button>
                </div>
            {{ form_end(form) }}
        </div>
    </div>
</div>
<!-- /left and right buttons -->
{% endblock %}

and my part js of the form :

{% extends 'admin/user/new_place.html.twig' %}
{% block javascripts_map %}
<script type="text/javascript">
/* -------------------------------------------------------------------
-----------
*
*  # Basic markers
*
*  Specific JS code additions for maps_google_markers.html page
*
* ---------------------------------------------------------------------------- */


// Setup module
// ------------------------------

var GoogleMapMarkerBasic = function() {


//
// Setup module components
//

// Line chart
    var _googleMapMarkerBasic = function() {
        if (typeof google == 'undefined') {
            console.warn('Warning - Google Maps library is not 
loaded.');
            return;
        }

// Setup map
        function initialize() {

// Define map element
            var map_marker_simple_element = 
document.getElementById('map_marker_simple');

// Set coordinates
            var myLatlng = new google.maps.LatLng(('{{ place.latitude 
}}'), ('{{ place.longitude }}'));

// Options
            var mapOptions = {
                zoom: 10,
                center: myLatlng
            };

// Apply options
            var map = new google.maps.Map(map_marker_simple_element, mapOptions);
            var contentString = 'Marker';

// Add info window
            var infowindow = new google.maps.InfoWindow({
                content: contentString
            });

// Add marker
            var marker = new google.maps.Marker({
                position: myLatlng,
                map: map,
                draggable: true,
                title: ('{{ place.title }}'),
                animation: google.maps.Animation.DROP
            });

            marker.addListener('drag', function() {
                infowindow.open(map,marker);
                var lat = marker.getPosition().lat();
                var lng = marker.getPosition().lng();
                console.log(lat, lng)

            });
// Attach click event
        };

// Initialize map on window load
        google.maps.event.addDomListener(window, 'load', initialize);
    };


    return {
        init: function() {
            _googleMapMarkerBasic();
        }
    }
}();


// Initialize module
// ------------------------------

document.addEventListener('DOMContentLoaded', function() {
    GoogleMapMarkerBasic.init();
});
</script>
{% endblock %}

This is the view of the form :

enter image description here

Thx for all that will try to answer :)

2
  • 1
    I personnaly use Vue.js for two way binding between symfony and javascript, I can make an answer if you are interested by not use only vanilla javascript Commented Aug 2, 2018 at 9:13
  • I don't know anything with vue but i will try it :) Commented Aug 2, 2018 at 9:16

1 Answer 1

2

All you have to do is to change the latitude and longitude input value through javascript.

Vanilla javascript:

document.getElementById("your_latitude_field").value = marker.getPosition().lat();
document.getElementById("your_longitude_field").value = marker.getPosition().lng();

JQuery:

$("#your_latitude_field").val(marker.getPosition().lat());
$("#your_longitude_field").val(marker.getPosition().lng());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.