I have a Google map with a semi transparent panel covering a portion of the area. I would like to adjust the center point of the map to take into account the portion of the map that is partially obscured. See the image below. Ideally, where the crosshairs and pin are placed would be the center point of the map.
I hope that makes sense.
The reason is simple: When you zoom it needs to center the map over the crosshair rather than at 50% 50%. Also, I will be plotting markers on the map and moving through them in sequence. When the map centers on them, they also need to be at the offset position.
Thanks in advance!
After extensive searching I could not find a way to do this that also included zoom. Thankfully a clever chap has figured it out. There is also a fiddle here
Also take a look at the panBy(x:number, y:number) function on the maps object.
The documentation mentions this about the function:
Just use it like this:
Here's an example of solving the problem using
panBy()
method of the maps API: http://jsfiddle.net/upsidown/2wej9smf/Andrew's is the answer. However, in my case, map.getBounds() kept returning undefined. I fixed it waiting for the bounds_changed event and then call the function to offset the center. Like so:
Old question, I know. But how about a more CSS-centric way?
http://codepen.io/eddyblair/pen/VjpNQQ
What I did was:
Wrap the map and overlay in a container with
overflow: hidden
Overlaid the overlay with
position: absolute
Extended the map's apparent width by the overlay width (plus any padding and offset) by setting a negative
margin-left
.Then in order to comply with https://www.google.com/permissions/geoguidelines/attr-guide.html positioned the widgets and attribution
div
s.This way the centre of the map lies in line with the centre of the desired area. The js is just standard map js.
Repositioning the icons for street-view is an exercise for the reader :)
If you want the overlay on the left, just change line
24
margin-left
tomargin-right
and line 32right
toleft
.This is not particularly difficult once you find the relevant previous answer.
You need to convert the centre of the map to its world co-ordinates, find where the map needs to be centered to put the apparent centre where you want it, and re-centre the map using the real centre.
The API will always centre the map on the centre of the viewport, so you need to be careful if you use
map.getCenter()
as it will return the real centre, not the apparent centre. I suppose it would be possible to overload the API so that itsgetCenter()
andsetCenter()
methods are replaced, but I haven't done that.Code below. Example online. In the example, clicking the button shifts the centre of the map (there's a road junction there) down 100px and left 200px.