Skip to main content

This app makes it easy to display a map for a given address.

Project description

This app makes it easy to display a map for given address in django templates. No API keys, manual geocoding, html/js copy-pasting or django model changes is needed.

The license is MIT.

Installation

pip install 'geopy >= 0.95'
pip install django-easy-maps

Then add ‘easy_maps’ to INSTALLED_APPS and run ./manage.py syncdb (or ./manage.py migrate easy_maps if South is in use)

Settings

If you need a place where center the map when no address is inserted yet add the latitude and longitude to the EASY_MAPS_CENTER variable in your settings.py like the following:

EASY_MAPS_CENTER = (-41.3, 32)

Usage

This app provides an easy_map template tag:

{% easy_map <address> [<width> <height>] [<zoom>] [using <template_name>] %}

Examples:

{% load easy_maps_tags %}

<!-- Default map with 300x400 dimensions -->
{% easy_map "Russia, Ekaterinburg, Mira 32" 300 400 %}

<!-- Variable address, custom detail level and custom template -->
{% easy_map address 200 200 5 using 'map.html' %}

The coordinates for map will be obtained using google geocoder on first access. Then they’ll be cached in DB. Django’s template caching can be used later in order to prevent DB access on each map render:

{% load easy_maps_tags cache %}

{% cache 600 my_map firm.address %}
    {% easy_map firm.address 300 400 %}
{% endcache %}

Customization

If the default map template is not sufficient then custom map template can be used. Examples:

{% easy_map address using 'map.html' %}
{% easy_map address 200 300 5 using 'map.html' %}

The template will have ‘map’ (it is the easy_maps.models.Address instance auto-created for passed address on first access), ‘width’, ‘height’ and ‘zoom’ variables. The outer template context is passed to rendered template as well.

The default template can be found here: https://bitbucket.org/kmike/django-easy-maps/src/tip/easy_maps/templates/easy_maps/map.html

You can start your own template from scratch or just override some blocks in the default template.

Please refer to http://code.google.com/apis/maps/documentation/javascript/ for detailed Google Maps JavaScript API help.

Customizing geocoder

To use a custom geocoder set EASY_MAPS_GEOCODE option:

# settings.py

from django_easy_maps import geocode

def my_geocode(address):
    """
    Given an address (an unicode string), return
    ``(computed_address, (latitude, longitude))`` tuple.
    """
    try:
        # ...
        return computed_address, (latitude, longitude)
    except (...) as e:
        raise geocode.Error(e)

EASY_MAPS_GEOCODE = my_geocode

Address model

easy_maps.models.Address model has the following fields:

  • address - the requested address

  • computed_address - address returned by geocoder

  • longitude

  • latitude

  • geocode_error - True if geocoder wasn’t able to handle the address

Address model should be considered implementation detail. Its purpose is to avoid using geocoder for each request, that’s a kind of persistent cache. It is included in readme because information about available data can be useful for custom map templates.

Admin address preview

django-easy-maps provides basic widget that displays a map under the address field. It can be used in admin for map previews. Example usage:

from django import forms
from django.contrib import admin
from easy_maps.widgets import AddressWithMapWidget
from firms.models import Firm

class FirmAdmin(admin.ModelAdmin):
    class form(forms.ModelForm):
        class Meta:
            widgets = {
                'address': AddressWithMapWidget({'class': 'vTextField'})
            }

admin.site.register(Firm, FirmAdmin)

‘address’ field should be a CharField or TextField.

Contributing

If you’ve found a bug, implemented a feature or customized the template and think it is useful then please consider contributing. Patches, pull requests or just suggestions are welcome!

Source code: https://bitbucket.org/kmike/django-easy-maps/

Bug tracker: https://bitbucket.org/kmike/django-easy-maps/issues/new

0.8.4 (2013-08-27)

  • fix bad 0.8.3 release

0.8.3 (2013-08-27)

  • easy_map tag now works when address is None.

0.8.2 (2013-07-02)

  • Unique constraint is added to Address.address field (to prevent MultipleObjectsReturned exceptions).

    In order to upgrade, run

    python manage.py migrate easy_maps

  • German translation is added.

0.8.1 (2013-03-25)

  • Fix regressions in geocoding errors handling introduced in 0.8.

0.8 (2013-03-24)

  • Testing improvements;

  • EASY_MAPS_CENTER setting for default map coordinates;

  • allow to pass an Address instance as argument of easy_map tag;

  • better error handling;

  • switch to GoogleV3 geocoder;

  • customization hook: it is now possible to use a custom geocoding method;

  • EASY_MAPS_GOOGLE_KEY now does nothing (it is not a meaningful option for V3 Geocoding API).

Minimum required django version is 1.3 since this release. It may work with older versions, but this is untested.

0.7.4 (2013-01-03)

  • switch to https;

  • make example settings django 1.4 compatible;

0.7.3 (2012-09-21)

  • use only first placemark from geocoder.

0.7.2 (2012-01-07)

  • static fallback for map.html;

  • fix localization of floats.

0.7.1 (2011-01-31)

  • better error handling;

  • EASY_MAPS_GOOGLE_KEY setting.

0.7 (2010-12-24)

  • longtitude -> longitude;

  • display is fixed for comma-delimited float locales.

0.6 (2010-12-02)

  • admin preview widget;

  • bugfixes.

0.5 (2010-12-01)

Initial release

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-easy-maps-0.8.4.tar.gz (11.5 kB view hashes)

Uploaded Source

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