Created
July 25, 2014 20:49
-
-
Save DavidSpriggs/06909e4739f8edf16cd7 to your computer and use it in GitHub Desktop.
Reposition infoWindow if out of frame
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_repositionInfoWin: function(graphic) { | |
// Determine the upper right, and center, coordinates of the map | |
var maxPoint = new esri.geometry.Point(this._map.extent.xmax, this._map.extent.ymax, this._map.spatialReference); | |
var centerPoint = new esri.geometry.Point(this._map.extent.getCenter()); | |
// Convert to screen coordinates | |
var maxPointScreen = this._map.toScreen(maxPoint); | |
var centerPointScreen = this._map.toScreen(centerPoint); | |
var graphicPointScreen = this._map.toScreen(graphic.geometry); // Points only | |
// Buffer | |
var marginLR = 10; | |
var marginTop = 3; | |
var infoWin = this._map.infoWindow.domNode.childNodes[0]; | |
var infoWidth = infoWin.clientWidth; | |
var infoHeight = infoWin.clientHeight + this._map.infoWindow.marginTop; | |
// X | |
var lOff = graphicPointScreen.x - infoWidth/2; | |
var rOff = graphicPointScreen.x + infoWidth/2; | |
var l = lOff - marginLR < 0; | |
var r = rOff > maxPointScreen.x - marginLR; | |
if (l) { | |
centerPointScreen.x -= (Math.abs(lOff) + marginLR) < marginLR ? marginLR : Math.abs(lOff) + marginLR; | |
} else if (r) { | |
centerPointScreen.x += (rOff - maxPointScreen.x) + marginLR; | |
} | |
// Y | |
var yOff = this._map.infoWindow.offsetY; | |
var tOff = graphicPointScreen.y - infoHeight - yOff; | |
var t = tOff - marginTop < 0; | |
if (t) { | |
centerPointScreen.y += tOff - marginTop; | |
} | |
//Pan the ap to the new centerpoint | |
if (r || l || t) { | |
centerPoint = this._map.toMap(centerPointScreen); | |
this._map.centerAt(centerPoint); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
taken from: https://github.com/Esri/bootstrap-map-js/blob/master/src/js/bootstrapmap.js#L358