开发者

Geocoding an address on form submission?

Trying to wrap my head around django forms and the django way of doing things. I want to create a basic web form that allows a user to input an address and have that address geocoded and saved to a database.

I created a Location model:

class Location(models.Model):
    address = models.CharField(max_length=200)
    city = models.CharField(max_length=100)
    state = models.CharField(max_length=100, null=True)
    postal_code = models.CharField(max_length=100, null=True)
    country = models.CharField(max_length=100)
    latitude = models.DecimalField(max_digits=18, decimal_places=10, null=True)
    longitude = models.DecimalField(max_digits=18, decimal_places=10, null=True)

And defined a form:

class LocationForm(forms.ModelForm):
    class Meta:
        model = models.Location
        exclude = ('latitude','longitude')

In my view I'm using form.save() to save the form. This works and saves an address to the database.

I created a module to geocode an address. I'm not sure what the django way of doing things is, but I guess in my view, before I save the form, I need to geocode the address and set the lat and long. How do I set the latitude and longit开发者_Go百科ude before saving?


You can override the model's save method. I geocode the data before saving. This is using googleapi, but it can be modified accordingly.

import urllib

def save(self):
    location = "%s, %s, %s, %s" % (self.address, self.city, self.state, self.zip)

    if not self.latitude or not self.longitude:
        latlng = self.geocode(location)
        latlng = latlng.split(',')
        self.latitude = latlng[0]
        self.longitude = latlng[1]

    super(Marker, self).save()

def geocode(self, location):
    output = "csv"
    location = urllib.quote_plus(location)
    request = "http://maps.google.com/maps/geo?q=%s&output=%s&key=%s" % (location, output, settings.GOOGLE_API_KEY)
    data = urllib.urlopen(request).read()
    dlist = data.split(',')
    if dlist[0] == '200':
        return "%s,%s" % (dlist[2], dlist[3])
    else:
        return ','


Update for Google Maps API v3:

import json
import urllib.parse
from decimal import Decimal

def save(self):
    if not self.lat or not self.lng:
        self.lat, self.lng = self.geocode(self.address)

    super(Location, self).save()

def geocode(self, address):
    address = urllib.parse.quote_plus(address)
    maps_api_url = "?".join([
        "http://maps.googleapis.com/maps/api/geocode/json",
        urllib.parse.urlencode({"address": address, "sensor": False})
    ])
    response = urllib.urlopen(maps_api_url)
    data = json.loads(response.read().decode('utf8'))

    if data['status'] == 'OK':
        lat = data['results'][0]['geometry']['location']['lat']
        lng = data['results'][0]['geometry']['location']['lng']
        return Decimal(lat), Decimal(lng)


You could also use the django.db.models.signals.pre_save-signal!

Have a look at Django's signal documentation at http://docs.djangoproject.com/en/dev/topics/signals/.


i am doing the same thing like this might be yew are looking for something similarstrong text

# forms.py 
from django.contrib.gis.utils import GeoIP


class registerForm(forms.ModelForm): 
class Meta:
    model=register
    fields = ('Availability', 'Status')

def save(self,ip_address, *args, **kwargs):
    g = GeoIP()
    lat, lon = g.lat_lon(ip_address)
    user_location = super(registerForm, self).save(commit=False)
    user_location.latitude = lat
    user_location.longitude = lon
    user_location.save(*args, **kwargs)

and my views

    #views.py
    if request.method == "POST":
 ip_address=request.META['REMOTE_ADDR']
    rform = registerForm(data = request.POST)
    if rform.is_valid():
  register = rform.save(commit=False)
  register.user=request.user
     register.save(ip_address)
        return render_to_response('home.html')
else:
    rform = registerForm() 
return render_to_response('status_set.html',{'rform':rform}) 


Similar to other answers:

def geocode(address, city, state, zip_code):
    try:
        location_param = urllib.request.quote("%s, %s, %s, %s" % (address, city, state, zip_code))
        url_request = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false" % location_param
        result = requests.get(url_request)
        data = result.json()
        location = data['results'][0]['geometry']['location']
        lat = location['lat']
        lng = location['lng']
        return lat, lng
    except Exception:
        return None
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜