Advertisement
Guest User

Untitled

a guest
Dec 11th, 2012
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * @package BasicGoogleMapsPlacemarks
  3.  * @author Ian Dunn <ian@iandunn.name>
  4.  * @link http://wordpress.org/extend/plugins/basic-google-maps-placemarks/
  5.  */
  6.  
  7.  
  8. /**
  9.  * Wrapper function to safely use $
  10.  * @author Ian Dunn <ian@iandunn.name>
  11.  */
  12. function bgmp_wrapper( $ )
  13. {
  14.     // @todo - figure out if wrapper bad for memory consumption (https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope#Efficiency_considerations)
  15.         // ask on stackoverflow
  16.    
  17.     $.bgmp =
  18.     {
  19.         /**
  20.          * Main entry point
  21.          * @author Ian Dunn <ian@iandunn.name>
  22.          */
  23.         init : function()
  24.         {
  25.             $.bgmp.name                 = 'Basic Google Maps Placemarks';
  26.             $.bgmp.canvas               = document.getElementById( 'bgmp_map-canvas' );     // We have to use getElementById instead of a jQuery selector here in order to pass it to the Maps API.
  27.             $.bgmp.previousInfoWindow   = undefined;
  28.             $.bgmp.map                  = undefined;
  29.             $.bgmp.markerClusterer      = undefined;
  30.             $.bgmp.markers              = [];
  31.            
  32.             // @todo make this loop through array instead of manual. also add any other numbers
  33.             bgmpData.options.zoom                   = parseInt( bgmpData.options.zoom ),
  34.             bgmpData.options.latitude               = parseFloat( bgmpData.options.latitude );
  35.             bgmpData.options.longitude              = parseFloat( bgmpData.options.longitude );
  36.             bgmpData.options.clustering.maxZoom     = parseInt( bgmpData.options.clustering.maxZoom );
  37.             bgmpData.options.clustering.gridSize    = parseInt( bgmpData.options.clustering.gridSize );
  38.                                
  39.             if( $.bgmp.canvas )
  40.                 $.bgmp.buildMap();
  41.             else
  42.                 $( $.bgmp.canvas ).html( $.bgmp.name + " error: couldn't retrieve DOM elements." );
  43.         },
  44.        
  45.         /**
  46.          * Pull in the map options from Wordpress' database and create the map
  47.          * @author Ian Dunn <ian@iandunn.name>
  48.          */
  49.         buildMap : function()
  50.         {
  51.             var mapOptions;
  52.            
  53.             if( bgmpData.options.mapWidth == '' || bgmpData.options.mapHeight == '' || bgmpData.options.latitude == '' || bgmpData.options.longitude == '' || bgmpData.options.zoom == '' || bgmpData.options.infoWindowMaxWidth == '' )
  54.             {
  55.                 // @todo update w/ cluster options?
  56.                
  57.                 $( $.bgmp.canvas ).html( $.bgmp.name + " error: map options not set." );
  58.                 return;
  59.             }
  60.            
  61.             mapOptions =
  62.             {
  63.                 'zoom'                      : bgmpData.options.zoom,
  64.                 'center'                    : new google.maps.LatLng( bgmpData.options.latitude, bgmpData.options.longitude ),
  65.                 'mapTypeId'                 : google.maps.MapTypeId[ bgmpData.options.type ],
  66.                 'mapTypeControl'            : bgmpData.options.typeControl == 'off' ? false : true,
  67.                 'mapTypeControlOptions'     : { style: google.maps.MapTypeControlStyle[ bgmpData.options.typeControl ] },
  68.                 'navigationControl'         : bgmpData.options.navigationControl == 'off' ? false : true,
  69.                 'navigationControlOptions'  : { style: google.maps.NavigationControlStyle[ bgmpData.options.navigationControl ] },
  70.                 'streetViewControl'         : bgmpData.options.streetViewControl
  71.             };
  72.            
  73.             // Override default width/heights from settings
  74.             $( '#bgmp_map-canvas' ).css( 'width', bgmpData.options.mapWidth );      // @todo use $.bgmp.canvas intead of hardcoding it?
  75.             $( '#bgmp_map-canvas' ).css( 'height', bgmpData.options.mapHeight );
  76.             // @todo this prevents users from using their own stylesheet?
  77.            
  78.            
  79.             // Create the map
  80.             try
  81.             {
  82.                 $.bgmp.map = new google.maps.Map( $.bgmp.canvas, mapOptions );
  83.             }
  84.             catch( e )
  85.             {
  86.                 $( $.bgmp.canvas ).html( $.bgmp.name + " error: couln't build map." );
  87.                 if( window.console )
  88.                     console.log( 'bgmp_buildMap: '+ e );
  89.                    
  90.                 return;
  91.             }
  92.            
  93.             $.bgmp.addPlacemarks( $.bgmp.map );     // @todo not supposed to add them when clustering is enabled? http://www.youtube.com/watch?v=Z2VF9uKbQjI
  94.            
  95.             if( bgmpData.options.clustering.enabled )
  96.             {
  97.                 $.bgmp.markerCluster = new MarkerClusterer(     // @todo should be .markerClusterer ?
  98.                     $.bgmp.map,
  99.                     $.bgmp.markers,
  100.                     {
  101.                         maxZoom     : bgmpData.options.clustering.maxZoom,
  102.                         gridSize    : bgmpData.options.clustering.gridSize,
  103.                         styles      : bgmpData.options.clustering.styles[ bgmpData.options.clustering.style ]
  104.                     }
  105.                 );
  106.             }
  107.         },
  108.        
  109.         /**
  110.          * Checks if the value is an integer. Slightly modified version of original.
  111.          * @author Invent Partners
  112.          * @link http://www.inventpartners.com/content/javascript_is_int
  113.          * @param mixed value
  114.          * @return bool
  115.          */
  116.         isInt : function( value )
  117.         {
  118.             if( !isNaN( value ) && parseFloat( value ) == parseInt( value ) )
  119.                 return true;
  120.             else
  121.                 return false;
  122.         },
  123.  
  124.         /**
  125.          * Pull the placemark posts from Wordpress' database and add them to the map
  126.          * @author Ian Dunn <ian@iandunn.name>
  127.          * @param object map Google Maps map
  128.          */
  129.         addPlacemarks : function( map )
  130.         {
  131.             // @todo - should probably refactor this since you pulled out the ajax. update phpdoc too
  132.            
  133.             if( bgmpData.markers.length > 0 )
  134.             {
  135.                 for( var m in bgmpData.markers )
  136.                 {
  137.                     $.bgmp.createMarker(
  138.                         map,
  139.                         bgmpData.markers[ m ][ 'title' ],
  140.                         bgmpData.markers[ m ][ 'latitude' ],
  141.                         bgmpData.markers[ m ][ 'longitude' ],
  142.                         bgmpData.markers[ m ][ 'details' ],
  143.                         bgmpData.markers[ m ][ 'icon' ],
  144.                         parseInt( bgmpData.markers[ m ][ 'zIndex' ] ),
  145.                         bgmpData.markers[ m ][ 'categories' ]
  146.                     );
  147.                 }
  148.             }
  149.         },
  150.  
  151.         /**
  152.          * Create a marker with an information window
  153.          * @author Ian Dunn <ian@iandunn.name>
  154.          * @param object map Google Maps map
  155.          * @param string title Placemark title
  156.          * @param float latituded
  157.          * @param float longitude
  158.          * @param string details Content of the infowinder
  159.          * @param string icon URL of the icon
  160.          * @param int zIndex The desired position in the placemark stacking order
  161.          * @return bool True on success, false on failure
  162.          * @param category array
  163.          */
  164.         createMarker : function( map, title, latitude, longitude, details, icon, zIndex, categories )
  165.         {
  166.             // @todo - clean up variable names
  167.            
  168.             var infowindowcontent, infowindow, marker;
  169.            
  170.             if( isNaN( latitude ) || isNaN( longitude ) )
  171.             {
  172.                 if( window.console )
  173.                     console.log( "bgmp_createMarker(): "+ title +" latitude and longitude weren't valid." );
  174.                    
  175.                 return false;
  176.             }
  177.            
  178.             if( icon == null )
  179.             {
  180.                 // @todo - this check may not be needed anymore
  181.                
  182.                 if( window.console )
  183.                     console.log( "bgmp_createMarker(): "+ title +"  icon wasn't passed in." );
  184.                 return false;
  185.             }
  186.            
  187.             if( !$.bgmp.isInt( zIndex ) )
  188.             {
  189.                 //if( window.console )
  190.                     //console.log( "bgmp_createMarker():  "+ title +" z-index wasn't valid." ); // this would fire any time it's empty
  191.                
  192.                 zIndex = 0;
  193.             }
  194.            
  195.             infowindowcontent = '<div class="bgmp_placemark"> <h3>'+ title +'</h3> <div>'+ details +'</div> </div>';
  196.            
  197.             try
  198.             {
  199.                 infowindow = new google.maps.InfoWindow( {
  200.                     content     : infowindowcontent,
  201.                     maxWidth    : bgmpData.options.infoWindowMaxWidth
  202.                 } );
  203.                
  204.                 // Replace commas with periods. Some (human) languages use commas to delimit the fraction from the whole number, but Google Maps doesn't accept that.
  205.                 latitude = parseFloat( latitude.replace( ',', '.' ) );
  206.                 longitude = parseFloat( longitude.replace( ',', '.' ) );
  207.                
  208.                 marker = new google.maps.Marker( {
  209.                     'position'  : new google.maps.LatLng( latitude, longitude ),
  210.                     'map'       : map,
  211.                     'icon'      : icon,
  212.                     'title'     : title,
  213.                     'zIndex'    : zIndex
  214.                 } );
  215.                
  216.                 for (var s in categories) {
  217.                     title = categories[s]['name'];
  218.                     if (!$.bgmp.markers[title]){
  219.                         //window.alert('$.bgmp.markers['+title+']not Array, creating it');
  220.                         $.bgmp.markers[title] = [];
  221.                     }
  222.                     $.bgmp.markers[title].push(marker);
  223.                 }
  224.                 $.bgmp.markers.push( marker );
  225.                
  226.                 google.maps.event.addListener( marker, 'click', function()
  227.                 {
  228.                     if( $.bgmp.previousInfoWindow != undefined )
  229.                         $.bgmp.previousInfoWindow.close();
  230.                    
  231.                     infowindow.open( map, marker );
  232.                     $.bgmp.previousInfoWindow = infowindow;
  233.                 } );
  234.                
  235.                 return true;
  236.             }
  237.             catch( e )
  238.             {
  239.                 //$( $.bgmp.canvas ).append( '<p>' + $.bgmp.name + " error: couldn't add map placemarks.</p>");     // add class for making red? other places need this too?    // @todo - need to figure out a good way to alert user that placemarks couldn't be added
  240.                 if( window.console )
  241.                     console.log( 'bgmp_createMarker: '+ e );
  242.             }
  243.         }
  244.     }; // end bgmp
  245.    
  246.     // Kick things off...
  247.     $( document ).ready( $.bgmp.init );
  248.    
  249. } // end bgmp_wrapper()
  250.  
  251. bgmp_wrapper( jQuery );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement