var merge = require('lodash.assign');
var util = require('./util');

module.exports = projection;

function projection(options) {
  var _gm = util.gm(), _m;

  function position(p) {
    if (p._m) {
      p = p._m.getPosition();
    }
    return _m.fromLatLngToContainerPixel(p);
  }

  function location(x, y) {
    var p = y === undefined ? x : new _gm.Point(x, y);
    return _m.fromContainerPixelToLatLng(p);
  }

  merge(new _gm.OverlayView(), {
    onAdd: function() {
      _m = this.getProjection();
    },
    onRemove: function () {},
    draw: function () {
      options.calculate();
    }
  }).setMap(options.map._m);

  function isReady() {
    return Boolean(_m);
  }

  return {
    position: position,
    location: location,
    isReady: isReady
  };
}
@@ -145,16 +145,25 @@
 il.gm(),
+ overlay,
  _m;%0A%0A  
@@ -181,24 +181,66 @@
 sition(p) %7B%0A
+    if (!isReady()) %7B%0A      return;%0A    %7D%0A
     if (p._m
@@ -365,19 +365,57 @@
     
-var p = y =
+if (!isReady()) %7B%0A      return;%0A    %7D%0A    if (y !
 == u
@@ -426,14 +426,21 @@
 ined
+) %7B%0A    
  
-?
  x 
-:
+=
  new
@@ -457,16 +457,22 @@
 (x, y);%0A
+    %7D%0A
     retu
@@ -504,26 +504,157 @@
 oLatLng(
-p);%0A  %7D%0A%0A 
+x);%0A  %7D%0A%0A  function isReady() %7B%0A    return Boolean(_m);%0A  %7D%0A%0A  function remove() %7B%0A    overlay.setMap();%0A    _m = undefined;%0A  %7D%0A%0A  overlay =
  merge(n
@@ -785,86 +785,61 @@
 aw: 
-function () %7B%0A      options.calculate();%0A    %7D%0A  %7D).setMap(options.map._m);%0A%0A 
+options.calculate ? options.calculate.bind(options) :
  fun
@@ -848,47 +848,53 @@
 ion 
-isReady
 () %7B
+%7D
 %0A  
-  return Boolean(_m);%0A  %7D
+%7D);%0A  overlay.setMap(options.map._m);
 %0A%0A  
@@ -970,15 +970,35 @@
  isReady
+,%0A    remove: remove
 %0A  %7D;%0A%7D

add safeguards to Google map projection