简体   繁体   English

geodjango管理地图未显示坐标点,尽管该点显然已正确保存

[英]geodjango admin map not displaying coordinate point, point is apparently saved correctly though

so I am building a program that requires me to geocode a users address and then store it in geodjango pointfield. 因此,我正在构建一个程序,要求我对用户地址进行地理编码,然后将其存储在geodjango点域中。 So I have everything setup and this is my code. 所以我已完成所有设置,这是我的代码。 In my models file I have my coordinates field which is a Pointfield. 在我的模型文件中,我有一个坐标字段,即一个Pointfield。 However on the admin page, the map displays for the coordinates field but no point is shown. 但是,在管理页面上,地图显示为坐标字段,但未显示任何点。 Does this mean there is an issue and things such as distance queries wont work properly? 这是否意味着存在问题,并且诸如距离查询之类的内容无法正常工作? All it shows is a map with no point zoomed into the middle of no where. 它显示的只是一张没有点放大到无处中间的地图。

The way I convert the address to coordinates is by using something called geopy. 我将地址转换为坐标的方法是使用称为geopy的东西。 You can see in my views when saving my restaurant form, I covert the address field using the geo function: 您可以在保存餐厅表单时在视图中看到,我使用geo函数隐蔽了地址字段:

a, c = geo.geocode(profile_address)

For example: 例如:

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

I tried other methods to covert the address to a coordinate and save it to the pointfield but it kept returning errors saying I cannot save that type of data. 我尝试了其他方法将地址隐式转换为坐标并将其保存到点域,但是它不断返回错误,提示我无法保存该类型的数据。 So I am assuming now that it saves fine, that means it should be working fine. 因此,我现在假设它可以节省罚款,这意味着它应该可以正常工作。

In my views, if I do 我认为,如果这样做

return HttpResponse(Restaurant.coordinates)

I get 我懂了

-31.8125470000000021 149.960394

models.py 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 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 and urls.py 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)),

Hope I wordered my question properly. 希望我正确回答我的问题。 Thanks for in advance :) 预先感谢:)

The geocode method is returning the list: (latitude, longitude), but the Point constructor takes those parameters in reverse order: Point(longitude, latitude) 地理编码方法返回列表:(纬度,经度),但是Point构造函数采用相反的顺序获取这些参数:Point(经度,纬度)

The call to Restaurant.coordinates is returning the Point exactly as you entered it, with the lat/long reversed, which is why it looks correct, but is plotting a random point in the middle of nowhere on the map. Restaurant.coordinates的调用将返回与您输入的点完全相同的点,但纬度/经度反转了,这就是为什么它看起来正确的原因,但却在地图上虚无处绘制了一个随机点。

This will do the trick: 这将达到目的:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM