Package muntjac :: Package addon :: Package google_maps :: Package overlay :: Module basic_marker_source
[hide private]
[frames] | no frames]

Source Code for Module muntjac.addon.google_maps.overlay.basic_marker_source

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  try: 
 17      from cStringIO import StringIO 
 18  except ImportError: 
 19      from StringIO import StringIO 
 20   
 21  from muntjac.addon.google_maps.overlay.marker_source \ 
 22      import IMarkerSource 
 23   
 24   
25 -class BasicMarkerSource(IMarkerSource):
26
27 - def __init__(self):
28 super(BasicMarkerSource, self).__init__() 29 self._markers = list()
30 31
32 - def getMarkers(self):
33 return self._markers
34 35
36 - def addMarker(self, newMarker):
37 if newMarker in self._markers: 38 return False 39 self._markers.append(newMarker) 40 return True
41 42
43 - def getMarkerJSON(self):
44 markerJSON = StringIO() 45 46 for i, marker in enumerate(self._markers): 47 markerJSON.write('{\"mid\":\"') 48 markerJSON.write(str(marker.getId())) 49 50 markerJSON.write('\",\"lat\":') 51 markerJSON.write(str(marker.getLatLng()[1])) 52 53 markerJSON.write(',\"lng\":') 54 markerJSON.write(str(marker.getLatLng()[0])) 55 56 # Escape single and double quotes 57 markerJSON.write(',\"title\":\"') 58 markerJSON.write(marker.getTitle().replace('\'', "\\'").replace('\"', '\\\\\"')) 59 60 markerJSON.write('\",\"visible\":') 61 markerJSON.write('true' if marker.isVisible() else 'false') 62 63 markerJSON.write(',\"info\":') 64 markerJSON.write('true' if marker.getInfoWindowContent() is not None else 'false') 65 66 markerJSON.write(',\"draggable\":') 67 markerJSON.write('true' if marker.isDraggable() else 'false') 68 69 if marker.getIconUrl() is not None: 70 markerJSON.write(',\"icon\":\"') 71 markerJSON.write(marker.getIconUrl() + '\"') 72 73 if marker.getIconAnchor() is not None: 74 markerJSON.write(',\"iconAnchorX\":') 75 markerJSON.write(str(marker.getIconAnchor()[0])) 76 77 markerJSON.write(',\"iconAnchorY\":') 78 markerJSON.write(str(marker.getIconAnchor()[1])) 79 else: 80 markerJSON.write(',\"iconAnchorX\":') 81 markerJSON.write(str(marker.getLatLng()[0])) 82 83 markerJSON.write(',\"iconAnchorY\":') 84 markerJSON.write(str(marker.getLatLng()[1])) 85 86 markerJSON.write('}') 87 88 if i != len(self._markers) - 1: 89 markerJSON.write(',') 90 91 try: 92 json = ('[' + markerJSON.getvalue() + ']').encode('utf-8') 93 except Exception: 94 json = ('[' + markerJSON.getvalue() + ']').encode() 95 96 markerJSON.close() 97 98 return json
99 100
101 - def registerEvents(self, map_):
102 # This marker source implementation is not interested in map events 103 pass
104 105
106 - def getMarker(self, markerId):
107 # TODO: The marker collection should be a map... 108 for marker in self._markers: 109 if str(marker.getId()) == markerId: 110 return marker 111 return None
112