Building a data array to pass into Leaflet to build a map.

Public

Do we need a maps category?

Get raw version
php
  1. <?php
  2.  
  3. function mymodule_load_points() {
  4. // Query all point nodes and join the geofield coordinates field
  5. $query = db_select('node', 'n');
  6. $query->join('field_data_field_coordinates', 'f', 'n.nid = f.entity_id');
  7. $query->fields('n')
  8. ->fields('f', array('field_coordinates_lat', 'field_coordinates_lon'))
  9. ->condition('n.status', 1)
  10. ->condition('n.type', 'point');
  11. $result = $query->execute();
  12.  
  13. $points = array();
  14. while($record = $result->fetchAssoc()) {
  15. $points[] = array(
  16. 'type' => 'point',
  17. 'lat' => $record['field_coordinates_lat'],
  18. 'lon' => $record['field_coordinates_lon'],
  19. 'popup' => l($record['title'], 'node/' . $record['nid']),
  20. 'leaflet_id' => 'point-' . $record['nid'],
  21. 'icon' => array(
  22. 'iconUrl' => '/' . drupal_get_path('module', 'mymodule') . '/img/marker.png',
  23. 'iconSize' => array('x' => 20, 'y' => 30),
  24. 'iconAnchor' => array('x' => 10, 'y' => 30),
  25. 'popupAnchor' => array('x' => 0, 'y' => -35),
  26. ),
  27. );
  28. }
  29. return $points;
  30. }
  31.  
  32.  
  33.  
  34.  
  35. # For large data sets, it will probably help to utilize the canvas circles instead of image icons on the points.
  36.  
  37. $points[$i] = array(
  38. 'type' => 'circle',
  39. 'lat' => $record['field_coordinates_lat'],
  40. 'lon' => $record['field_coordinates_lon'],
  41. 'radius' => 10,
  42. 'leaflet_id' => 'point-' . $i,
  43. 'popup' => l($record['title'], 'node/' . $record['nid']),
  44. 'options' => array(
  45. 'color' => '#f00',
  46. 'stroke' => TRUE,
  47. 'fillColor' => '#f00',
  48. 'weight' => 1,
  49. 'opacity' => .8,
  50. 'fillOpacity' => .8,
  51. 'clickable' => TRUE,
  52. );
  53.  
  54.  
  55.  
  56. /**
  57.  * Implements hook_leaflet_map_info().
  58.  */
  59. function mymodule_leaflet_map_info() {
  60. return array(
  61. 'mymodule' => array(
  62. 'label' => 'mymodule',
  63. 'description' => t('This is the mymodule map style.'),
  64. 'settings' => array(
  65. 'zoom' => 5,
  66. 'minZoom' => 1,
  67. 'maxZoom' => 18,
  68. 'dragging' => TRUE,
  69. 'touchZoom' => TRUE,
  70. 'scrollWheelZoom' => TRUE,
  71. 'doubleClickZoom' => TRUE,
  72. 'zoomControl' => TRUE,
  73. 'attributionControl' => TRUE,
  74. 'trackResize' => TRUE,
  75. 'fadeAnimation' => TRUE,
  76. 'zoomAnimation' => TRUE,
  77. 'closePopupOnClick' => TRUE,
  78. ),
  79. 'attribution' => array(
  80. 'prefix' => '',
  81. ),
  82. 'layers' => array(
  83. 'earth' => array(
  84. 'type' => 'mymodule',
  85. 'urlTemplate' => 'http://{s}.tile.stamen.com/toner/{z}/{x}/{y}.png',
  86. 'options' => array(
  87. 'attribution' => NULL,
  88. 'noWrap' => TRUE,
  89. ),
  90. ),
  91. ),
  92. ),
  93. );
  94. }
  95.  
  96.  
  97. /**
  98.  * Implements hook_menu().
  99.  */
  100. function mymodule_menu() {
  101. $items = array();
  102. $items['foobar'] = array(
  103. 'type' => MENU_NORMAL_ITEM,
  104. 'title' => t('Map'),
  105. 'page callback' => 'mymodule_page',
  106. 'access arguments' => array('view content'),
  107. );
  108. return $items;
  109. }
  110.  
  111. function mymodule_page() {
  112. $points = mymodule_load_points();
  113. if (!empty($points)) {
  114. $styles = module_invoke_all('leaflet_map_info');
  115. return leaflet_render_map($styles['mymodule'], $points, '500px');
  116. }
  117. return t('There are no points associated with this map.');
  118. }