Django软盘从(lat,lon)设置PointField,反之亦然

时间:2016-08-29 09:11:55

标签: django google-maps django-floppyforms

我尝试使用软盘进行一些更改:

models.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)

views.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

forms.py:

{% 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 %}

template.html

{{1}}

enter image description here

我对此代码有三个问题:

  1. 默认缩放距离太近...... GMapPointWidget 中的小部件设置 point_zoom 无法执行任何操作。
  2. 正如您在上面的屏幕截图中看到的那样,我尝试用 lat,lon(31.0461,34.8516)将点设置为以色列但是该点放在海洋中......为什么?是否有不同的纬度计算?
  3. 我怎么能做相反的方式,我的意思是我想在地图上设置一个点,并在方框中看到lat,lon数据???

2 个答案:

答案 0 :(得分:1)

我希望它会有所帮助, 根据{{​​3}}

  1. 在html中尝试设置:
  2. {% block options %}
        options['point_zoom'] = 9;
        options['default_zoom'] = 2;
    {% endblock %}
    
    1. 您可以尝试更改窗口小部件属性:&#39; map_srid&#39;到900913(google like lat lon) 默认值为4326 看起来应该是这样的:
    2. class GMapPointWidget(floppyforms.gis.BaseGMapWidget, floppyforms.gis.PointWidget):
          map_srid = 900913  # Use the google projection
      

答案 1 :(得分:1)

您使用

重写汽车
from django.contrib.gis.db.models import PointField
p = Point(
        85.3240, 
        27.7172,
        geography=True, 
        # srid=4326 - default_value
)

https://docs.djangoproject.com/en/2.1/ref/contrib/gis/model-api/#pointfield