]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#336] simplify googlemap plugin
authorDaniel Brooks <db48x@db48x.net>
Sat, 19 Feb 2011 19:07:41 +0000 (13:07 -0600)
committerDaniel Brooks <db48x@db48x.net>
Sat, 19 Feb 2011 19:07:41 +0000 (13:07 -0600)
plugins/googlemap/popcorn.googlemap.js

index 84cf031ead2a53a134bb86ae2993616622b3cf7b..3c765230a35f67b27c40dfddf6fabb773eb317cf 100644 (file)
@@ -1,7 +1,7 @@
 // PLUGIN: Google Maps
 var googleCallback;
 (function (Popcorn) {
-  
+
   /**
    * googlemap popcorn plug-in
    * Adds a map to the target div centered on the location specified by the user
@@ -9,7 +9,7 @@ var googleCallback;
    * -Start is the time that you want this plug-in to execute
    * -End is the time that you want this plug-in to stop executing
    * -Target is the id of the DOM element that you want the map to appear in. This element must be in the DOM
-   * -Type [optional] either: HYBRID (default), ROADMAP, SATELLITE, TERRAIN, STREETVIEW 
+   * -Type [optional] either: HYBRID (default), ROADMAP, SATELLITE, TERRAIN, STREETVIEW
    * -Zoom [optional] defaults to 0
    * -Heading [optional] STREETVIEW orientation of camera in degrees relative to true north (0 north, 90 true east, ect)
    * -Pitch [optional] STREETVIEW vertical orientation of the camera (between 1 and 3 is recommended)
@@ -31,88 +31,70 @@ var googleCallback;
     } )
   *
   */
-  Popcorn.plugin( "googlemap" , (function(){
-      
-    var newdiv, i = 1, _mapFired = false, _mapLoaded = false;
-    
-    return {
-      manifest: {
-        about:{
-          name: "Popcorn Google Map Plugin",
-          version: "0.1",
-          author: "@annasob",
-          website: "annasob.wordpress.com"
-        },
-        options:{
-          start    : {elem:"input", type:"text", label:"In"},
-          end      : {elem:"input", type:"text", label:"Out"},
-          target   : "map-container",
-          type     : {elem:"select", options:["ROADMAP","SATELLITE", "STREETVIEW", "HYBRID", "TERRAIN"], label:"Type"},
-          zoom     : {elem:"input", type:"text", label:"Zoom"},
-          lat      : {elem:"input", type:"text", label:"Lat"},
-          lng      : {elem:"input", type:"text", label:"Lng"},
-          location : {elem:"input", type:"text", label:"Location"},
-          heading  : {elem:"input", type:"text", label:"Heading"},
-          pitch    : {elem:"input", type:"text", label:"Pitch"}
-        }
-      },
-      _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%";
-        i++;
-        if (document.getElementById(options.target)) {
-          document.getElementById(options.target).appendChild(options._newdiv);
-        }
-        
-        // insert google api script once
-        if (!_mapFired) {
-          _mapFired = true;
-          var loadScriptTime = (new Date).getTime();
-          var head = document.getElementsByTagName("head")[0];
-          var script = document.createElement("script");
-         
-          script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=googleCallback";
-          script.type = "text/javascript";
-          head.insertBefore( script, head.firstChild );
-        }
-        // callback function fires when the script is run
-        googleCallback = function() {
-          _mapLoaded = true;
-        };
-        // If there is no lat/lng, and there is location, geocode the location
-        // you can only do this once google.maps exists
-        var isGeoReady = function() {
-          if ( !_mapLoaded ) {
-            setTimeout(function () {
-              isGeoReady();
-            }, 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) {
-                  options.lat = results[0].geometry.location.lat();
-                  options.lng = results[0].geometry.location.lng();
-                  options._location = new google.maps.LatLng(options.lat, options.lng);
-                  options._map = new google.maps.Map(options._newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID });
-                  options._map.getDiv().style.display = "none";
-                }
-              });
-            } else {
-              options._location = new google.maps.LatLng(options.lat, options.lng);
-              options._map = new google.maps.Map(options._newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID });
-              options._map.getDiv().style.display = "none";
+
+  var newdiv, i = 1, _mapFired = false, _mapLoaded = false;
+
+  // callback function fires when the script is run
+  googleCallback = function() {
+    _mapLoaded = true;
+  };
+  // insert google api script once
+  if (!_mapFired) {
+    _mapFired = true;
+    var loadScriptTime = (new Date).getTime();
+    var head = document.getElementsByTagName("head")[0];
+    var script = document.createElement("script");
+
+    script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=googleCallback";
+    script.type = "text/javascript";
+    head.insertBefore( script, head.firstChild );
+  }
+
+
+  Popcorn.plugin( "googlemap" , function( options ) {
+    var newdiv, map, location;
+
+    // create a new div this way anything in the target div
+    // this is later passed on to the maps api
+    newdiv = document.createElement("div");
+    newdiv.id = "actualmap"+i;
+    newdiv.style.width = "100%";
+    newdiv.style.height = "100%";
+    i++;
+    if (document.getElementById(options.target)) {
+      document.getElementById(options.target).appendChild(newdiv);
+    }
+
+    // If there is no lat/lng, and there is location, geocode the location
+    // you can only do this once google.maps exists
+    var isGeoReady = function() {
+      if ( !_mapLoaded ) {
+        setTimeout(function () {
+          isGeoReady();
+        }, 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) {
+              options.lat = results[0].geometry.location.lat();
+              options.lng = results[0].geometry.location.lng();
+              location = new google.maps.LatLng(options.lat, options.lng);
+              map = new google.maps.Map(newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID });
+              map.getDiv().style.display = "none";
             }
-          }
-        };
-        isGeoReady();
-        
-      },
+          });
+        } else {
+          location = new google.maps.LatLng(options.lat, options.lng);
+          map = new google.maps.Map(newdiv, {mapTypeId: google.maps.MapTypeId[options.type] || google.maps.MapTypeId.HYBRID });
+          map.getDiv().style.display = "none";
+        }
+      }
+    };
+    isGeoReady();
+
+    return {
       /**
        * @member webpage
        * The start function will be executed when the currentTime
@@ -122,23 +104,23 @@ var googleCallback;
       start: function(event, options){
         // dont do anything if the information didn't come back from google map
         var isReady = function () {
-          if (!options._map) {
+          if (!map) {
             setTimeout(function () {
               isReady();
             }, 13);
           } else {
-            options._map.getDiv().style.display = "block";
+            map.getDiv().style.display = "block";
             // reset the location and zoom just in case the user plaid with the map
-            options._map.setCenter(options._location);
+            map.setCenter(location);
 
             // make sure options.zoom is a number
             if ( options.zoom && typeof options.zoom !== "number" ) {
               options.zoom = +options.zoom;
             }
-            
+
             options.zoom = options.zoom || 0; // default to 0
-            options._map.setZoom( options.zoom );
-            
+            map.setZoom( options.zoom );
+
             //Make sure heading is a number
             if ( options.heading && typeof options.heading !== "number" ) {
               options.heading = +options.heading;
@@ -148,25 +130,25 @@ var googleCallback;
               options.pitch = +options.pitch;
             }
 
-            google.maps.event.trigger(options._map, 'resize');
-            
+            google.maps.event.trigger(map, 'resize');
+
             if ( options.type === "STREETVIEW" ) {
               // Switch this map into streeview mode
-              options._map.setStreetView( 
+              map.setStreetView(
                 // Pass a new StreetViewPanorama instance into our map
-                new google.maps.StreetViewPanorama( options._newdiv, {
-                  position: options._location,
+                new google.maps.StreetViewPanorama( newdiv, {
+                  position: location,
                   pov: {
                     heading: options.heading = options.heading || 0,
                     pitch: options.pitch = options.pitch || 0,
                     zoom: options.zoom
-                  }    
+                  }
                 })
               );
             }
           }
         };
-        
+
         isReady();
       },
       /**
@@ -178,13 +160,30 @@ var googleCallback;
       end: function(event, options){
         // if the map exists hide it do not delete the map just in
         // case the user seeks back to time b/w start and end
-        if (options._map) {
-          options._map.getDiv().style.display = "none";
+        if (map) {
+          map.getDiv().style.display = "none";
         }
       }
-      
     };
-    
-  })());
-
+  },
+  {
+    about: {
+      name: "Popcorn Google Map Plugin",
+      version: "0.1",
+      author: "@annasob",
+      website: "annasob.wordpress.com"
+    },
+    options: {
+      start    : {elem:"input", type:"text", label:"In"},
+      end      : {elem:"input", type:"text", label:"Out"},
+      target   : "map-container",
+      type     : {elem:"select", options:["ROADMAP","SATELLITE", "STREETVIEW", "HYBRID", "TERRAIN"], label:"Type"},
+      zoom     : {elem:"input", type:"text", label:"Zoom"},
+      lat      : {elem:"input", type:"text", label:"Lat"},
+      lng      : {elem:"input", type:"text", label:"Lng"},
+      location : {elem:"input", type:"text", label:"Location"},
+      heading  : {elem:"input", type:"text", label:"Heading"},
+      pitch    : {elem:"input", type:"text", label:"Pitch"}
+    }
+  });
 })( Popcorn );