generic.map 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. {{"""
  2. this is an example of usage of google map
  3. the web2py action should be something like:
  4. def map():
  5. return dict(
  6. googlemap_key='...',
  7. center_latitude = 41.878,
  8. center_longitude = -87.629,
  9. scale = 7,
  10. maker = lambda point: A(row.id,_href='...')
  11. points = db(db.point).select() where a points have latitute and longitude
  12. )
  13. the corresponding views/defaut/map.html should be something like:
  14. \{\{extend 'layout.html'\}\}
  15. <center>\{\{include 'generic.map'\}\}</center>
  16. """}}
  17. <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key={{=googlemap_key}}" type="text/javascript"></script>
  18. <script type="text/javascript">
  19. //<![CDATA[
  20. function load() {
  21. if (GBrowserIsCompatible()) {
  22. var map = new GMap2(document.getElementById("map"));
  23. map.addControl(new GSmallMapControl());
  24. map.addControl(new GMapTypeControl());
  25. map.setCenter(new GLatLng({{=center_latitude}},
  26. {{=center_longitude}}), {{=scale}});
  27. // Create a base icon for all of our markers that specifies the
  28. // shadow, icon dimensions, etc.
  29. var baseIcon = new GIcon();
  30. baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  31. baseIcon.iconSize = new GSize(20, 34);
  32. baseIcon.shadowSize = new GSize(37, 34);
  33. baseIcon.iconAnchor = new GPoint(9, 34);
  34. baseIcon.infoWindowAnchor = new GPoint(9, 2);
  35. baseIcon.infoShadowAnchor = new GPoint(18, 14);
  36. var blueIcon = new GIcon();
  37. blueIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
  38. blueIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  39. blueIcon.iconSize = new GSize(37, 34);
  40. blueIcon.shadowSize = new GSize(37, 34);
  41. blueIcon.iconAnchor = new GPoint(9, 34);
  42. blueIcon.infoWindowAnchor = new GPoint(9, 2);
  43. blueIcon.infoShadowAnchor = new GPoint(18, 14);
  44. function createMarker(point, i, message) {
  45. // Set up our GMarkerOptions object
  46. if(i==0) markerOptions = { icon:blueIcon };
  47. else markerOptions= {}
  48. var marker = new GMarker(point, markerOptions);
  49. GEvent.addListener(marker, "click", function() {
  50. marker.openInfoWindowHtml(message);
  51. });
  52. return marker;
  53. }
  54. {{for point in points:}}{{if point.latitude and point.longitude:}}
  55. var point = new GLatLng({{=point.latitude}},{{=point.longitude}});
  56. map.addOverlay(createMarker(point, 0,
  57. '{{=point.get('map_marker',maker(point))}}'));
  58. {{pass}}{{pass}}
  59. }
  60. }
  61. //]]>
  62. </script>
  63. <div id="map" style="width: 800px; height: 500px"></div>
  64. <script>load();</script>