Skip to main content

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

Project description

A fork of django-geoposition.

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

https://badge.fury.io/py/django-geoposition-2.svg https://travis-ci.org/imdario/django-geoposition.svg?branch=master

Prerequisites

Please use from version 0.3.4. Previous versions had several unsolved issues by their original maintainers. django-geoposition requires Django 1.8 or greater.

Installation

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

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

    INSTALLED_APPS = (
        # …
        "geoposition",
    )
  • If you want to use Google Maps, set your Google API key in your 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 want to use OpenStreetMap, activate Leaflet backend in your settings file:

    GEOPOSITION_BACKEND = ‘leaflet’

    As Leaflet is provider agnostic, you could use any other map provider from the following list: http://leaflet-extras.github.io/leaflet-providers/preview

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 geoposition import Geoposition
    >>> from myapp.models import PointOfInterest
    >>> poi = PointOfInterest.objects.create(name='Foo', position=Geoposition(52.522906, 13.41156))
    >>> 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:

geoposition-widget-admin

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,
    'parentSelector': 'li.changeform-tabs-item',
    'isDjangoAdmin': True
}

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-geoposition-2-0.4.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

django_geoposition_2-0.4.0-py3-none-any.whl (24.1 kB view details)

Uploaded Python 3

File details

Details for the file django-geoposition-2-0.4.0.tar.gz.

File metadata

  • Download URL: django-geoposition-2-0.4.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.2

File hashes

Hashes for django-geoposition-2-0.4.0.tar.gz
Algorithm Hash digest
SHA256 0a644c4b3d97e87e9b44bc6c00f0bdcfc440eb6729efa2aa75bb16623ea6c6a0
MD5 2a7ec34c2559750cc45b7673980a1fcc
BLAKE2b-256 b8975dd43de6ce9727a8b182be40a117b11ab188b7c6f249c2a3a7d469bac624

See more details on using hashes here.

File details

Details for the file django_geoposition_2-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_geoposition_2-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a7ae0a5c3ac81e6268cd56d9ad254d9b73e5d8b9c470df5b10725843f8de767
MD5 0c642d35eebdded767a9fae2742ffea0
BLAKE2b-256 745fc4eddae8b2af030907e707d44195720892e7152107c44a3fc05cdc56c2f0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page