forked from erdem/django-map-widgets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.rst
344 lines (251 loc) · 8.94 KB
/
README.rst
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
Django Map Widgets
~~~~~~~~~~~~~~~~~~
Configurable, pluggable and more user friendly map widgets for Django
PostGIS fields.
Achievements
~~~~~~~~~~~~
The aim of the Django map widgets is to make all the Django widgets more
user friendly and configurable. Map widgets support major map services
(GoogleMaps, OpenStreetMap) for your geoDjango fields.
Installation
~~~~~~~~~~~~
::
pip install django-map-widgets
Add ‘map\_widgets’ to your ``INSTALLED_APPS`` in settings.py
.. code:: python
INSTALLED_APPS = [
...
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mapwidgets',
]
Requirements
~~~~~~~~~~~~
Django map widgets require the jQuery framework, it does not import a
jQuery library. It's working with your global jQuery. If you will use
map widgets in Django Admin, you don't need to add a jQuery. Map widgets
work with Django Admin jQuery file.
PointField Map Widgets
~~~~~~~~~~~~~~~~~~~~~~
Google Map Widget
^^^^^^^^^^^^^^^^^
Settings
''''''''
- **GOOGLE\_MAP\_API\_KEY**: Put your Google API key
- **mapCenterLocationName**: You can give specific location name for
center of map. Map widget will found this location coordinates using
Google Places Autocomplete. (Optional)
- **mapCenterLocation**: You can give specific coordinates for center
of the map. Coordinates must be list type. ([latitude, longitude])
(Optional)
- **zoom** : Default zoom value.
Note: If there is no set value for the map center,
(mapCenterLocationName, mapCenterLocation) the widget will be
centered by the timezone setting of the project.
Check out this links.
- Timezone Center Locations
- countries.json
Usage
~~~~~
**settings.py**
.. code:: python
MAP_WIDGETS = {
"GooglePointFieldWidget": (
("zoom", 15),
("mapCenterLocationName", "london"),
),
"GOOGLE_MAP_API_KEY": "<google-map-api-key>"
}
If you want to give specific coordinates for center of the map, you can
update your settings file like that.
.. code:: python
MAP_WIDGETS = {
"GooglePointFieldWidget": (
("zoom", 15),
("mapCenterLocation", [57.7177013, -16.6300491]),
),
"GOOGLE_MAP_API_KEY": "<google-map-api-key>"
}
Django Admin
^^^^^^^^^^^^
.. code:: python
from mapwidgets.widgets import GooglePointFieldWidget
class CityAdmin(admin.ModelAdmin):
formfield_overrides = {
models.PointField: {"widget": GooglePointFieldWidget}
}
Django Forms
^^^^^^^^^^^^
.. code:: python
from mapwidgets.widgets import GooglePointFieldWidget
class CityAdminForm(forms.ModelForm):
class Meta:
model = City
fields = ("coordinates", "city_hall")
widgets = {
'coordinates': GooglePointFieldWidget,
'city_hall': GooglePointFieldWidget,
}
Preview
^^^^^^^
.. figure:: https://cloud.githubusercontent.com/assets/1518272/18732352/724dd098-805a-11e6-8eb4-6ba9b5e06a81.png
:alt:
jQuery Triggers
^^^^^^^^^^^^^^^
If you want develop your map UI on front-end side, you can use map
widget jQuery triggers.
- **google\_point\_map\_widget:marker\_create**: Triggered when user
create first marker on map. *callback params: lat, lng,
locationInputElem, mapWrapID*
- **google\_point\_map\_widget:marker\_change**: Triggered when user
change marker position on map. *callback params: lat, lng,
locationInputElem, mapWrapID*
- **google\_point\_map\_widget:marker\_delete**: Triggered when user
delete marker on map. *callback params: lat, lng, locationInputElem,
mapWrapID*
.. code:: javascript
$(document).on("google_point_map_widget:marker_create", function (e, lat, lng, locationInputElem, mapWrapID) {
console.log(locationInputElem); // django widget textarea widget (hidden)
console.log(lat, lng); // created marker coordinates
console.log(mapWrapID); // map widget wrapper element ID
});
$(document).on("google_point_map_widget:marker_change", function (e, lat, lng, locationInputElem, mapWrapID) {
console.log(locationInputElem); // django widget textarea widget (hidden)
console.log(lat, lng); // changed marker coordinates
console.log(mapWrapID); // map widget wrapper element ID
});
$(document).on("google_point_map_widget:marker_delete", function (e, lat, lng, locationInputElem, mapWrapID) {
console.log(locationInputElem); // django widget textarea widget (hidden)
console.log(lat, lng); // deleted marker coordinates
console.log(mapWrapID); // map widget wrapper element ID
})
Google Map Widget for Django Admin Inlines
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
As you know Django Admin has inline feature and you can add an inline
row with dynamically. In this case, Django default map widget doesn't
initialize widget when created a new inline row.
If you want to use Google Map Widget on admin inlines with no issue, you
just need to use ``GooglePointFieldInlineWidget`` class.
Example
^^^^^^^
.. code:: python
from mapwidgets.widgets import GooglePointFieldInlineWidget
class DistrictAdminInline(admin.TabularInline):
model = District
extra = 3
formfield_overrides = {
models.PointField: {"widget": GooglePointFieldInlineWidget}
}
class CityAdmin(admin.ModelAdmin):
inlines = (DistrictAdminInline,)
Preview
^^^^^^^
|image0| ### Google Static Map Widget (ReadOnly)
Settings
^^^^^^^^
Django map widgets provide all Google Static Map API features. Check out
this link for google static map features.
Here is the all default settings attribute for google static map widget.
.. code:: python
MAP_WIDGETS = {
"GoogleStaticMapWidget": (
("zoom", 15),
("size", "480x480"),
("scale", ""),
("format", ""),
("maptype", ""),
("path", ""),
("visible", ""),
("style", ""),
("language", ""),
("region", "")
),
"GoogleStaticMapMarkerSettings": (
("size", "normal"),
("color", ""),
("icon", ""),
)
"GOOGLE_MAP_API_SIGNATURE": "",
"GOOGLE_MAP_API_KEY": "",
}
Usage
'''''
If you are not using specific features on Google Static Map API, you
just need to update ``GOOGLE_MAP_API_KEY`` value in your Django settings
file. If you need also individual size map images, you can pass ``size``
and ``zoom`` parameter for each ``GoogleStaticMapWidget`` class.
Example
^^^^^^^
**settings.py**
.. code:: python
MAP_WIDGETS = {
"GoogleStaticMapWidget": (
("zoom", 15),
("size", "320x320"),
),
"GoogleStaticMapMarkerSettings": (
("color", "green"),
)
"GOOGLE_MAP_API_KEY": "<google-map-api-key>"
}
**forms.py**
.. code:: python
from mapwidgets.widgets import GoogleStaticMapWidget
class CityDetailForm(forms.ModelForm):
class Meta:
model = City
fields = ("name", "coordinates", "city_hall")
widgets = {
'coordinates': GoogleStaticMapWidget,
'city_hall': GoogleStaticMapWidget(zoom=12, size="240x240"),
}
Google Static Map Overlay Widget (ReadOnly)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This widget is working with Magnific Popup jQuery plugin.
Usage
'''''
You can use also all static map features in this widget. Besides you can
give a ``thumbnail_size`` value.
Here is the all default settings attribute for google static overlay map
widget.
.. code:: python
MAP_WIDGETS = {
"GoogleStaticMapMarkerSettings": (
("size", "normal"),
("color", ""),
("icon", ""),
),
"GoogleStaticOverlayMapWidget": (
("zoom", 15),
("size", "480x480"),
("thumbnail_size", "160x160"),
("scale", ""),
("format", ""),
("maptype", ""),
("path", ""),
("visible", ""),
("style", ""),
("language", ""),
("region", "")
),
"GOOGLE_MAP_API_SIGNATURE": "",
"GOOGLE_MAP_API_KEY": "",
}
Example
^^^^^^^
.. code:: python
from mapwidgets.widgets import GoogleStaticOverlayMapWidget
class CityDetailForm(forms.ModelForm):
class Meta:
model = City
fields = ("name", "coordinates", "city_hall")
widgets = {
'coordinates': GoogleStaticOverlayMapWidget,
'city_hall': GoogleStaticOverlayMapWidget(zoom=12, size="640x640", thumbnail_size="50x50"),
}
Preview
^^^^^^^
.. figure:: https://cloud.githubusercontent.com/assets/1518272/18732296/18f1813e-805a-11e6-8801-f1f48ed02a9c.png
:alt:
.. |image0| image:: https://cloud.githubusercontent.com/assets/1518272/18221609/2cac10fe-7178-11e6-9990-a93176693ef7.gif