geodjango管理员地图不显示坐标点,点似乎保存正确

0 投票
1 回答
1260 浏览
提问于 2025-04-18 11:33

我正在开发一个程序,需要将用户的地址转换成坐标,然后存储在geodjango的Pointfield中。我已经设置好了所有内容,这就是我的代码。在我的模型文件中,有一个坐标字段是Pointfield。不过在管理页面上,坐标字段的地图显示出来了,但没有任何点。这是否意味着存在问题,比如距离查询可能无法正常工作?现在只显示一张地图,中心却没有任何点,看起来像是空旷的地方。

我将地址转换为坐标的方式是使用一个叫做geopy的工具。在我的视图中,当我保存餐厅表单时,我通过geo函数来转换地址字段:

a, c = geo.geocode(profile_address)

例如:

a, c = geo.geocode('408 Lonsdale street')
print c
(-31.812547, 149.960394)
pnt = Point(c)
print pnt
POINT (-31.8125470000000021 149.9603940000000080)

我尝试了其他方法来将地址转换为坐标并保存到Pointfield中,但总是返回错误,提示我无法保存那种类型的数据。所以我现在假设如果保存成功,那就应该没问题。

在我的视图中,如果我这样做:

return HttpResponse(Restaurant.coordinates)

我得到的结果是:

-31.8125470000000021 149.960394

models.py

class Restaurant(models.Model):
    name = models.CharField(max_length=25, blank=False)
    user = models.ForeignKey(User)
    cuisine = models.ManyToManyField(Cuisine, blank=True)
    address = models.CharField(max_length=50, blank=False)
    coordinates = models.PointField(null=False, blank=False)
    approved = models.BooleanField(default=False)
    objects = models.GeoManager()

    def __str__(self):
        return self.name

views.py

def user_register(request):
if request.user.is_authenticated():
    return redirect('swings:profile')
if request.method == 'POST':
    user_form = UserSignUpForm(request.POST)
    restaurant_form = RestaurantForm(request.POST, request.FILES)
    if user_form.is_valid() and restaurant_form.is_valid():
        user = user_form.save(commit=False)
        user.set_password(user.password)
        user.save()
        profile = restaurant_form.save(commit=False)
        profile.user = user
        profile_address = restaurant_form.cleaned_data['address']
        a, c = geo.geocode(profile_address)
        profile_coordinates = Point(c)
        profile.coordinates = profile_coordinates
        profile.save()
        restaurant_form.save_m2m()
        #cuisines = request.POST['cuisine']
        #profile.cuisine.add(cuisine)
        return redirect('swings:login')
    else:
        return render(request, 'swings/register.html',
                      {'user_form.errors': user_form.errors, 'restaurant_form.errors': restaurant_form.errors})
else:
    user_form = UserSignUpForm()
    restaurant_form = RestaurantForm()
return render(request, 'swings/register.html', {'restaurant_form': restaurant_form, 'user_form': user_form})

forms.py

class RestaurantForm(forms.ModelForm):
class Meta:
    model = Restaurant
    exclude = ['user', 'coordinates']

admin.py和urls.py

from django.contrib.gis import admin
from swings.models import Restaurant

admin.site.register(Restaurant)

from django.contrib.gis import admin

urlpatterns = patterns('',

    url(r'^admin/', include(admin.site.urls)),

希望我问的问题表达得清楚。提前谢谢你们 :)

1 个回答

0

geocode方法返回的是一个列表,里面有(纬度, 经度),但是Point构造函数需要的参数顺序是反过来的:Point(经度, 纬度)。

你调用Restaurant.coordinates时,返回的Point正好是你输入的那样,纬度和经度的顺序反了,这就是为什么看起来没问题,但实际上在地图上却标记了一个随机的、没什么地方的点。

这样做就能解决这个问题:

a, c = geo.geocode('408 Lonsdale street')
print c
(-31.812547, 149.960394)
pnt = Point(c[1], c[0])
print pnt
POINT (149.9603940000000080, -31.8125470000000021)

撰写回答