olwidget
is a library which replaces textareas containing WKT data with editable OpenLayers maps. While the javascript library functions on its own, it was written to use with django apps which display and edit geographic data.
Documentation for olwidget.js
is included in the doc
directory.
To use the django app requires a couple of steps:
- Copy or link the
olwidget
directory indjango_app
into the search path for your django project. - Copy or link the media (
css
,img
, andjs
) into a directory namedolwidget
under your project'sMEDIA_URL
. If you wish to name the directory something else, defineOLWIDGET_MEDIA_URL
with the URL for the media files in your settings file. - Include
'olwidget'
in your project's settingsINSTALLED_APPS
list. - (Optional) If you want to use google or yahoo map layers, you must include
GOOGLE_API_KEY
orYAHOO_APP_ID
in your settings file.olwidget
uses OpenStreetMaps by default, which requires no key.
A form definition that uses OLWidget:
from django import forms
from olwidget.widgets import OLWidget
class MyForm(forms.Form):
location = forms.CharField(widget=OLWidget())
In a template:
<html>
<head> {{ form.media }} </head>
<body>... {{ form }} ...</body>
</html>
A map displaying several fields:
from olwidget.widgets import MapDisplay
map = MapDisplay(fields=[mymodel.start_point, mymodel.destination])
# template
<head> {{ map.media }} </head>
<body>... {{ map }} ...</body>
Example to use OLWidget
in the Django admin site:
# admin.py
from olwidget import admin
from myapp import Restaurant, Owner
# Use the default map
admin.site.register(Restaurant, admin.GeoModelAdmin)
# Customize the map
admin.site.register(Owner, admin.custom_geo_admin({
'layers': ['google.streets'],
'default_lat': 44,
'default_lon': -72,
})
Several olwidget
features, including widgets.OLWidget
, widgets.MapDisplay
, and admin.custom_geo_admin
, take a map_options
argument that customizes the look and feel of the maps that are produced. These options are passed directly to the olwidget.js
Map constructor. An example:
from olwidget.widgets import MapDisplay
map = MapDisplay(map_options={
'layers': ['osm.mapnik', 'google.hybrid', 'yahoo'],
'default_lat': 44,
'default_lon': -72,
})
For a complete list of options available to olwidget.js, see the olwidget.js documentation.