2016-08-29 67 views
1

我試着使用floppyforms有了一些變化:Django的floppyforms從(緯度,經度)設置PointField反之亦然

models.py:

class LocationPoint(models.Model): 
    title = models.CharField(max_length=30, null=False, blank=False) 
    radius = models.DecimalField(max_digits=5, decimal_places=2, null=False, blank=False) 
    point = models.PointField(srid=4326, null=False, blank=False) 
    objects = models.GeoManager() 

views.py:

def location_point_new(request): 
    if request.method == "POST": 
     form = LocationPointForm(request.POST) 
     if form.is_valid(): 
      form.save() 
      return redirect('ui:population-constraint-add') 
    else: 
     form = LocationPointForm() 

    template = 'api/location_point_template.html' 
    context = RequestContext(request, {'form': form}) 
    return render_to_response(template, context) 

forms.py:

class GMapPointWidget(floppyforms.gis.BaseGMapWidget, floppyforms.gis.PointWidget): 
    google_maps_api_key = 'my-key' 


class LocationPointForm(forms.ModelForm): 
    latitude = forms.DecimalField(
     min_value=-90, 
     max_value=90, 
     required=True, 
    ) 
    longitude = forms.DecimalField(
     min_value=-180, 
     max_value=180, 
     required=True, 
    ) 

    class Meta: 
     model = LocationPoint 
     exclude = [] 
     widgets = {'point': GMapPointWidget(attrs={'map_width': 1000, 
               'map_height': 500, 
               'is_point': True, 
               'mouse_position': True, 
               'point_zoom': 1})} 

    def __init__(self, *args, **kwargs): 
     super(LocationPointForm, self).__init__(*args, **kwargs) 
     if args: # If args exist 
      data = args[0] 
      if data['latitude'] and data['longitude']: # If lat/lng exist 
       latitude = float(data['latitude']) 
       longitude = float(data['longitude']) 
       data['point'] = Point(longitude, latitude) # Set PointField 
     try: 
      coordinates = kwargs['instance'].point.tuple # If PointField exists 
      initial = kwargs.get('initial', {}) 
      initial['latitude'] = coordinates[0] # Set Latitude from coordinates 
      initial['longitude'] = coordinates[1] # Set Longitude from coordinates 
      kwargs['initial'] = initial 
     except (KeyError, AttributeError): 
      pass 

template.html

{% extends "base.html" %} 
{% load staticfiles %} 
{% block content %} 
{{ form.media }} 
<div class="container" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html"> 
    <div class="page-header"> 
     <h1>Location Point</h1> 
    </div> 
    <form role="form" class="form-inline" action="" method="post" enctype="multipart/form-data" novalidate> 
     {% csrf_token %} 
     <div class="dl-horizontal"> 
      <a href="{% url 'ui:population-constraint-add' %}"> 
       <button type="button" class="btn btn-info">Back</button> 
      </a> 
     </div> 
     <br/> 
     <br/> 
     {{ form.as_p }} 
     <div class="dl-horizontal"> 
      <button type="submit" class="btn btn-success">Save</button> 
     </div> 
     <br/> 
     <br/> 
    </form> 
</div> 
{% endblock %} 

enter image description here

我有三個問題與此代碼:

  1. 默認縮放是太接近......小部件設置point_zoomGMapPointWidget犯規做任何事情。
  2. 正如你在截圖中看到上面我想點設置爲以色列緯度,經度(31.0461,34.8516)但點放置在大海......爲什麼?是否有不同的經緯度計算?
  3. 我該如何做另一種方式,我的意思是我想在地圖上設置一個點,並在框中查看緯度,經度數據?

回答

1

我希望這會幫助, 根據floppyforms docs

    在HTML
  1. 嘗試設置:
{% block options %} 
    options['point_zoom'] = 9; 
    options['default_zoom'] = 2; 
{% endblock %} 
  • 您可以嘗試將Widget屬性:'map_srid'更改爲900913(谷歌喜歡緯度) 默認爲4326 它看起來應該財產以後這樣的:
  • class GMapPointWidget(floppyforms.gis.BaseGMapWidget, floppyforms.gis.PointWidget): 
        map_srid = 900913 # Use the google projection 
    
    +0

    不工作。完全沒有效果...... :( – Rada