2011-11-15 37 views
2

我想使用Google地圖在我的Django項目中顯示多個郵件地址。地址是來自數據庫的變量。谷歌地圖在Django項目中顯示多個位置

到現在爲止,我已經試過django-easy-maps這是很適合只顯示一個地址。就像它說的那樣,如果你只有一個地址(可以顯示多個地址),它很容易使用。

我也試過django-gmapi它可以顯示多個地址(以latlng格式)。但我很難將我的郵寄地址轉換爲latlng格式。

所以我的問題是:

  1. 是否django-easy-maps支持多個地址嗎?
  2. 如何使用geocodingdjango-gmapi
  3. 任何建議如何顯示多個我們在Django谷歌地圖上發佈的地址?

回答

0

我可以幫助解決地點2 ...如何地理編碼您的現有地址。

UPDATE
看起來gmapi有它的內置,你可能不需要任何我粘貼下面的代碼自己的地理編碼幫手。請參閱:Does anybody has experiences with geocoding using django-gmapi?


我用下面的代碼:

import urllib 

from django.conf import settings 
from django.utils.encoding import smart_str 
from django.db.models.signals import pre_save 
from django.utils import simplejson as json 


def get_lat_long(location): 
    output = "csv" 
    location = urllib.quote_plus(smart_str(location)) 
    request = "http://maps.google.co.uk/maps/api/geocode/json?address=%s&sensor=false" % location 
    response = urllib.urlopen(request).read() 
    data = json.loads(response) 
    if data['status'] == 'OK': 
     # take first result 
     return (str(data['results'][0]['geometry']['location']['lat']), str(data['results'][0]['geometry']['location']['lng'])) 
    else: 
     return (None, None) 

def get_geocode(sender, instance, **kwargs): 
    tlat, tlon = instance._geocode__target_fields 
    if not getattr(instance, tlat) or not getattr(instance, tlon): 
     map_query = getattr(instance, instance._geocode__src_field, '') 
     if callable(map_query): 
      map_query = map_query() 
     lat, lon = get_lat_long(map_query) 
     setattr(instance, tlat, lat) 
     setattr(instance, tlon, lon) 

def geocode(model, src_field, target_fields=('lat','lon')): 
    # pass src and target field names as strings 
    setattr(model, '_geocode__src_field', src_field) 
    setattr(model, '_geocode__target_fields', target_fields) 
    pre_save.connect(get_geocode, sender=model) 

(可能我借它從一個Github上項目的地方,我已經失去了歸屬,如果是這樣,對不起!)

然後在你的模型上你需要這樣的東西:

from django.db import models 
from gmaps import geocode # import the function from above 

class MyModel(models.Model): 
    address = models.TextField(blank=True) 
    city = models.CharField(max_length=32, blank=True) 
    postcode = models.CharField(max_length=32, blank=True) 

    lat = models.DecimalField(max_digits=12, decimal_places=6, verbose_name='latitude', blank=True, null=True, help_text="Will be filled automatically.") 
    lon = models.DecimalField(max_digits=12, decimal_places=6, verbose_name='longitude', blank=True, null=True, help_text="Will be filled automatically.") 

    def map_query(self): 
     """ 
     Called on save by the geocode decorator which automatically fills the 
     lat,lng values. This method returns a string to use as query to gmaps. 
     """ 
     map_query = '' 
     if self.address and self.city: 
      map_query = '%s, %s' % (self.address, self.city) 
     if self.postcode: 
      if map_query: 
       map_query = '%s, ' % map_query 
      map_query = '%s%s' % (map_query, self.postcode) 
     return map_query 

geocode(Venue, 'map_query') 

然後來geocode喲烏爾現有的數據,你可以只是重新保存所有現有的記錄,如:

from .models import MyModel 

for obj in MyModel.objects.all(): 
    obj.save() 
+0

@ user1046012哦,我看到的另一個問題是你太讓我猜你有答案了! – Anentropic

相關問題