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); |
A very particular/specific/critical thinking question: If I trigger a zoom (in or out, not by mouse click)... will it zoom from the relative middle of the container, or zoom in relative to the new offset center?
map.panBy(0,-30)
when zoom will offset the new center... I also want to know if this one could fix that~
This code makes Firefox and Chrome on my Computer take 90-100% of cpu-power (until they stop responding)... =/
Any idea why?
Very helpful! congrats!
Works great, thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice solution, cheers for sharing