Created
September 5, 2018 08:33
-
-
Save sharapeco/718c05f427fb5eea1d6776f8f62f733f to your computer and use it in GitHub Desktop.
Google Maps API fitBoundsPerfectly
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
// 2014-12-16 | |
google.maps.Map.prototype.fitBoundsPerfectly = function fitBoundsPerfectly(aBounds) { | |
var mapBounds, mapSW, mapNE, mapDLat, mapDLng, aSW, aNE, dLat, dLng, ratio, dZoom; | |
mapBounds = this.getBounds(); | |
mapSW = mapBounds.getSouthWest(); | |
mapNE = mapBounds.getNorthEast(); | |
mapDLat = mapNE.lat() - mapSW.lat(); | |
mapDLng = mapNE.lng() - mapSW.lng(); | |
aSW = aBounds.getSouthWest(); | |
aNE = aBounds.getNorthEast(); | |
dLat = aNE.lat() - aSW.lat(); | |
dLng = aNE.lng() - aSW.lng(); | |
ratio = Math.max(dLat / mapDLat, dLng / mapDLng); | |
dZoom = -Math.ceil(Math.log(ratio) / Math.log(2)); | |
if (dZoom) { | |
this.setZoom(this.getZoom() + dZoom); | |
} | |
this.panTo(aBounds.getCenter()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment