如何以像素为单位偏移谷歌地图(API v3)的中心? [英] How to offset the center of a Google maps (API v3) in pixels?

查看:27
本文介绍了如何以像素为单位偏移谷歌地图(API v3)的中心?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以在 Google maps api v3 中偏移地图的中心.我想以像素来控制这个偏移量,因为 lat 和 lng 似乎是错误的并且很难预测.

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.

我只需要放置一个标记,然后将中心偏移 250 像素,这样我就可以在地图中间放置一些其他内容.

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.

希望有人能帮忙!

推荐答案

在研究时发现这个问题,认为我应该提供一个答案:

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
    )));
}

如果您的 Google 地图变量不是 map,请使用以下调用函数参考:

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

function map_recenter(map,latlng,offsetx,offsety) { ...

这篇关于如何以像素为单位偏移谷歌地图(API v3)的中心?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆