]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
refactoring google map [#254 state:peer-review-requested]
authorAnna Sobiepanek <annaopensource@gmail.com>
Tue, 11 Jan 2011 19:00:48 +0000 (14:00 -0500)
committerAnna Sobiepanek <annaopensource@gmail.com>
Tue, 11 Jan 2011 19:00:48 +0000 (14:00 -0500)
plugins/googleMap/popcorn.googleMap.js

index 33dca4d71e53b1a17d284b3b6fa65fc93313eee4..6693bd145b9847768f9995e152f44d05477b05b6 100644 (file)
@@ -3,7 +3,7 @@ var googleCallback;
 (function (Popcorn) {
   
   /**
-   * googleMap popcorn plug-in 
+   * googlemap popcorn plug-in 
    * Adds a map to the target div centered on the location specified by the user
    * Options parameter will need a start, end, target, type, zoom, lat and long, and location
    * -Start is the time that you want this plug-in to execute
@@ -19,7 +19,7 @@ var googleCallback;
    * 
    * Example:
      var p = Popcorn('#video')
-        .googleMap({
+        .googlemap({
           start: 5, // seconds
           end: 15, // seconds
           type: 'ROADMAP',
@@ -29,7 +29,7 @@ var googleCallback;
         } )
    *
    */
-  Popcorn.plugin( "googleMap" , (function(){
+  Popcorn.plugin( "googlemap" , (function(){
       
     var newdiv, i = 1, _mapFired = false, _mapLoaded = false;
     
@@ -53,6 +53,18 @@ var googleCallback;
         }
       },
       _setup : function( options ) {
+        // create a new div this way anything in the target div 
+        // this is later passed on to the maps api
+        options._newdiv               = document.createElement('div');
+        options._newdiv.id            = "actualmap"+i;
+        options._newdiv.style.width   = "100%";
+        options._newdiv.style.height  = "100%";
+        options._newdiv.style.display = "none";
+        i++;
+        if (document.getElementById(options.target)) {
+          document.getElementById(options.target).appendChild(options._newdiv);
+        }
+        
         // insert google api script once
         if (!_mapFired) {
           _mapFired = true;
@@ -70,35 +82,33 @@ var googleCallback;
         };
         // If there is no lat/long, and there is location, geocode the location
         // you can only do this once google.maps exists
-        // however geocode takes a while so loop this function until lat/long is defined.
         var isGeoReady = function() {
-          if ( !_mapLoaded && !options.lat) {
+          if ( !_mapLoaded) {
             setTimeout(function () {
               isGeoReady();
-            }, 13);
+            }, 5);
           } else {
             if (options.location) {
               var geocoder = new google.maps.Geocoder();
+              // calls an anonymous function called on separate thread
               geocoder.geocode({ 'address': options.location}, function(results, status) {
                 if (status === google.maps.GeocoderStatus.OK) {
+                  alert( "making geolocation");
                   options.lat  = results[0].geometry.location.lat();
                   options.long = results[0].geometry.location.lng(); 
+                  options._location = new google.maps.LatLng(options.lat, options.long);
+                  options._map = new google.maps.Map(options._newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID }); 
                 } 
               });
+            } else {
+              alert("making regular map");
+              options._location = new google.maps.LatLng(options.lat, options.long);
+              options._map = new google.maps.Map(options._newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID });  
             }
           }
         };
         isGeoReady();
-        // create a new div this way anything in the target div
-        // will stay intack 
-        options._newdiv              = document.createElement('div');
-        options._newdiv.id           = "actualmap"+i;
-        options._newdiv.style.width  = "100%";
-        options._newdiv.style.height = "100%";
-        i++;
-        if (document.getElementById(options.target)) {
-          document.getElementById(options.target).appendChild(options._newdiv);
-        }
+        
       },
       /**
        * @member webpage 
@@ -108,23 +118,23 @@ var googleCallback;
        */
       start: function(event, options){
         // dont do anything if the information didn't come back from google map
-        var isReady = function () {
-          
-          if ( !google.maps || !options.lat) {
+        var isReady = function () {    
+          if (!options._map) {
             setTimeout(function () {
               isReady();
             }, 13);
           } else {
-             
-            if(options._map){
-              options._map.getDiv().style.display = 'block';
+            options._map.getDiv().style.display = 'block';
+            // reset the location and zoom just in case the user plaid with the map
+            options._map.setCenter(options._location);
+            if ( options.zoom ) {
+              if ( typeof options.zoom !== "Number" ) {
+                options.zoom = +options.zoom;
+              } 
             } else {
-              var location = new google.maps.LatLng(options.lat, options.long);
-              options._map = new google.maps.Map(options._newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID });      
+              options.zoom = 0;
             }
-            // reset the location and zoom just in case the user plaid with the map
-            options._map.setCenter(location);
-            options._map.setZoom(options.zoom || 0);
+            options._map.setZoom( options.zoom );
           }
         };
         
@@ -148,4 +158,4 @@ var googleCallback;
     
   })());
 
-})( Popcorn );
\ No newline at end of file
+})( Popcorn );