-
Copy/symlink location_picker.py onto your path
-
Copy/symlink the static/location_picker directory into your static directory. If you're not using staticfiles, you'll need to set LOCATION_PICKER_STATIC_URL to wherever you put the location_picker files.
-
Use in your models.py as follows:
from location_picker import LocationField class MyModel(models.Model): location = LocationField()
Last active
September 23, 2015 16:18
-
-
Save gregplaysguitar/581736 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(document).unload(function(){ | |
google.maps.Unload(); | |
}); | |
$(document).ready(function(){ | |
$("input.location_picker").each(function (i) { | |
var me = $(this), | |
mapDiv = $('<div>').insertBefore(me).addClass('location_picker_map'); | |
me.css('display','none'); | |
var lat = -43.531065; | |
var lng = 172.636671; | |
if (me.val().split(/,\s*/).length == 2) { | |
values = this.value.split(','); | |
lat = values[0]; | |
lng = values[1]; | |
} | |
var center = new google.maps.LatLng(lat, lng); | |
var map = new google.maps.Map(mapDiv[0], { | |
zoom: 15, | |
center: center, | |
scaleControl: true, | |
navigationControl: true, | |
navigationControlOptions: { | |
position: google.maps.ControlPosition.RIGHT | |
}, | |
disableDefaultUI: true, | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}); | |
var marker = new google.maps.Marker({ | |
position: center, | |
map: map | |
}); | |
google.maps.event.addListener(map, 'click', function (e) { | |
me.val(e.latLng.lat() + ',' + e.latLng.lng()); | |
marker.setPosition(e.latLng); | |
}); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.location_picker_map { | |
width: 300px; | |
height: 200px; | |
border: 1px solid black; | |
padding: 2px; | |
display: inline-block; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django import forms | |
from django.db import models | |
from django.conf import settings | |
from django.core.exceptions import ValidationError | |
# See readme.markdown | |
STATIC_URL = getattr(settings, 'LOCATION_PICKER_STATIC_URL', '%slocation_picker/' % settings.STATIC_URL) | |
class LocationPickerWidget(forms.TextInput): | |
class Media: | |
css = { | |
'all': ( | |
'%slocation_picker.css' % STATIC_URL, | |
) | |
} | |
js = ( | |
'http://maps.google.com/maps/api/js?sensor=false', | |
'%sjquery.location_picker.js' % STATIC_URL, | |
) | |
def __init__(self, language=None, attrs=None): | |
self.language = language or settings.LANGUAGE_CODE[:2] | |
super(LocationPickerWidget, self).__init__(attrs=attrs) | |
def render(self, name, value, attrs=None): | |
if None == attrs: | |
attrs = {} | |
attrs['class'] = 'location_picker' | |
return super(LocationPickerWidget, self).render(name, value, attrs) | |
class LocationField(models.CharField): | |
def __init__(self, *args, **kwargs): | |
if not 'max_length' in kwargs: | |
kwargs['max_length'] = 255 | |
super(LocationField, self).__init__(*args, **kwargs) | |
def formfield(self, **kwargs): | |
kwargs['widget'] = LocationPickerWidget | |
return super(LocationField, self).formfield(**kwargs) | |
def south_field_triple(self): | |
"Returns a suitable description of this field for South." | |
# We'll just introspect the _actual_ field. | |
from south.modelsinspector import introspector | |
field_class = "django.db.models.fields.CharField" | |
args, kwargs = introspector(self) | |
# That's our definition! | |
return (field_class, args, kwargs) | |
def validate(self, value, obj): | |
super(LocationField, self).validate(value, obj) | |
try: | |
x, y = value.split(',') | |
float(x.strip()), float(y.strip()) | |
except: | |
raise ValidationError('Bad coordinate format - should be ll,la. Example: 43.5343,172.6236') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment