Skip to content

Instantly share code, notes, and snippets.

@msherstobitow
Created September 23, 2014 13:43
Show Gist options
  • Save msherstobitow/524412b3c8555166ca25 to your computer and use it in GitHub Desktop.
Save msherstobitow/524412b3c8555166ca25 to your computer and use it in GitHub Desktop.
How to offset the center of a Google maps (API v3) in pixels?
google.maps.Map.prototype.setCenterWithOffset= function(latlng, offsetX, offsetY) {
var map = this;
var ov = new google.maps.OverlayView();
ov.onAdd = function() {
var proj = this.getProjection();
var aPoint = proj.fromLatLngToContainerPixel(latlng);
aPoint.x = aPoint.x+offsetX;
aPoint.y = aPoint.y+offsetY;
map.setCenter(proj.fromContainerPixelToLatLng(aPoint));
};
ov.draw = function() {};
ov.setMap(this);
};
// Using
var latlng = new google.maps.LatLng(-34.397, 150.644);
var map = new google.maps.Map(document.getElementById("map_canvas"), {
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: latlng
});
map.setCenterWithOffset(latlng, 0, 250);
@klapec
Copy link

klapec commented Apr 3, 2020

Works great, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment