Answer the question
In order to leave comments, you need to log in
How to bind GoogleMaps to GeoDjango?
models.py:
class Location(models.Model):
name = models.CharField(max_length=100)
coords = gis_models.PointField(srid=4326, blank=True, null=True)
class LocationForm(gis_forms.ModelForm):
class Meta:
model = Location
fields = ['coords', 'name']
<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>
$(document).ready(function(){
var mapOptions = {...};
map = new google.maps.Map($('#id_coords_map')[0], mapOptions);
map.addListener('click', function(e){
placeMarker(e.latLng, map);
var position = marker.getPosition();
$('#id_coords').val(position.lng(), position.lat());
});
var marker;
function placeMarker(latLng, map) {
marker = new google.maps.Marker({...});
return marker
}
});
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/common.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/map.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/util.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/onion.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/stats.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/controls.js"></script>
<script type="text/javascript" charset="UTF-8" src="https://maps.googleapis.com/maps-api-v3/api/js/25/5/intl/ru_ALL/marker.js"></script>
<script async="" src="//kitbit.net/kb.js" type="text/javascript"></script>
<script async="" src="//t.insigit.com/assets/dct.js" type="text/javascript"></script>
<script async="" src="//kitbit.net/s.js?u=http%3A%2F%2F127.0.0.1%3A8000%2Fadd_location%2F" type="text/javascript"></script>
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question