简体   繁体   English

Google Map不在Firefox中居中

[英]Google map not centering in Firefox

I've got a strange issue with a google map, which I can only duplicate in Firefox. 我在Google地图上遇到了一个奇怪的问题,我只能在Firefox中复制它。 To explain, we have a static map which when clicked on enlarges to a full interactable map with a marker in the center. 为了说明,我们有一个静态地图,单击该静态地图会放大为一个完整的可交互地图,中间带有一个标记。 This works fine in all browsers, except Firefox. 这在除Firefox之外的所有浏览器中都可以正常工作。 In Firefox, what happens is the map is not centered correctly. 在Firefox中,会发生地图未正确居中的情况。 The marker is placed, however, it is in the top left corner of the map just outside of the visible area. 标记被放置,但是它位于地图的左上角,就在可见区域之外。

Any ideas? 有任何想法吗? This has been baffling me for a while. 这一直困扰着我一段时间。

JavaScript: JavaScript:

// lat and lng are initialized earlier
var latlng = new google.maps.LatLng(lat, lng);
var myOptions = {
    zoom: 13,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
};

var map = new google.maps.Map($("#map_canvas")[0], myOptions);

var marker = new google.maps.Marker({
    position: latlng,
    map: map,
    icon: icon
});

var resizeMap = function () {
    $("#smallMap, #smallMapOverlay").remove();
    $("#map_canvas").show();
    google.maps.event.trigger(map, 'resize');
    map.setCenter(latlng, 13);
};

$("#smallMapOverlay").live("click", function (e) {
    e.preventDefault();
    resizeMap();
});

CSS: CSS:

#map_canvas {
    width: 640px;
    height: 377px; 
}

Karim79' fix works. Karim79的修复程序。 I worked it out in a slightly different way. 我以稍微不同的方式解决了问题。 If you have a wrapper class to handle mapping functions, you could create a reset function like so, 如果您有一个包装类来处理映射函数,则可以像这样创建一个重置函数,

function reset(){
        setTimeout(function(){gm.event.trigger(_gMap, 'resize');}, 50);
        setTimeout(function(){_gMap.setCenter(settings.mapOptions.center);}, 100);
    }

Best 最好

It seems that putting the resize and setCenter methods into show() 's callback, and giving a tiny delay fixes the issue. 似乎将resizesetCenter方法放入show()的回调中,并稍加延迟即可解决此问题。

var resizeMap = function () {
    $("#smallMap, #smallMapOverlay").remove();
    $("#map_canvas").show(10, function () {
        google.maps.event.trigger(map, 'resize');
        map.setCenter(latlng, 13);
    });
};

It's not the most elegant solution, but it works. 这不是最优雅的解决方案,但它可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM