django-geoposition 0.2.2

Django model field that can hold a geoposition, and corresponding admin widget.

A model field that can hold a geoposition (latitude/longitude), and corresponding admin/form widget.

Prerequisites

Starting with version 0.2, django-geoposition requires Django 1.4.10 or greater. If you need to support Django versions prior to 1.4.10, please use django-geoposition 0.1.5.

Installation

  • Use your favorite Python packaging tool to install geoposition from PyPI, e.g.:

    pip install django-geoposition
    
  • Add "geoposition" to your INSTALLED_APPS setting:

    INSTALLED_APPS = (
        # …
        "geoposition",
    )
    
  • If you are still using Django <1.3, you are advised to install django-staticfiles for static file serving.

Usage

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
    

Form field and widget

Admin

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:

Regular Forms

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>
<form method="POST" action="">{% csrf_token %}
    {{ form.media }}
    {{ form.as_p }}
</form>

Settings

You can customize the MapOptions and MarkerOptions used to initialize the map and marker in JavaScript by defining GEOPOSITION_MAP_OPTIONS or GEOPOSITION_MARKER_OPTIONS in your settings.py.

Example:

GEOPOSITION_MAP_OPTIONS = {
    'minZoom': 3,
    'maxZoom': 15,
}

GEOPOSITION_MARKER_OPTIONS = {
    'cursor': 'move'
}

Please note that you cannot use a value like new google.maps.LatLng(52.5,13.4) for a setting like center or position because that would end up as a string in the JavaScript code and not be evaluated. Please use Lat/Lng Object Literals for that purpose, e.g. {'lat': 52.5, 'lng': 13.4}.

You can also customize the height of the displayed map widget by setting GEOPOSITION_MAP_WIDGET_HEIGHT to an integer value (default is 480).

License

MIT

Author

Philipp Bosch

Pip

django-geoposition==0.2.2

Classifiers

  • Environment :: Web Environment
  • Framework :: Django
  • Intended Audience :: Developers
  • Operating System :: OS Independent
  • Programming Language :: Python
  • Development Status :: 4 - Beta
  • License :: OSI Approved :: BSD License
File Type Python Version Uploaded On Downloads
django-geoposition-0.2.2.tar.gz Source July 29, 2014 22,065
Version Release Date
0.2.2 July 29, 2014
0.2.1 June 12, 2014
0.2.0 May 20, 2014
0.1.5 Nov. 19, 2013
0.1.4 May 2, 2012
0.1.3 June 12, 2011
0.1.2 April 15, 2011
0.1.1 April 4, 2011
0.1.0 April 4, 2011
Date Package Version Action
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.1.5 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.1.4 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.1.3 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.1.2 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.1.1 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.1.0 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.2.0 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.2.1 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition 0.2.2 Release Created
Aug. 24, 2015, 11:03 p.m. django-geoposition Package Created