如何以像素为单位偏移Google地图(API v3)的中心?

时间:2022-04-03 15:25:39

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.

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

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.

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

Hope someone can help!

希望有人可以帮忙!

4 个解决方案

#1


57  

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:

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

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

#2


23  

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

请参阅此问题:如何以像素为单位偏移Google地图(API v3)的中心?

This is a modification of an answer I provided here.

这是我在这里提供的答案的修改。

google.maps.Map.prototype.setCenterWithOffset= function(latlng, offsetX, offsetY) {
    var map = this;
    var ov = new google.maps.OverlayView();
    ov.onAdd = function() {
        var proj = this.getProjection();
        var aPoint = proj.fromLatLngToContainerPixel(latlng);
        aPoint.x = aPoint.x+offsetX;
        aPoint.y = aPoint.y+offsetY;
        map.setCenter(proj.fromContainerPixelToLatLng(aPoint));
    }; 
    ov.draw = function() {}; 
    ov.setMap(this); 
};

You can then just use it like so:

然后您可以像这样使用它:

var latlng = new google.maps.LatLng(-34.397, 150.644);
var map = new google.maps.Map(document.getElementById("map_canvas"), {
    zoom: 8,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: latlng
});

map.setCenterWithOffset(latlng, 0, 250);

Here is a working example.

这是一个有效的例子。

#3


4  

Have a look at the MapCanvasProjection object - http://code.google.com/apis/maps/documentation/javascript/reference.html#MapCanvasProjection

看看MapCanvasProjection对象 - http://code.google.com/apis/maps/documentation/javascript/reference.html#MapCanvasProjection

You can get the map center latLng (map.getCenter()) then convert that to the container pixel coordinates with fromContainerPixeltoLatLng()

您可以获取地图中心latLng(map.getCenter())然后使用fromContainerPixeltoLatLng()将其转换为容器像素坐标

#4


3  

there's also the panBy method of the map object... you can offset by a specified number of pixels. However, you'll get a smooth transition animation, which may not be what you wanted. I'm looking for the same thing myself, will let you know what I find.

还有地图对象的panBy方法......你可以偏移指定数量的像素。但是,您将获得平滑过渡动画,这可能不是您想要的。我自己也在找同样的东西,会让你知道我发现了什么。

#1


57  

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:

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

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

#2


23  

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

请参阅此问题:如何以像素为单位偏移Google地图(API v3)的中心?

This is a modification of an answer I provided here.

这是我在这里提供的答案的修改。

google.maps.Map.prototype.setCenterWithOffset= function(latlng, offsetX, offsetY) {
    var map = this;
    var ov = new google.maps.OverlayView();
    ov.onAdd = function() {
        var proj = this.getProjection();
        var aPoint = proj.fromLatLngToContainerPixel(latlng);
        aPoint.x = aPoint.x+offsetX;
        aPoint.y = aPoint.y+offsetY;
        map.setCenter(proj.fromContainerPixelToLatLng(aPoint));
    }; 
    ov.draw = function() {}; 
    ov.setMap(this); 
};

You can then just use it like so:

然后您可以像这样使用它:

var latlng = new google.maps.LatLng(-34.397, 150.644);
var map = new google.maps.Map(document.getElementById("map_canvas"), {
    zoom: 8,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: latlng
});

map.setCenterWithOffset(latlng, 0, 250);

Here is a working example.

这是一个有效的例子。

#3


4  

Have a look at the MapCanvasProjection object - http://code.google.com/apis/maps/documentation/javascript/reference.html#MapCanvasProjection

看看MapCanvasProjection对象 - http://code.google.com/apis/maps/documentation/javascript/reference.html#MapCanvasProjection

You can get the map center latLng (map.getCenter()) then convert that to the container pixel coordinates with fromContainerPixeltoLatLng()

您可以获取地图中心latLng(map.getCenter())然后使用fromContainerPixeltoLatLng()将其转换为容器像素坐标

#4


3  

there's also the panBy method of the map object... you can offset by a specified number of pixels. However, you'll get a smooth transition animation, which may not be what you wanted. I'm looking for the same thing myself, will let you know what I find.

还有地图对象的panBy方法......你可以偏移指定数量的像素。但是,您将获得平滑过渡动画,这可能不是您想要的。我自己也在找同样的东西,会让你知道我发现了什么。