Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to offset the center of a Google maps (API v3) in pixels?

I was wondering if it is possible to offset the center of a map in Google maps api v3. I would like to control this offset in pixels, since by lat and lng seems wrong and very difficult to predict.

I just need to place a marker, and then offset the center by 250px so I can place some other content in the middle of the map.

Hope someone can help!

like image 347
Landitus Avatar asked Aug 13 '10 01:08

Landitus


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.

Can you style Google Maps API?

With style options you can customize the presentation of the standard Google map styles, changing the visual display of features like roads, parks, businesses, and other points of interest. As well as changing the style of these features, you can hide features entirely.

How do I change the CSS on Google Maps?

The first step is to go to the Add or Edit Map section and then to the 'Map Style Setting' section. Then, select any feature type, type, and color you want. The third step is to save the map to your computer and then launch it in the browser.


1 Answers

Found this question when researching and thought I should provide an answer:

function map_recenter(latlng,offsetx,offsety) {     var point1 = map.getProjection().fromLatLngToPoint(         (latlng instanceof google.maps.LatLng) ? latlng : map.getCenter()     );     var point2 = new google.maps.Point(         ( (typeof(offsetx) == 'number' ? offsetx : 0) / Math.pow(2, map.getZoom()) ) || 0,         ( (typeof(offsety) == 'number' ? offsety : 0) / Math.pow(2, map.getZoom()) ) || 0     );       map.setCenter(map.getProjection().fromPointToLatLng(new google.maps.Point(         point1.x - point2.x,         point1.y + point2.y     ))); } 

If your Google maps variable isn't map use the following call function reference:

function map_recenter(map,latlng,offsetx,offsety) { ... 
like image 84
iambriansreed Avatar answered Sep 21 '22 15:09

iambriansreed