views:

2895

answers:

3

I know that method exists and is documented, but I don't know how to get an MapCanvasProjection object.

+4  A: 

To get a MapCanvasProjection you could derive a class from OverlayView and call the getProjection() method which returns a MapCanvasProjection type

onAdd(), draw() and onRemove() must be implemented to derive from OverlayView.

function MyOverlay(options) {
    this.setValues(options);

    var div = this.div_= document.createElement('div');

    div.className = "overlay";
};

// MyOverlay is derived from google.maps.OverlayView
MyOverlay.prototype = new google.maps.OverlayView;

MyOverlay.prototype.onAdd = function() {

    var pane = this.getPanes().overlayLayer;
    pane.appendChild(this.div_);

}

MyOverlay.prototype.onRemove = function() {
    this.div_.parentNode.removeChild(this.div_);
}

MyOverlay.prototype.draw = function() {
    var projection = this.getProjection();
    var position = projection.fromLatLngToDivPixel(this.getMap().getCenter());

    var div = this.div_;
    div.style.left = position.x + 'px';
    div.style.top = position.y + 'px';
    div.style.display = 'block';
};

then when you're creating your map

var OverLayMap = new MyOverlay( { map: map } );

For V2 you should be able to call fromLatLngToDivPixel from your GMap2 instance

var centerPoint = map.fromLatLngToDivPixel(map.getCenter());
Michael G
aren't you mixing the API v2 with v3?
Jader Dias
These are from V3
Michael G
Now with your last edit, it's ok
Jader Dias
Sorry, it must not have taken a previous edit. :)
Michael G
I think this is a great problem with the new API. It should have an easier way to do that
Jader Dias
+4  A: 
var map;
// Create your map
MyOverlay.prototype = new google.maps.OverlayView();
MyOverlay.prototype.onAdd = function() { }
MyOverlay.prototype.onRemove = function() { }
MyOverlay.prototype.draw = function() { }
function MyOverlay(map) { this.setMap(map); }
var overlay = new MyOverlay(map);
var projection = overlay.getProjection();
Ralph Stevens
A: 

The second example by Ralph works perfectly. thanks :-)

PedroMorgan