Plugs google maps V3 api into Django admin.
Project description
django-google-maps is a simple application that provides the basic hooks into google maps V3 api for use in Django models from Django version 1.11+.
Starting with django-google-maps version (0.7.0), Django 1.11+ is required because Django changed their widget template rendering system. Version 0.8.0 supports Django 2.0+, and as such removes support for Python 2.7
I’m using this to allow someone from the admin panels to type a freeform address, have the address geocoded on change and plotted on the map. If the location is not 100% correct, the user can drag the marker to the correct spot and the geo coordinates will update.
Status
USAGE:
include the django_google_maps app in your settings.py
Add your Google Maps API Key in your settings.py as GOOGLE_MAPS_API_KEY
create a model that has both an address field and geolocation field
from django.db import models from django_google_maps import fields as map_fields class Rental(models.Model): address = map_fields.AddressField(max_length=200) geolocation = map_fields.GeoLocationField(max_length=100)
in the admin.py include the following as a formfield_override
from django.contrib import admin from django_google_maps import widgets as map_widgets from django_google_maps import fields as map_fields class RentalAdmin(admin.ModelAdmin): formfield_overrides = { map_fields.AddressField: {'widget': map_widgets.GoogleMapsAddressWidget}, }
To change the map type (hybrid by default), you can add an html attribute on the AddressField widget. The list of allowed values is: hybrid, roadmap, satellite, terrain
from django.contrib import admin from django_google_maps import widgets as map_widgets from django_google_maps import fields as map_fields class RentalAdmin(admin.ModelAdmin): formfield_overrides = { map_fields.AddressField: { 'widget': map_widgets.GoogleMapsAddressWidget(attrs={'data-map-type': 'roadmap'})}, }
To change the autocomplete options, you can add an html attribute on the AddressField widget. See https://developers.google.com/maps/documentation/javascript/places-autocomplete#add_autocomplete for a list of available options
import json from django.contrib import admin from django_google_maps import widgets as map_widgets from django_google_maps import fields as map_fields class RentalAdmin(admin.ModelAdmin): formfield_overrides = { map_fields.AddressField: { ‘widget’: map_widgets.GoogleMapsAddressWidget(attrs={ ‘data-autocomplete-options’: json.dumps({ ‘types’: [‘geocode’, ‘establishment’], ‘componentRestrictions’: { 'country': 'us' } }) }) }, }
That should be all you need to get started.
I also like to make the geolocation field readonly in the admin so a user (myself) doesn’t accidentally change it to a nonsensical value. There is validation on the field so you can’t enter an incorrect value, but you could enter something that is not even close to the address you intended.
When you’re displaying the address back to the user, just request the map using the geocoordinates that were saved in your model. Maybe sometime when I get around to it I’ll see if I can create a method that will build that into the model.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-google-maps-0.13.0.tar.gz
.
File metadata
- Download URL: django-google-maps-0.13.0.tar.gz
- Upload date:
- Size: 10.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d8905f6c42892b8d429bfb89532b7d0eaa06ab53a850f3c3625440b0caa9099d |
|
MD5 | 6c607091698693b2a37d47d50f9dbf0d |
|
BLAKE2b-256 | 1fb2946e9d00b0d85dc27a62854e336856bddb285a7773f22360ca05a3999006 |
File details
Details for the file django_google_maps-0.13.0-py3-none-any.whl
.
File metadata
- Download URL: django_google_maps-0.13.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0dbffbae929cde07666e2c5eba752596cd6fee12d39f50afd3940cc80a535a9 |
|
MD5 | 32ed56e6ad51def8fa2be68288393bd2 |
|
BLAKE2b-256 | 2f60f6e6a0aeccfbab9e4928709c3b6eeb9ec6d41e66c3529986890a32a1a2b0 |