From 05b7a2b969256da7d831a3947bf771692e0d39ea Mon Sep 17 00:00:00 2001 From: Martin Bauer Date: Fri, 27 Sep 2013 11:39:28 +0200 Subject: [PATCH] Improved location widget --- blechreiz/database.sqlite | Bin 50176 -> 50176 bytes eventplanner/models.py | 2 +- location_field/forms.py | 8 +- location_field/media/form.js | 114 +++++++++---------- location_field/models.py | 10 +- location_field/views.py | 1 - location_field/widgets.py | 31 +++-- website/templates/website/concert_route.html | 2 +- 8 files changed, 74 insertions(+), 94 deletions(-) delete mode 100755 location_field/views.py diff --git a/blechreiz/database.sqlite b/blechreiz/database.sqlite index b45dbe788ab064c33c0c22ca68adf4e4c1799848..078a0808f9d1857da5b09f6329a63240776791f7 100644 GIT binary patch delta 917 zcmb7D%}*0S6rY_}2rblDizh&9B;g>`*^k-n_Es@LsJp?)M_UN7AKUKsW7}*OTGoSr zi6QZz(!YR{o-o)1&*H_CCyrjcc+eXcE*u=V7-I-=PV;;7-kaa=_hvS>VPhL!4{`RJ z;YrTkf3hp~BZM&CLwFCCyP<&)#$2LvQ-0OAd4~(4&9Ox1w!9KjB1DagN?f2(DoIqP zotLuGT~^**?Zey=!aunqR{Vuq*$P9C_M9fll2x{xEOjllUA3fgtWfp5O2t~Gq#di+ zL=)0tdM-`0a;c@)GBw>Rwe^}N==V)ipGvQ5-n7VKLT2usrA=ub%M-P7$4owbwosZ) zu$2<^YF^YEIe`{4DYKPL&CvX#g@=g;^Q!{k#R;BF@cbX%iV`D~-wNY%ek4{m+-AKZ z$!5i9uMyHJ)Cvu+A<9C-X*tD<`3+x2aQc4;l88X!XWq(DDu`+<7JzRE!w!Ovu){2U z0rn_p++}vcS<+>iFdOx#o1?NMR8%3C7qW$lVz-DM;v9o#8vGS&CW~`sgC+l`T zX*qe*uvAY<&UHO50aDd<|9K&ZgV4Zhi z-`zdf-Gi42YVFn4 zMQUxh^_D+Re?v5#Ky(5&N0%;4(V;<-VuCs8MX)T#rx8N0(coq_a+_h8NHiO`^46`i zbO&=B+p){GWtAR2*>)=}ujQ6F7V{}qNpaG3EM){a!;LoO_tOyyVhCdNC&GV6KciN) zK{30{xy^D9Tm5#!RXoS_9(_)G-aU&fm-2PebLoj@^VXCI1I~Fzz zxxV4Mj=64#dZ8|wPQ}sHV0q=~{DWe55)!j1%uYgz8CFQk3Klsj21kg3Lj;F#M52Cx z`$^EKvx3s3;$(AY2Mf3-dAwEPeWfSJEss^IZr>8h_M=Mq^J*v(reKI*NXYvz8g3Nh z;WmP=&?bfrl1|4^;>w)D+?btRX*GSj^LVk 1) { - initial_position = new google.maps.LatLng(l[0], l[1]); - location_map.setZoom( parseInt(l[2]) ); + current_position = new google.maps.LatLng(l[0], l[1]); + map.setZoom( parseInt( l[2] ) ); } } - else + + if ( ! current_position ) { - location_map.setZoom(zoom); + var locationRohr = new google.maps.LatLng( 49.340174,10.890595 ); + current_position = locationRohr; + map.setZoom( initZoom ); } - - function savePosition( p ) - { - point = p; - location_coordinate.val(point.lat().toFixed(6) + "," + point.lng().toFixed(6) + "," + location_map.getZoom() ); - } - - var marker = new google.maps.Marker({ - map: location_map, - position: initial_position, - draggable: true - }); + function savePosition( p ) { + current_position = p; + coordinate_field.val( current_position.lat().toFixed(6) + "," + current_position.lng().toFixed(6) + "," + map.getZoom() ); + } + + var marker = new google.maps.Marker({ + map: map, + position: current_position, + draggable: true + }); google.maps.event.addListener(marker, 'dragend', function(mouseEvent) { savePosition(mouseEvent.latLng); }); - google.maps.event.addListener(location_map, 'click', function(mouseEvent){ + google.maps.event.addListener(map, 'click', function(mouseEvent){ marker.setPosition(mouseEvent.latLng); - savePosition(mouseEvent.latLng); + savePosition( mouseEvent.latLng ); }); - google.maps.event.addListener(location_map, 'zoom_changed', function(mouseEvent){ - savePosition(point); + google.maps.event.addListener(map, 'zoom_changed', function(mouseEvent){ + savePosition( current_position ); }); - location_based.bindWithDelay("keypress", function() { - - var lstr = []; - location_based.each(function(){ - var b = $(this); - lstr.push(b.val()) - }); + base_field.bindWithDelay( "keypress", function() { - geocode(lstr.join(','), function(l){ - location_coordinate.val( l.lat()+','+l.lng() ); + geocode( base_field.val() , function(l){ + coordinate_field.val( l.lat()+','+l.lng() ); }); - onLocationCoordinateChange(); }, 2000 ); @@ -79,19 +69,16 @@ { var latlng = jQuery(this).val().split(/,/); if (latlng.length < 2) return; - var latlng = new google.maps.LatLng(latlng[0], latlng[1]); + var latlng = new google.maps.LatLng( latlng[0], latlng[1] ); placeMarker( latlng ); - location_map.setZoom( latlng[2] ); - //geocode_reverse(latlng, function(l){ - // location_coordinate.val(l.lat()+','+l.lng()); - //}); + map.setZoom( latlng[2] ); } function placeMarker(location) { marker.setPosition(location); - location_map.setCenter(location); + map.setCenter(location); savePosition(location); - location_map.panTo(location); + map.panTo(location); } function geocode(address, cb) { @@ -119,21 +106,22 @@ } }*/ - - placeMarker(initial_position); + placeMarker( current_position ); } - + load(); } - + $('input[data-location-widget]').each(function(){ var $el = $(this); var $map = $($el.attr('data-map')), - $based_fields = $($el.attr('data-based-fields')), + $based_field = $(($el.attr('data-based-field'))), zoom = parseInt($el.attr('data-zoom')); - location_field_load($map, $based_fields, zoom); + location_field_load( $map, $based_field, zoom ); }); + + }); diff --git a/location_field/models.py b/location_field/models.py index 4349da9..de9e2cd 100755 --- a/location_field/models.py +++ b/location_field/models.py @@ -4,8 +4,8 @@ from location_field import forms class BaseLocationField(object): - def __init__(self, based_fields=[], zoom=2, default=None, *args, **kwargs): - self._based_fields = based_fields + def __init__(self, based_field=None, zoom=2, default=None, *args, **kwargs): + self._based_field = based_field self._zoom = zoom self._default = default self.default = default @@ -13,7 +13,7 @@ class BaseLocationField(object): def formfield(self, **kwargs): return super(BaseLocationField, self).formfield( form_class=self.formfield_class, - based_fields=self._based_fields, + based_field=self._based_field, zoom=self._zoom, default=self._default, **kwargs) @@ -22,10 +22,10 @@ class BaseLocationField(object): class PlainLocationField(BaseLocationField, CharField): formfield_class = forms.PlainLocationField - def __init__(self, based_fields=None, zoom=None, + def __init__(self, based_field=None, zoom=None, max_length=63, *args, **kwargs): - super(PlainLocationField, self).__init__(based_fields=based_fields, + super(PlainLocationField, self).__init__(based_field=based_field, zoom=zoom, *args, **kwargs) CharField.__init__(self, max_length=max_length, *args, **kwargs) diff --git a/location_field/views.py b/location_field/views.py deleted file mode 100755 index 60f00ef..0000000 --- a/location_field/views.py +++ /dev/null @@ -1 +0,0 @@ -# Create your views here. diff --git a/location_field/widgets.py b/location_field/widgets.py index 7774fc7..a255e99 100644 --- a/location_field/widgets.py +++ b/location_field/widgets.py @@ -1,11 +1,12 @@ from django.forms import widgets from django.utils.safestring import mark_safe - class LocationWidget(widgets.TextInput): - def __init__(self, attrs=None, based_fields=None, zoom=None, **kwargs): - self.based_fields = based_fields + def __init__(self, attrs=None, based_field=None, zoom=None, width=610, height = 480, **kwargs): + self.based_field = based_field self.zoom = zoom + self.width = width + self.height = height super(LocationWidget, self).__init__(attrs) def render(self, name, value, attrs=None): @@ -20,29 +21,23 @@ class LocationWidget(widgets.TextInput): else: value = '' - if '-' not in name: - prefix = '' - else: - prefix = name[:name.rindex('-') + 1] - - based_fields = ','.join( - map(lambda f: '#id_' + prefix + f.name, self.based_fields)) - + based_field = "#id_" + self.based_field.name + attrs = attrs or {} attrs['data-location-widget'] = name - attrs['data-based-fields'] = based_fields + attrs['data-based-field'] = based_field attrs['data-zoom'] = self.zoom attrs['data-map'] = '#map_' + name text_input = super(LocationWidget, self).render(name, value, attrs) map_div = u''' -
- -
-
-''' - return mark_safe(text_input + map_div % {'name': name}) +
+
+
+ ''' + + return mark_safe(text_input + map_div % {'name': name, 'width': self.width, 'height': self.height }) class Media: # Use schemaless URL so it works with both, http and https websites diff --git a/website/templates/website/concert_route.html b/website/templates/website/concert_route.html index 8b80240..fbaefa6 100644 --- a/website/templates/website/concert_route.html +++ b/website/templates/website/concert_route.html @@ -84,7 +84,7 @@ controlText.innerHTML = 'Konzertort anzeigen'; controlUI.appendChild(controlText); - var target = new google.maps.LatLng( {{routeInfo.destination}} ); + var target = new google.maps.LatLng( {{ nextConcert.map_location }} ); google.maps.event.addDomListener(controlUI, 'click', function() { map.setMapTypeId( google.maps.MapTypeId.HYBRID );