D
D
Dennis2016-07-12 16:45:54
Django
Dennis, 2016-07-12 16:45:54

Google Maps GeoDjango - No geometry value provided?

models.py:

from django.contrib.gis.db import models
class Location(models.Model):
    name = models.CharField(max_length=100)
    coords = models.PointField(null=True)

forms.py:
from django.contrib.gis import forms
class LocationForm(forms.ModelForm):
    class Meta:
        model = Location
        fields = ['coords', 'name']

In add_location.html, the code for the 'coords' model field was automatically generated:
<div id="div_id_coords" class="form-group">
    <div class="controls ">
        <div id="id_coords_div_map">

            <div id="id_coords_map"></div>

            <span class="clear_features">
                <a href="javascript:geodjango_coords.clearFeatures()">Delete</a>
            </span>
            <textarea id="id_coords" class="vSerializedField required" cols="150" rows="10" name="coords"></textarea>

<script type="text/javascript">
                    var map_options = {};
                    var options = {
                        geom_name: 'Point',
                        id: 'id_coords',
                        map_id: 'id_coords_map',
                        map_options: map_options,
                        map_srid: 4326,
                        name: 'coords'
                    };

                    var geodjango_coords = new MapWidget(options);
            </script>

</div>
    </div>
</div>

In HEAD, I will link a Google map:
<script src="https://maps.googleapis.com/maps/api/js?key=MY-KEY"></script>
{{ form.media }}

In js I define map and marker:
var map, marker, mapOptions = {...};
map = new google.maps.Map($('#id_coords_map')[0], mapOptions);

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

function placeMarker(latLng, map) {
    marker = new google.maps.Marker({...});
    return marker
}

When I submit the form, I see a validation error on the COORDS field - "No geometry value provided"
and in the wrapper - Error creating geometry from value '' (String or unicode input unrecognized as WKT EWKT, and HEXEWKB.)
That is in TEXTAREA id=' id_coords' is passed an empty string instead of coordinates.
I think this is because OpenLayers remains a widget, not a GMap (Openlayers comes by default, i.e. on the screen I see Google Map, but OpenLayers is under it).
But I don't understand what I have to do to make GMap a widget so that I can save the location coordinates when submitting the form.
Thanks for the help!!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Kazantsev, 2016-07-26
@saintbyte

You screw up your widget, but only for good, you need to send a string like POINT(lon,lat) to the server. And take it and see how it is in the built-in widget - there should be a hidden field

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question