Advertisement
Guest User

Drupalgap App My_Module Map

a guest
Apr 7th, 2016
487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Create global variables to hold coordinates and the map.
  2. var _my_module_user_latitude = null;
  3. var _my_module_user_longitude = null;
  4. var _my_module_map_bounds = null;
  5. var _my_module_map = null;
  6.  
  7. /**
  8.  * Implements hook_menu().
  9.  */
  10. function my_module_menu() {
  11.   try {
  12.     var items = {};
  13.     items['map'] = {
  14.       title: 'Map',
  15.       page_callback: 'my_module_map',
  16.       pageshow: 'my_module_map_pageshow'
  17.     };
  18.     return items;
  19.   }
  20.   catch (error) { console.log('my_module_menu - ' + error); }
  21. }
  22.  
  23. /**
  24.  * The map page callback.
  25.  */
  26. function my_module_map() {
  27.   try {
  28.     var content = {};
  29.     content['my_slider'] = {
  30.       theme: 'range',
  31.       attributes: {
  32.         min: '0',
  33.         max: '100',
  34.         value: '10',
  35.         'data-theme': 'b',
  36.         id: 'my_module_distance',
  37.       }
  38.     };
  39.     content['find_nearby_locations'] = {
  40.       theme: 'button',
  41.       text: 'Find Nearby Locations',
  42.       attributes: {
  43.         onclick: "_my_module_map_button_click()",
  44.         'data-theme': 'b'
  45.       }
  46.     };
  47.     var map_attributes = {
  48.       id: 'my_module_map',
  49.       style: 'width: 100%; height: 320px;'
  50.     };
  51.     content['map'] = {
  52.       markup: '<div ' + drupalgap_attributes(map_attributes) + '></div>'
  53.     };
  54.     content['location_results'] = {
  55.       theme: 'jqm_item_list',
  56.       items: [],
  57.       attributes: {
  58.         id: 'location_results_list'
  59.       }
  60.     };
  61.     return content;
  62.   }
  63.   catch (error) { console.log('my_module_map - ' + error); }
  64. }
  65.  
  66. /**
  67.  * The map pageshow callback.
  68.  */
  69. function my_module_map_pageshow() {
  70.   try {
  71.     navigator.geolocation.getCurrentPosition(
  72.      
  73.       // Success.
  74.       function(position) {
  75.  
  76.         // Set aside the user's position.
  77.         _my_module_user_latitude = position.coords.latitude;
  78.         _my_module_user_longitude = position.coords.longitude;
  79.        
  80.         // Build the lat lng object from the user's position.
  81.         var myLatlng = new google.maps.LatLng(
  82.           _my_module_user_latitude,
  83.           _my_module_user_longitude
  84.         );
  85.        
  86.         // Set the map's options.
  87.         var mapOptions = {
  88.           center: myLatlng,
  89.           zoom: 11,
  90.           mapTypeControl: true,
  91.           mapTypeControlOptions: {
  92.             style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
  93.           },
  94.           zoomControl: true,
  95.           zoomControlOptions: {
  96.             style: google.maps.ZoomControlStyle.SMALL
  97.           }
  98.         };
  99.        
  100.         // Initialize the map, and set a timeout to resize properly.
  101.         _my_module_map = new google.maps.Map(
  102.           document.getElementById("my_module_map"),
  103.           mapOptions
  104.         );
  105.         setTimeout(function() {
  106.             google.maps.event.trigger(_my_module_map, 'resize');
  107.             _my_module_map.setCenter(myLatlng);
  108.         }, 500);
  109.        
  110.         // Add a marker for the user's current position.
  111.         var marker = new google.maps.Marker({
  112.             position: myLatlng,
  113.             map: _my_module_map,
  114.             icon: 'http://maps.google.com/mapfiles/ms/icons/blue-dot.png'
  115.         });
  116.        
  117.        
  118.        
  119.         setTimeout(function() {
  120.             google.maps.event.trigger(_my_module_map, 'resize');
  121.         }, 500);
  122.        
  123.       },
  124.      
  125.       // Error
  126.       function(error) {
  127.        
  128.         // Provide debug information to developer and user.
  129.         console.log(error);
  130.         drupalgap_alert(error.message);
  131.        
  132.         // Process error code.
  133.         switch (error.code) {
  134.  
  135.           // PERMISSION_DENIED
  136.           case 1:
  137.             break;
  138.  
  139.           // POSITION_UNAVAILABLE
  140.           case 2:
  141.             break;
  142.  
  143.           // TIMEOUT
  144.           case 3:
  145.             break;
  146.  
  147.         }
  148.  
  149.       },
  150.      
  151.       // Options
  152.       { enableHighAccuracy: true }
  153.      
  154.      
  155.              
  156.     );
  157.   }
  158.   catch (error) {
  159.     console.log('my_module_map_pageshow - ' + error);
  160.   }
  161. }
  162.  
  163.  
  164. /**
  165.  * The "Find Nearby Locations" click handler.
  166.  */
  167. function _my_module_map_button_click() {
  168.   try {
  169.     // Build the path to the view to retrieve the results.
  170.        
  171.         //clears marker
  172.         marker = [];
  173.    
  174.         // Build the lat lng object from the user's current position.
  175.           _my_module_map_myLatlng = new google.maps.LatLng(
  176.             _my_module_user_latitude,
  177.             _my_module_user_longitude
  178.           );
  179.          
  180.             //adds bounds variable for map center
  181.             //_my_module_map_bounds = new google.maps.LatLngBounds();
  182.    
  183.         //adds bounds variable for map center
  184.         _my_module_map_bounds = new google.maps.LatLngBounds();
  185.        
  186.         //Add your current location to bounds
  187.         _my_module_map_bounds.extend(_my_module_map_myLatlng);
  188.    
  189.    
  190.     var range = 5; // Search within a 5 mile radius, for illustration purposes.
  191.    
  192.     // Get Range
  193.     range = $('input#my_module_distance').val();
  194.    
  195.     var path = 'nearby-locations.json/' +
  196.       _my_module_user_latitude + ',' + _my_module_user_longitude + '_' + range;
  197.      
  198.     // Call the server.
  199.     views_datasource_get_view_result(path, {
  200.         success: function(data) {
  201.          
  202.           if (data.nodes.length == 0) {
  203.             drupalgap_alert('Sorry, we did not find any nearby locations!');
  204.             return;
  205.           }
  206.  
  207.           // Iterate over each spot, add it to the list and place a marker on the map.
  208.           var items = [];
  209.           $.each(data.nodes, function(index, object) {
  210.              
  211.               // Render a nearby location, and add it to the item list.
  212.               var row = object.node;
  213.               var image_html = theme('image', { path: row.field_image.src });
  214.               var distance =
  215.                 '<span class="ui-li-count ui-btn-up-c ui-btn-corner-all" style="top:18px;">' + row.field_geofield_distance + ' ' +
  216.                 drupalgap_format_plural(row.field_geofield_distance, 'mile', 'miles') + ' away</span>';
  217.               var description =
  218.                 '<h2>' + row.title + '</h2>' +
  219.                 '<p>' + row.body + '</p>' +
  220.                 '<p>' + distance + '</p>';
  221.               var link = l(image_html + description, 'node/' + row.nid);
  222.               items.push(link);
  223.              
  224.               // Add a marker on the map for the location.
  225.               var locationLatlng = new google.maps.LatLng(row.lat, row.lon);
  226.               var marker = new google.maps.Marker({
  227.                   position: locationLatlng,
  228.                   map: _my_module_map,
  229.                   data: row,
  230.                   title: row.title,
  231.               });
  232.              
  233.              
  234.              
  235.             //pass each lat long to the bounds extend for centering the map
  236.             _my_module_map_bounds.extend(locationLatlng);
  237.        
  238.                
  239.           });
  240.          
  241.           //Map Center
  242.           _my_module_map.fitBounds(_my_module_map_bounds);
  243.          
  244.           setTimeout(function() {
  245.             google.maps.event.trigger(_my_module_map, 'resize');
  246.           }, 500);
  247.              
  248.          
  249.           drupalgap_item_list_populate("#location_results_list", items);
  250.  
  251.         }
  252.     });
  253.   }
  254.   catch (error) { console.log('_my_module_map_button_click - ' + error); }
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement