Skip to content

Instantly share code, notes, and snippets.

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