Repository: yagoferrer/map-tools
Pull request #205 (2015-04-02 20:41:30)

Yago/set center


Patch:
diff --git a/README.md b/README.md
index 7f204ed..3f55b36 100644
--- a/README.md
+++ b/README.md
@@ -90,6 +90,13 @@ Update any option by calling the updateMap method like this example:
 map.updateMap({zoom: 6, type: 'TERRAIN'});
 ```
 
+### Center Map
+Call this method to center the Map. If you don't pass any coordinates it will use the initial values set to the Map
+```javascript
+map.center(41.3833, 2.1833);
+```
+
+
 Once instantiated: you can access directly to the Google API like this: `map.instance`
 
 ## Markers
diff --git a/examples/code.snippet/update.map.snippet.html b/examples/code.snippet/update.map.snippet.html
index 08f8185..b386c12 100644
--- a/examples/code.snippet/update.map.snippet.html
+++ b/examples/code.snippet/update.map.snippet.html
@@ -16,4 +16,7 @@
 			streetViewControl: true,
 			panControl: true
 	});
+
+  // Center Map
+  map.center(40.416854, -3.703419);
 </code></pre></div>
diff --git a/examples/update.map.html b/examples/update.map.html
index a7fc9c0..43add8c 100644
--- a/examples/update.map.html
+++ b/examples/update.map.html
@@ -21,6 +21,7 @@
 		  template: '<div class="actions">' +
 		  '<div class="update-terrain">Update to type TERRAIN and disable controls</div>' +
 		  '<div class="update-roadmap">Update to type ROADMAP and enable controls</div>' +
+      '<div class="center-map">Center Map</div>' +
 		  '</div>',
 		  position: 'right center',
 		  events: {
@@ -45,7 +46,12 @@
 					  streetViewControl: true,
 					  panControl: true
 				  });
-			  }
+			  },
+
+        '.center-map click': function(e) {
+          e.preventDefault();
+          map.center(40.416854, -3.703419);
+        }
 		  }
 	  });
 
diff --git a/lib/map-tools/center.js b/lib/map-tools/center.js
new file mode 100644
index 0000000..4f13156
--- /dev/null
+++ b/lib/map-tools/center.js
@@ -0,0 +1,17 @@
+/*jslint node: true */
+"use strict";
+module.exports = function (global, that) {
+
+  function center(lat, lng) {
+    var position;
+    if (lat && lng) {
+      position = new global.google.maps.LatLng(lat, lng);
+    } else {
+      position = new global.google.maps.LatLng(that.options.lat, that.options.lng);
+    }
+
+    that.instance.setCenter(position);
+  }
+
+  return center;
+};
diff --git a/lib/map-tools/index.js b/lib/map-tools/index.js
index 7641ae5..6a9e206 100644
--- a/lib/map-tools/index.js
+++ b/lib/map-tools/index.js
@@ -22,6 +22,7 @@ module.exports = function (global) {
     this.addGroup = require('map-tools/groups')(global, that).addGroup;
     this.updateGroup = require('map-tools/groups')(global, that).updateGroup;
     this.updateMap = require('map-tools/updateMap')(global, that);
+    this.center = require('map-tools/center')(global, that);
 
     var map = require('map-tools/addMap')(global, that);
 
diff --git a/lib/map-tools/infoWindow.js b/lib/map-tools/infoWindow.js
index e2dc568..307e89e 100644
--- a/lib/map-tools/infoWindow.js
+++ b/lib/map-tools/infoWindow.js
@@ -5,11 +5,9 @@ module.exports = function (global) {
   function create(marker, options, map) {
     var event = options.event || 'click';
 
-    if (options.content) {
-      options.content = options.content.replace(/\{(\w+)\}/g, function (m, variable) {
-        return marker.data[variable] || '';
-      });
-    }
+    options.content = options.content.replace(/\{(\w+)\}/g, function (m, variable) {
+      return marker.data[variable] || '';
+    });
 
     marker.infoWindow.instance = new global.google.maps.InfoWindow(options);
 


Test patch:
diff --git a/test/unit/center.Spec.js b/test/unit/center.Spec.js
new file mode 100644
index 0000000..f60389c
--- /dev/null
+++ b/test/unit/center.Spec.js
@@ -0,0 +1,43 @@
+describe('Given the setCenter Class', function () {
+
+
+  var center, spy;
+
+  beforeEach(function () {
+    var global = {
+      google: {
+        maps: {
+          LatLng: function(a,b) { return {lat: a, lng: b}}
+        }
+      }
+    };
+
+
+    spy = sinon.spy();
+
+    var that = {
+        instance: {
+          setCenter: spy
+        },
+      options: {
+        lat: 1,
+        lng: 2
+      }
+    };
+
+    center = require('map-tools/center')(global, that);
+  });
+
+  it('should center the Map given coordinates', function () {
+    center(40.416854, -3.703419);
+    expect(spy).to.have.been.calledWith({ lat: 40.416854, lng: -3.703419 });
+  });
+
+
+  it('should center the Map using initial coordinates', function() {
+    center();
+    expect(spy).to.have.been.calledWith({ lat: 1, lng: 2 });
+  });
+
+
+});