Created
September 23, 2014 13:43
-
-
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?
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
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great, thanks!