<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Sealyu

    --- 博客已遷移至: http://www.sealyu.com/blog

      BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
      618 隨筆 :: 87 文章 :: 225 評論 :: 0 Trackbacks

    We’re building a Django application at work, and I had to implement a way for a user to select a location on a map, in order to be able to save the exact coordinates and the zoom level the user chose.
    I looked around to see if something similar had been developed, but I did not find anything so simple and useful as what I later developed, so for the sake of open source-ness I’m sharing it here with you.
    It is implemented as a standard Django form widget so just do as any other Django widget, for example:

    class PersonForm(forms.Form):
    name = forms.CharField()
    website = forms.URLField()
    location = forms.CharField(widget=GMapInput(attrs={'width':'600', 'height':'400'}))

    Here’s the code for the GMapInput class (sorry for the docs in Spanish, but I’m in Mexico):

    from django.forms.widgets import Input
    from django.utils.safestring import mark_safe
     
    class GMapInput(Input):
    """
    Widget para seleccionar un punto en un mapa de Google
    """

     
    def render(self, name, value, attrs=None):
    """
    Atributos extras:
    - width: ancho del mapa en pixeles
    - height: alto del mapa en pixeles
    - center: latitud,longitud del punto central del mapa
    - zoom: zoom inicial del mapa, 1 - 17
    """

     
    final_attrs = self.build_attrs(attrs)
    width = final_attrs['width'] if 'width' in final_attrs else '500'
    height = final_attrs['height'] if 'height' in final_attrs else '300'
    center = final_attrs['center'] if 'center' in final_attrs else '21.983801,-100.964355' # Centro de México
    zoom = final_attrs['zoom'] if 'zoom' in final_attrs else '4' # Zoom amplio, se ve todo un país
     
    widget = u'''<div style="margin-left:7em; padding-left:30px;">
    <input type="hidden" value="%(value)s" name="%(name)s" id="%(id)s" />
    <div id="%(id)s_map" style="width: %(width)spx; height: %(height)spx"></div></div>
    <script type="text/javascript">
    var %(id)s_map = new GMap2(document.getElementById("%(id)s_map"));
    %(id)s_map.addControl(new GLargeMapControl3D());
     
    var %(id)s_marker;
    function %(id)s_updateField() {
    document.getElementById("%(id)s").value = %(id)s_marker.getLatLng().toUrlValue() + "|" + %(id)s_map.getZoom();
    %(id)s_map.panTo(%(id)s_marker.getLatLng(), true);
    }
    '
    '' % { 'value': value, 'name': name, 'id': final_attrs['id'], 'width': width, 'height': height }
     
    if value is None or value == '':
    widget = widget + u'''
    %(id)s_map.setCenter(new GLatLng(%(center)s), %(zoom)s);
    var %(id)s_clickListener = GEvent.addListener(%(id)s_map, "click", function(overlay, latlng) {
    if(latlng) {
    %(id)s_marker = new GMarker(latlng, {draggable: true});
    %(id)s_map.addOverlay(%(id)s_marker);
    %(id)s_updateField();
     
    GEvent.addListener(%(id)s_marker, "dragend", %(id)s_updateField);
    GEvent.addListener(%(id)s_map, "zoomend", %(id)s_updateField);
    GEvent.addListener(%(id)s_map, "dblclick", function (overlay, latlng) { %(id)s_marker.setLatLng(latlng); %(id)s_updateField(); });
    GEvent.removeListener(%(id)s_clickListener);
    }
    });
    </script>'
    '' % { 'id': final_attrs['id'], 'center': center, 'zoom': zoom }
    else:
    values = value.partition('|')
     
    widget = widget + u'''
    %(id)s_map.setCenter(new GLatLng(%(coords)s), %(zoom)s);
    %(id)s_marker = new GMarker(new GLatLng(%(coords)s), {draggable: true});
    %(id)s_map.addOverlay(%(id)s_marker);
     
    GEvent.addListener(%(id)s_marker, "dragend", %(id)s_updateField);
    GEvent.addListener(%(id)s_map, "zoomend", %(id)s_updateField);
    GEvent.addListener(%(id)s_map, "dblclick", function (overlay, latlng) { %(id)s_marker.setLatLng(latlng); %(id)s_updateField(); });
    '
    '' % { 'id': final_attrs['id'], 'coords': values[0], 'zoom': values[2] }
     
    return mark_safe(widget)

    Don’t forget to add the <script> tag linking to the Google Maps API.

    It is yet far from perfect, but I still hope it helps someone out there.

    posted on 2010-05-11 03:42 seal 閱讀(471) 評論(0)  編輯  收藏 所屬分類: Python
    主站蜘蛛池模板: 国产特级淫片免费看| 全部免费毛片在线播放| 夜夜嘿视频免费看| 亚洲av无码专区在线| 在线看片v免费观看视频777| 亚洲an天堂an在线观看| 国内永久免费crm系统z在线| 国产精品国产亚洲精品看不卡| 成人性做爰aaa片免费看| 国产l精品国产亚洲区在线观看| 两个人看的www免费视频中文| 亚洲国产精品无码久久SM| a级毛片在线免费看| 亚洲av永久无码精品漫画| 久久99热精品免费观看牛牛| 亚洲美女一区二区三区| 国产一卡2卡3卡4卡2021免费观看 国产一卡2卡3卡4卡无卡免费视频 | 免费观看四虎精品国产永久| 在线91精品亚洲网站精品成人| 又大又硬又爽免费视频| 国产精品内射视频免费| 亚洲AV综合色一区二区三区| 最近中文字幕高清免费中文字幕mv| 亚洲成aⅴ人在线观看| 在线播放高清国语自产拍免费| 免费无码国产V片在线观看| 亚洲精品色午夜无码专区日韩| 青青草无码免费一二三区| 亚洲va成无码人在线观看| 国产成人青青热久免费精品| 久久久久久久久久免免费精品 | 国产精品xxxx国产喷水亚洲国产精品无码久久一区 | 久久伊人久久亚洲综合| 91成人免费观看网站| 亚洲乱妇老熟女爽到高潮的片| 亚洲国产专区一区| 99久久久国产精品免费牛牛| 亚洲欧好州第一的日产suv| 国产成人麻豆亚洲综合无码精品| 69pao强力打造免费高清| 久久亚洲精品高潮综合色a片|