fromLatLngToDivPixel showing strange results

246 views
Skip to first unread message

Johnny

unread,
Jan 27, 2012, 10:58:44 AM1/27/12
to Google Maps JavaScript API v3
hi,

I made a new class based on the custom overlay example in the docs.
Basically all it does is display a div on the map. The problem is that
when I position it with fromLatLngToDivPixel I get things like
-38198.38138981233 for position.y. This has only started in the past
few days, and everything was working fine before. AFAIK I haven't
changed anything, so this is pretty weird. Does anyone have any
insights on this?

thanks,
Johnny.

p.s. Here's the code:

function DivOverlay(div, map, position) {

// Now initialize all properties.
this.div_ = div;
this.map_ = map;
this.position_ = position;

// Explicitly call setMap() on this overlay
this.setMap(map);
}

DivOverlay.prototype = new google.maps.OverlayView();

DivOverlay.prototype.onAdd = function() {

// Note: an overlay's receipt of onAdd() indicates that
// the map's panes are now available for attaching
// the overlay to the map via the DOM.

// We add an overlay to a map via one of the map's panes.
// We'll add this overlay to the overlayImage pane.
var panes = this.getPanes();
panes.overlayLayer.style['zIndex'] = 1000;
panes.overlayLayer.appendChild(this.div_);
}

DivOverlay.prototype.draw = function() {

// Size and position the overlay. We use a southwest and northeast
// position of the overlay to peg it to the correct position and
size.
// We need to retrieve the projection from this overlay to do this.
var overlayProjection = this.getProjection();

// Retrieve the southwest and northeast coordinates of this overlay
// in latlngs and convert them to pixels coordinates.
// We'll use these coordinates to resize the DIV.
var position =
overlayProjection.fromLatLngToDivPixel(this.position_);

// Position the div
var div = this.div_;
var horizontalOffset = 10;
var verticalOffset = div.clientHeight + 10;
div.style.position = 'absolute';
if ((position.x + this.div_.clientWidth + horizontalOffset) >
this.map_.getDiv().clientWidth) {
div.style.left = (this.map_.getDiv().clientWidth -
this.div_.clientWidth - 20) + 'px';
} else if (position.x < this.div_.clientWidth) {
div.style.left = 10 + 'px';
} else {
div.style.left = position.x + horizontalOffset + 'px';
}
if ((position.y - verticalOffset) > this.map_.getDiv().clientHeight)
{
div.style.top = (this.map_.getDiv().clientHeight - verticalOffset) +
'px';
} else if (position.y - verticalOffset < 0) {
div.style.top = position.y + 10 + 'px';
} else {
div.style.top = position.y - verticalOffset + 'px';
}
}

DivOverlay.prototype.onRemove = function() {
this.div_ = null;
}

// Note that the visibility property must be a string enclosed in
quotes
DivOverlay.prototype.hide = function() {
if (this.div_) {
this.div_.style.visibility = "hidden";
}
}

DivOverlay.prototype.show = function() {
if (this.div_) {
this.div_.style.visibility = "visible";
}
}

DivOverlay.prototype.toggle = function() {
if (this.div_) {
if (this.div_.style.visibility == "hidden") {
this.show();
} else {
this.hide();
}
}
}

DivOverlay.prototype.getDiv = function() {
if (this.div_) {
return this.div_;
}
}

DivOverlay.prototype.destroy = function() {
if (this.getMap()) {
this.setMap(null);
}
}

Johnny

unread,
Jan 27, 2012, 7:18:23 PM1/27/12
to Google Maps JavaScript API v3
This appears to be a bug in the latest version of the maps API since
setting v=3.4 when loading the maps JS solves the problem.

Luke Mahé

unread,
Jan 27, 2012, 7:45:29 PM1/27/12
to google-map...@googlegroups.com
Are you able to include a link to your demo?

-- Luke



--
You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group.
To post to this group, send email to google-map...@googlegroups.com.
To unsubscribe from this group, send email to google-maps-js-a...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.


Johnny

unread,
Jan 28, 2012, 4:32:08 PM1/28/12
to Google Maps JavaScript API v3
That might be a bit complicated since it's in a Facebook application
I'm developing and, as I said, I fixed it by loading v3.4. If you want
I can revert for a few minutes. Please email me if you're interested.

Rossko

unread,
Jan 28, 2012, 4:42:46 PM1/28/12
to Google Maps JavaScript API v3
> I'm developing and, as I said, I fixed it by loading v3.4

Not a fix, but a delaying tactic
v3.4 will go away one day
Reply all
Reply to author
Forward
0 new messages