Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is not particularly difficult once you find <a href="https://stackoverflow.com/questions/2674392/how-to-access-google-maps-api-v3-markers-div-and-its-pixel-position">the relevant previous answer</a>.</p> <p>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 <em>apparent</em> centre where you want it, and re-centre the map using the <em>real</em> centre.</p> <p>The API will always centre the map on the centre of the viewport, so you need to be careful if you use <code>map.getCenter()</code> as it will return the real centre, not the apparent centre. I suppose it would be possible to overload the API so that its <code>getCenter()</code> and <code>setCenter()</code> methods are replaced, but I haven't done that.</p> <p>Code below. <a href="http://acleach.me.uk/gmaps/v3/map-offsetcentre.htm" rel="noreferrer">Example online</a>. In the example, clicking the button shifts the centre of the map (there's a road junction there) down 100px and left 200px.</p> <pre><code>function offsetCenter(latlng, offsetx, offsety) { // latlng is the apparent centre-point // offsetx is the distance you want that point to move to the right, in pixels // offsety is the distance you want that point to move upwards, in pixels // offset can be negative // offsetx and offsety are both optional var scale = Math.pow(2, map.getZoom()); var worldCoordinateCenter = map.getProjection().fromLatLngToPoint(latlng); var pixelOffset = new google.maps.Point((offsetx/scale) || 0,(offsety/scale) ||0); var worldCoordinateNewCenter = new google.maps.Point( worldCoordinateCenter.x - pixelOffset.x, worldCoordinateCenter.y + pixelOffset.y ); var newCenter = map.getProjection().fromPointToLatLng(worldCoordinateNewCenter); map.setCenter(newCenter); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload