A fork of django-geoposition to fix a few bugs related to porting from Python2 to Python3.
A model field that can hold a geoposition (latitude/longitude), and corresponding admin/form widget.
Starting with version 0.3, django-geoposition requires Django 1.8 or greater. If you need to support Django versions prior to 1.8 please use django-geoposition 0.2.3. For Django versions prior to 1.4.10 please use django-geoposition 0.1.5.
Use your favorite Python packaging tool to install
geoposition
from PyPI, e.g.:pip install django-geoposition
Add
"geoposition"
to yourINSTALLED_APPS
setting:INSTALLED_APPS = ( # … "geoposition", )
Set your Google API key in you settings file:
GEOPOSITION_GOOGLE_MAPS_API_KEY = 'YOUR_API_KEY'
API keys may be obtained here: https://developers.google.com/maps/documentation/javascript/get-api-key
If you are still using Django <1.3, you are advised to install django-staticfiles for static file serving.
django-geoposition
comes with a model field that makes it pretty
easy to add a geoposition field to one of your models. To make use of
it:
In your
myapp/models.py
:from django.db import models from geoposition.fields import GeopositionField class PointOfInterest(models.Model): name = models.CharField(max_length=100) position = GeopositionField()
This enables the following simple API:
>>> from myapp.models import PointOfInterest >>> poi = PointOfInterest.objects.get(id=1) >>> poi.position Geoposition(52.522906,13.41156) >>> poi.position.latitude 52.522906 >>> poi.position.longitude 13.41156
If you use a GeopositionField
in the admin it will automatically
show a Google Maps widget with a marker at the currently stored
position. You can drag and drop the marker with the mouse and the
corresponding latitude and longitude fields will be updated
accordingly.
It looks like this:
Using the map widget on a regular form outside of the admin requires just a little more work. In your template make sure that
- jQuery is included
- the static files (JS, CSS) of the map widget are included (just use
{{ form.media }}
)
Example:
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script&