Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix latLng nulls + right click move marker implementation #56

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ env:
- DJANGO=1.8.14
- DJANGO=1.9.9
- DJANGO=1.10
- DJANGO=1.11
install:
- pip install -q Django==$DJANGO
- pip install .
Expand Down
2 changes: 1 addition & 1 deletion geoposition/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

default_app_config = 'geoposition.apps.GeoPositionConfig'

VERSION = (0, 3, 0)
VERSION = (0, 3, 1)
__version__ = '.'.join(map(str, VERSION))


Expand Down
6 changes: 6 additions & 0 deletions geoposition/app_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from __future__ import unicode_literals
from django.conf import settings

GEOPOSITION_GMAP_URL = getattr(settings, 'GEOPOSITION_GMAP_URL', '')
GEOPOSITION_GMAP_URL_AUTOLOAD = getattr(settings, 'GEOPOSITION_GMAP_URL_AUTOLOAD', True)

25 changes: 19 additions & 6 deletions geoposition/static/geoposition/geoposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ if (jQuery != undefined) {
$searchInput.appendTo($searchRow);
$container.append($searchRow, $mapContainer, $addressRow);

mapLatLng = new google.maps.LatLng(latitude, longitude);
mapLatLng = new google.maps.LatLng(latitude || 0, longitude || 0);

mapOptions = $.extend({}, mapDefaults, mapCustomOptions);

Expand Down Expand Up @@ -153,15 +153,28 @@ if (jQuery != undefined) {
google.maps.event.trigger(marker, 'dragend');
}

$latitudeField.add($longitudeField).bind('keyup', function(e) {
var latitude = parseFloat($latitudeField.val()) || 0;
$latitudeField.add($longitudeField).on('keyup', function(e) {
updateMarkerPos();
map.setCenter(center);
map.setZoom(15);

});

map.addListener('rightclick', function(e) {
$latitudeField.val(e.latLng.lat());
$longitudeField.val(e.latLng.lng());
updateMarkerPos();
doGeocode();
});

function updateMarkerPos(){
var latitude = parseFloat($latitudeField.val()) || 0;

var longitude = parseFloat($longitudeField.val()) || 0;
var center = new google.maps.LatLng(latitude, longitude);
map.setCenter(center);
map.setZoom(15);
marker.setPosition(center);
doGeocode();
});
}
});
});
})(django.jQuery);
16 changes: 14 additions & 2 deletions geoposition/templates/geoposition/widgets/geoposition.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,23 @@
<table>
<tr>
<td>{{ latitude.label|capfirst }}</td>
<td>{{ latitude.html }}</td>
<td>
{% if latitude.widget %}
{% include latitude.widget.template_name with widget=latitude.widget %}
{% else %}
{{ latitude.html }}
{% endif %}
</td>
</tr>
<tr>
<td>{{ longitude.label|capfirst }}</td>
<td>{{ longitude.html }}</td>
<td>
{% if longitude.widget %}
{% include longitude.widget.template_name with widget=longitude.widget %}
{% else %}
{{ longitude.html }}
{% endif %}
</td>
</tr>
</table>
</div>
40 changes: 34 additions & 6 deletions geoposition/widgets.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
from __future__ import unicode_literals

import json

from django import forms
from django.template.loader import render_to_string
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from .conf import settings

from .conf import settings
from . import app_settings

class GeopositionWidget(forms.MultiWidget):
template_name = 'geoposition/widgets/geoposition.html'

def __init__(self, attrs=None):
widgets = (
forms.TextInput(),
Expand All @@ -23,7 +27,30 @@ def decompress(self, value):
return [value.latitude, value.longitude]
return [None, None]

def get_config(self):
return {
'map_widget_height': settings.MAP_WIDGET_HEIGHT or 500,
'map_options': json.dumps(settings.MAP_OPTIONS),
'marker_options': json.dumps(settings.MARKER_OPTIONS),
}


def get_context(self, name, value, attrs):
# Django 1.11 and up
context = super(GeopositionWidget, self).get_context(name, value, attrs)
context['latitude'] = {
'widget': context['widget']['subwidgets'][0],
'label': _("latitude"),
}
context['longitude'] = {
'widget': context['widget']['subwidgets'][1],
'label': _("longitude"),
}
context['config'] = self.get_config()
return context

def format_output(self, rendered_widgets):
# Django 1.10 and down
return render_to_string('geoposition/widgets/geoposition.html', {
'latitude': {
'html': rendered_widgets[0],
Expand All @@ -33,18 +60,19 @@ def format_output(self, rendered_widgets):
'html': rendered_widgets[1],
'label': _("longitude"),
},
'config': {
'map_widget_height': settings.MAP_WIDGET_HEIGHT or 500,
'map_options': json.dumps(settings.MAP_OPTIONS),
'marker_options': json.dumps(settings.MARKER_OPTIONS),
}
'config': self.get_config(),
})

class Media:
js = (
'//maps.google.com/maps/api/js?key=%s' % settings.GOOGLE_MAPS_API_KEY,
'geoposition/geoposition.js',
)
if app_settings.GEOPOSITION_GMAP_URL_AUTOLOAD:
if app_settings.GEOPOSITION_GMAP_URL:
js += (
app_settings.GEOPOSITION_GMAP_URL
)
css = {
'all': ('geoposition/geoposition.css',)
}