Last active
June 23, 2016 10:16
-
-
Save LucaColonnello/8e57b62d8fefd4b1a6d9 to your computer and use it in GitHub Desktop.
Google Maps Offset Center
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
// http://stackoverflow.com/questions/10656743/how-to-offset-the-center-point-in-google-maps-api-v3 | |
function offsetCenter(map, latlng, offsetx, offsety) { | |
// latlng is the apparent centre-point | |
// offsetx is the distance you want that point to move to the right, in pixels | |
// offsety is the distance you want that point to move upwards, in pixels | |
// offset can be negative | |
// offsetx and offsety are both optional | |
var scale = Math.pow(2, map.getZoom()); | |
var nw = new google.maps.LatLng( | |
map.getBounds().getNorthEast().lat(), | |
map.getBounds().getSouthWest().lng() | |
); | |
var worldCoordinateCenter = map.getProjection().fromLatLngToPoint(latlng); | |
var pixelOffset = new google.maps.Point((offsetx/scale) || 0,(offsety/scale) ||0) | |
var worldCoordinateNewCenter = new google.maps.Point( | |
worldCoordinateCenter.x - pixelOffset.x, | |
worldCoordinateCenter.y + pixelOffset.y | |
); | |
var newCenter = map.getProjection().fromPointToLatLng(worldCoordinateNewCenter); | |
map.setCenter(newCenter); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment