[1] | 1 | ## Script (Python) "maps_markers" |
---|
| 2 | ##bind container=container |
---|
| 3 | ##bind context=context |
---|
| 4 | ##bind namespace= |
---|
| 5 | ##bind script=script |
---|
| 6 | ##bind subpath=traverse_subpath |
---|
| 7 | ##parameters= longlat=[], node, controls='nothing', maptype='hybrid', overviewControls=None, typeControls=None, events=False, color='default', zoom=None, loc=(37.4419, -122.1419), mapevents=None, auto='Full' |
---|
| 8 | ##title= |
---|
| 9 | ## |
---|
| 10 | |
---|
| 11 | from Products.qPloneGoogleMaps.utility import processDesc |
---|
| 12 | |
---|
| 13 | lon = [] |
---|
| 14 | lat = [] |
---|
| 15 | sPortal = context.portal_url() |
---|
| 16 | sControls = "" |
---|
| 17 | sMarkerEvents = "" |
---|
| 18 | sMapEvents = "" |
---|
| 19 | sDefaults = "" |
---|
| 20 | sMarkers = "" |
---|
| 21 | sInfoWindow = """ |
---|
| 22 | "<div><h2><a href='"+url+"'>"+title+"</a></h2><p>"+content+"</p></div>" |
---|
| 23 | """ |
---|
| 24 | sMarkerForm = """ |
---|
| 25 | "<input name='markertitle' type='text' value='' /><br /><input name='ok' type='button' value='OK' /><input name='cancel' type='button' value='CANCEL' />" |
---|
| 26 | """ |
---|
| 27 | |
---|
| 28 | if maptype == 'satellite': maptype = 'G_SATELLITE_MAP' |
---|
| 29 | elif maptype == 'map': maptype = 'G_NORMAL_MAP' |
---|
| 30 | else: maptype = 'G_HYBRID_MAP' |
---|
| 31 | |
---|
| 32 | if (not color) and longlat: |
---|
| 33 | lat = [float(e.geoLocation[0]) for el in longlat.values() for e in el] |
---|
| 34 | lon = [float(e.geoLocation[1]) for el in longlat.values() for e in el] |
---|
| 35 | sMarkers = "\n".join([""" |
---|
| 36 | addMarker(%(lt)f, %(lg)f, "%(title)s", "%(url)s/view", "%(desc)s", "%(c)s"); |
---|
| 37 | typeof(overLays["%(id)s"])=='undefined'?overLays["%(id)s"]=[]:''; |
---|
| 38 | overLays["%(id)s"].push([%(lt)f, %(lg)f, "%(title)s", "%(url)s/view", "%(desc)s", "%(c)s"]); |
---|
| 39 | """ % {'lt':float(e.geoLocation[0]), |
---|
| 40 | 'lg':float(e.geoLocation[1]), |
---|
| 41 | 'title':e.Title, |
---|
| 42 | 'url':e.getURL(), |
---|
| 43 | 'desc':processDesc(e.Description), |
---|
| 44 | 'c':el[1], |
---|
| 45 | 'id':el[0]} for el, value in longlat.items() for e in value]) |
---|
| 46 | elif longlat: |
---|
| 47 | lat = [float(el.geoLocation[0]) for el in longlat] |
---|
| 48 | lon = [float(el.geoLocation[1]) for el in longlat] |
---|
| 49 | sMarkers = "\n".join(["""addMarker(%f, %f, "%s", "%s/view", "%s", "%s");""" % (float(el.geoLocation[0]), float(el.geoLocation[1]), el.Title, el.getURL(), processDesc(el.Description), color) for el in longlat]) |
---|
| 50 | |
---|
| 51 | if controls == 'large': sControls += "map1.addControl(new GLargeMapControl());\n" |
---|
| 52 | elif controls == 'small': sControls += "map1.addControl(new GSmallMapControl());\n" |
---|
| 53 | if typeControls: sControls += "map1.addControl(new GMapTypeControl());\n" |
---|
| 54 | if overviewControls: sControls += "map1.addControl(new GOverviewMapControl());\n" |
---|
| 55 | |
---|
| 56 | #if mapevents: |
---|
| 57 | if False: |
---|
| 58 | sMapEvents = """ |
---|
| 59 | var listener = function(marker, point) { |
---|
| 60 | if (!marker){ |
---|
| 61 | var m = new GMarker(point); |
---|
| 62 | map1.addOverlay(m); |
---|
| 63 | m.openInfoWindowHtml(%s); |
---|
| 64 | var ok_buttons = document.getElementsByName('ok'); |
---|
| 65 | var c_buttons = document.getElementsByName('cancel'); |
---|
| 66 | for (var i=0; i < ok_buttons.length; i++) { |
---|
| 67 | ok_buttons[i].addEventListener('click', function(){map1.closeInfoWindow()}, false); |
---|
| 68 | c_buttons[i].addEventListener('click', function(){map1.closeInfoWindow();map1.removeOverlay(m);}, false); |
---|
| 69 | GEvent.addEvent(m, 'infowindowclose', function(){map1.removeOverlay(m);}, false); |
---|
| 70 | }; |
---|
| 71 | //map1.removeOverlay(m); |
---|
| 72 | infowindow = map1.getInfoWindow(); |
---|
| 73 | infowindow.reset(point); |
---|
| 74 | //window.alert(infowindow instanceof null); |
---|
| 75 | //GEvent.addListener(infowindow, 'closeclick', function(){map1.removeOverlay(m)}); |
---|
| 76 | } |
---|
| 77 | }; |
---|
| 78 | GEvent.addListener(map1, 'click', listener);""" % sMarkerForm |
---|
| 79 | |
---|
| 80 | if events: |
---|
| 81 | sMarkerEvents = """ |
---|
| 82 | var f = function(){marker.openInfoWindowHtml(%s, opt);}; |
---|
| 83 | GEvent.addListener(marker, 'click', f);\n""" % sInfoWindow |
---|
| 84 | |
---|
| 85 | if lon and lat and auto != 'None': |
---|
| 86 | sDefaults = """ |
---|
| 87 | var centerPoint = new GLatLng(%f, %f), |
---|
| 88 | autoZoom = map1.getBoundsZoomLevel(new GLatLngBounds(new GLatLng(%f, %f), new GLatLng(%f, %f))); |
---|
| 89 | """ % ((min(lat)+max(lat))/2, (min(lon)+max(lon))/2, min(lat), min(lon), max(lat), max(lon)) |
---|
| 90 | if auto == 'Zoom': sDefaults += """map1.setZoom(autoZoom);""" |
---|
| 91 | elif auto == 'Center': sDefaults += """map1.setCenter(centerPoint);""" |
---|
| 92 | elif auto == 'Full': sDefaults += """map1.setCenter(centerPoint, autoZoom);""" |
---|
| 93 | sDefaults += """map1.savePosition();""" |
---|
| 94 | |
---|
| 95 | if not zoom: zoom = 6 |
---|
| 96 | |
---|
| 97 | return """ |
---|
| 98 | <script type="text/javascript"> |
---|
| 99 | //<![CDATA[ |
---|
| 100 | |
---|
| 101 | function onLoadMap(){ |
---|
| 102 | if (GBrowserIsCompatible()) { |
---|
| 103 | |
---|
| 104 | map1 = new GMap2(document.getElementById('%(node)s')); |
---|
| 105 | %(controls)s |
---|
| 106 | map1.setCenter(new GLatLng(%(lt)f, %(ln)f), %(zoom)s, %(maptype)s); |
---|
| 107 | var opt = map1.getInfoWindow(), overLays = {}; |
---|
| 108 | opt.maxWidth = 400; |
---|
| 109 | |
---|
| 110 | var icon = new GIcon(); |
---|
| 111 | icon.shadow = "%(url)s/images/map_shadow.png"; |
---|
| 112 | icon.iconSize = new GSize(20, 34); |
---|
| 113 | icon.shadowSize = new GSize(37, 34); |
---|
| 114 | icon.iconAnchor = new GPoint(9, 34); |
---|
| 115 | icon.infoWindowAnchor = new GPoint(9, 2); |
---|
| 116 | |
---|
| 117 | function addMarker(lat, lng, title, url, content, col){ |
---|
| 118 | icon.image = "%(url)s/images/"+col+"/map_marker.png"; |
---|
| 119 | var point = new GLatLng(lat, lng); |
---|
| 120 | var marker = new GMarker(point, icon) |
---|
| 121 | map1.addOverlay(marker); |
---|
| 122 | %(event)s |
---|
| 123 | }; |
---|
| 124 | |
---|
| 125 | %(mapevents)s |
---|
| 126 | %(markers)s |
---|
| 127 | %(defaults)s |
---|
| 128 | |
---|
| 129 | if (typeof(getData) == 'function') { |
---|
| 130 | var over_obj = getData(); |
---|
| 131 | if (over_obj) { |
---|
| 132 | for (var i=0; i < over_obj['boxes'].length; i++) registerEventListener(over_obj['boxes'][i], 'click', |
---|
| 133 | function (){ |
---|
| 134 | map1.clearOverlays(); |
---|
| 135 | var bs = getData(); |
---|
| 136 | for (id in bs) { |
---|
| 137 | if (bs[id] == true) { |
---|
| 138 | for (var j=0; j < overLays[id].length; j++) { |
---|
| 139 | var opts = overLays[id][j]; |
---|
| 140 | addMarker(opts[0], opts[1], opts[2], opts[3], opts[4], opts[5]); |
---|
| 141 | }; |
---|
| 142 | } |
---|
| 143 | }; |
---|
| 144 | }); |
---|
| 145 | } |
---|
| 146 | }; |
---|
| 147 | |
---|
| 148 | } |
---|
| 149 | else window.alert("Google maps aren't compatible with current browser or your map api key doesn't match your portal root."); |
---|
| 150 | }; |
---|
| 151 | |
---|
| 152 | registerEventListener(window, 'load', onLoadMap); |
---|
| 153 | registerEventListener(window, 'unload', GUnload); |
---|
| 154 | |
---|
| 155 | //]]> |
---|
| 156 | </script> |
---|
| 157 | """ % { |
---|
| 158 | 'url' : sPortal, |
---|
| 159 | 'node' : node, |
---|
| 160 | 'markers' : sMarkers, |
---|
| 161 | 'defaults' : sDefaults, |
---|
| 162 | 'controls' : sControls, |
---|
| 163 | 'event' : sMarkerEvents, |
---|
| 164 | 'mapevents' : sMapEvents, |
---|
| 165 | 'zoom' : zoom, |
---|
| 166 | 'lt' : float(loc[0]), |
---|
| 167 | 'ln' : float(loc[1]), |
---|
| 168 | 'maptype' : maptype} |
---|