D
D
Dennis2016-06-13 17:38:34
Django
Dennis, 2016-06-13 17:38:34

How to save marker coordinates from GoogleMap to GeoDjango model?

models.py:

class Location(models.Model):
    name = models.CharField(max_length=100, verbose_name=u"Локация", default=u'')
    coords = gis_models.PointField(u"долгота/широта", srid=4326, blank=True, null=True)

forms.py:
class LocationForm(forms.ModelForm):
    class Meta:
        model = Location
        fields = ['name']

views.py:
class AddLocationPageView(CreateView):
    model = Location
    form_class = LocationForm
    template_name = 'add_location.html'

add_location.html:
<form action="" method="POST">{% csrf_token %}

    <div id="map-add-location" ></div>

    <div class="col-md-5">
         {{ form }}
         <button type="submit">Add location</button>
    </div>
</form>

js:
$(document).ready(function(){
    map = new google.maps.Map($('#map-add-location')[0], mapOptions);

    map.addListener('click', function(e){
            placeMarker(e.latLng, map);
            var position = marker.getPosition();
    });

    var marker;
    function placeMarker(latLng, map) {
        marker = new google.maps.Marker({
            position: latLng,
            map: map,
        });
        return marker
    }
});
</script>

How to store marker coordinates (var position in js) in Location.coords on form save?
Thanks!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-06-14
@sim3x

Apparently there is no need to mess around with js
stackoverflow.com/questions/27306180/how-to-use-ge...
https://docs.djangoproject.com/en/dev/ref/contrib/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question