Created
November 16, 2012 09:16
-
-
Save amytych/4085779 to your computer and use it in GitHub Desktop.
Calculate shifted area of google map
This file contains 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
/** | |
* Calculate bounds for the whole map or just the shifted area | |
* @param {Boolean} reset Force calculation for the whole map | |
* @return {Array} Array of map bounds arrays | |
*/ | |
function calculateNewBounds(reset) { | |
var curSW = currentBounds.getSouthWest(), | |
curNE = currentBounds.getNorthEast(), | |
curNW = new google.maps.LatLng(curNE.lat(), curSW.lng()), | |
curSE = new google.maps.LatLng(curSW.lat(), curNE.lng()), | |
preSW = previousBounds.getSouthWest(), | |
preNE = previousBounds.getNorthEast(), | |
preNW = new google.maps.LatLng(preNE.lat(), preSW.lng()), | |
preSE = new google.maps.LatLng(preSW.lat(), preNE.lng()), | |
shiftSE = currentBounds.contains(preNW), | |
shiftSW = currentBounds.contains(preNE), | |
shiftNE = currentBounds.contains(preSW), | |
shiftNW = currentBounds.contains(preSE), | |
shiftN = shiftNE && shiftNW, | |
shiftS = shiftSE && shiftSW, | |
shiftW = shiftNE && shiftSE, | |
shiftE = shiftNW && shiftSW, | |
roundNum = Utils.roundNum; | |
// Initial request, zoom in or zoom out, forced reset | |
// We need bounds for whole map | |
if (reset || | |
(shiftNE && shiftSE && shiftNW && shiftSW) || | |
(!shiftNE && !shiftSE && !shiftNW && !shiftSW) || | |
(curSW.lng() > curNE.lng())) { | |
return [ | |
[roundNum(curSW.lat(), 6), roundNum(curSW.lng(), 6)], | |
[roundNum(curNE.lat(), 6), roundNum(curNE.lng(), 6)] | |
]; | |
} | |
// Map is dragged - we need bounds only for the new area | |
if (shiftS) { | |
return [ | |
[roundNum(curNE.lat(), 6), roundNum(curNE.lng(), 6)], | |
[roundNum(preNW.lat(), 6), roundNum(preNW.lng(), 6)] | |
]; | |
} | |
if (shiftN) { | |
return [ | |
[roundNum(curSW.lat(), 6), roundNum(curSW.lng(), 6)], | |
[roundNum(preSE.lat(), 6), roundNum(preSE.lng(), 6)] | |
]; | |
} | |
if (shiftE) { | |
return [ | |
[roundNum(curSW.lat(), 6), roundNum(curSW.lng(), 6)], | |
[roundNum(preNW.lat(), 6), roundNum(preNW.lng(), 6)] | |
]; | |
} | |
if (shiftW) { | |
return [ | |
[roundNum(preSE.lat(), 6), roundNum(preSE.lng(), 6)], | |
[roundNum(curNE.lat(), 6), roundNum(curNE.lng(), 6)] | |
]; | |
} | |
if (shiftSE) { | |
return [ | |
[roundNum(curNW.lat(), 6), roundNum(curNW.lng(), 6)], | |
[roundNum(curNE.lat(), 6), roundNum(curNE.lng(), 6)], | |
[roundNum(preNE.lat(), 6), roundNum(curNE.lng(), 6)], | |
[roundNum(preNW.lat(), 6), roundNum(preNW.lng(), 6)], | |
[roundNum(curSW.lat(), 6), roundNum(preSW.lng(), 6)], | |
[roundNum(curSW.lat(), 6), roundNum(curSW.lng(), 6)] | |
]; | |
} | |
if (shiftSW) { | |
return [ | |
[roundNum(curNW.lat(), 6), roundNum(curNW.lng(), 6)], | |
[roundNum(curNE.lat(), 6), roundNum(curNE.lng(), 6)], | |
[roundNum(curSE.lat(), 6), roundNum(curSE.lng(), 6)], | |
[roundNum(curSW.lat(), 6), roundNum(preNE.lng(), 6)], | |
[roundNum(preNE.lat(), 6), roundNum(preNE.lng(), 6)], | |
[roundNum(preNE.lat(), 6), roundNum(curSW.lng(), 6)] | |
]; | |
} | |
if (shiftNE) { | |
return [ | |
[roundNum(curNW.lat(), 6), roundNum(curNW.lng(), 6)], | |
[roundNum(curNE.lat(), 6), roundNum(preSW.lng(), 6)], | |
[roundNum(preSW.lat(), 6), roundNum(preSW.lng(), 6)], | |
[roundNum(preSW.lat(), 6), roundNum(curNE.lng(), 6)], | |
[roundNum(curSE.lat(), 6), roundNum(curSE.lng(), 6)], | |
[roundNum(curSW.lat(), 6), roundNum(curSW.lng(), 6)] | |
]; | |
} | |
if (shiftNW) { | |
return [ | |
[roundNum(curNE.lat(), 6), roundNum(preNE.lng(), 6)], | |
[roundNum(curNE.lat(), 6), roundNum(curNE.lng(), 6)], | |
[roundNum(curSE.lat(), 6), roundNum(curSE.lng(), 6)], | |
[roundNum(curSW.lat(), 6), roundNum(curSW.lng(), 6)], | |
[roundNum(preSW.lat(), 6), roundNum(curSW.lng(), 6)], | |
[roundNum(preSE.lat(), 6), roundNum(preSE.lng(), 6)] | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment