Created
May 13, 2015 15:11
-
-
Save izumskee/017601a44b89d9ca4737 to your computer and use it in GitHub Desktop.
offset google maps center
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
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