Skip to content

Instantly share code, notes, and snippets.

@alettieri
Created September 27, 2012 21:47
Show Gist options
  • Save alettieri/3796655 to your computer and use it in GitHub Desktop.
Save alettieri/3796655 to your computer and use it in GitHub Desktop.
Offset Google Maps without Animating
/**
* Offsets the map's center from it's current position.
*
* @param {Integer} offsetX. X coord to offset by.
* @param {Integer} offsetY. Y coord to offset by.
*/
google.maps.Map.prototype.offsetCenter = function ( offsetX, offsetY ) {
// Shortcut for google Point
var gPoint = google.maps.Point,
// Shortcut for google LatLng
gLatLng = google.maps.LatLng,
// Get the map's projection
mProj = this.getProjection(),
// Get th map's current bounds
mBounds = this.getBounds(),
// Get the map's current center point, returns LatLng
mapCenter = this.getCenter(),
// Scale of current map, helps us determine how much of an offset we have.
scale = Math.pow( 2, this.getZoom() ),
// Get world center
worldCordCenter = mProj.fromLatLngToPoint( mapCenter ),
// Get the offset point.
pOffset = new gPoint( ( offsetX/scale ) || 0, ( offsetY/scale ) || 0 ),
// Get our new coordinate world center, the offset happens here.
worldCoordNewCenter = new gPoint( worldCordCenter.x - pOffset.x, worldCordCenter.y - pOffset.y ),
// Get the new center LatLng coordinates.
newCenter = mProj.fromPointToLatLng( worldCoordNewCenter )
;
// Set the map's new offset center.
this.setCenter( newCenter );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment