• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

madisona/django-google-maps: Using the Google Maps API with django model admin

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

madisona/django-google-maps

开源软件地址(OpenSource Url):

https://github.com/madisona/django-google-maps

开源编程语言(OpenSource Language):

Python 76.4%

开源软件介绍(OpenSource Introduction):

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

Build 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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap