Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to offset the center point in Google maps api V3

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!

Mockup of the map I am building

like image 989
will Avatar asked May 18 '12 16:05

will


People also ask

Can you customize Google Maps API?

Easily create your styleThe new Google Maps APIs Styling Wizard helps you to create a map style in a few clicks. Use one of our pre-built styles or create your own style from scratch.

How do you center Google Maps?

Please go to your map and drag and drop your map to the position you wish. You can also zoom in your map a little bit with mouse wheel or zoom cotrols on the bottom right corner of the map. Please remember to save your map after any changes. Hope that helps.


2 Answers

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 its getCenter() and setCenter() 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.

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);  } 
like image 152
Andrew Leach Avatar answered Oct 09 '22 08:10

Andrew Leach


Also take a look at the panBy(x:number, y:number) function on the maps object.

The documentation mentions this about the function:

Changes the center of the map by the given distance in pixels. If the distance is less than both the width and height of the map, the transition will be smoothly animated. Note that the map coordinate system increases from west to east (for x values) and north to south (for y values).

Just use it like this:

mapsObject.panBy(200, 100) 
like image 20
Kah Tang Avatar answered Oct 09 '22 07:10

Kah Tang