简体   繁体   English

Google Maps fitBounds是否在标记标签上抛出错误?

[英]Is Google Maps fitBounds throwing a bug on marker label?

if I add a marker with a label on a Google Maps map and then I call fitBounds to another region, I still continue to see the label without the marker unless I zoom out. 如果我在Google Maps地图上添加带有标签的标记,然后将fitBounds调用到另一个区域,除非我缩小地图,否则我仍然会继续看到没有标记的标签。 Is it a bug? 是虫子吗?

Here's a working fiddle, try to click Milano button: JSFiddle 这是一个有效的小提琴,尝试单击Milano按钮: JSFiddle

Html: HTML:

<div id="map-canvas"></div>

CSS: CSS:

#map-canvas {
    width:100%;
    height:500px;
    position:"absolute";
    top: 0px;
    left: 0px;
    overflow:"hidden";
    background-color:#000;
}

.map-button{    
    margin-top: 10px;
    height: 30px;
    cursor: pointer;
    color: #565656;
    border:0px;
    border-radius: 3px;
    background-color:#fff;
    box-shadow: rgba(0, 0, 0, 0.3) 1px 1px 4px -1px;    
}

.map-button:hover {
    background-color: #ebebeb;
    color: #000;
}

Javascript: 使用Javascript:

function initialize() {
    var mapOptions = {
        center: new google.maps.LatLng(41.29085, 12.71216),
        zoom: 6,
        gestureHandling: 'greedy'               
    };
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    var marker = new google.maps.Marker({
                                    position: map.getCenter(), 
                                    map: map, 
                                    label: {
                                        text:'Roma',
                                        fontSize: '28px'
                                    }
                                });

    var control = document.createElement('button');
    control.innerHTML = 'Milano';
    control.className = 'map-button';

    map.controls[google.maps.ControlPosition.TOP_LEFT].push(control);

    google.maps.event.addDomListener(control, 'click', function (event) {
        var milanBounds = new google.maps.LatLngBounds(
                                                {lat: 45.462818, lng: 9.184145},
                                                {lat: 45.466806, lng: 9.190239});

        map.fitBounds(milanBounds);
    });
}
google.maps.event.addDomListener(window, 'load', initialize);

Changing the version of API helped me with the same bug (labels moving without markers). 更改API版本有助于解决相同的错误(标签移动时没有标记)。 It happens when I try to pan map to a specific marker position and then zoom it in. In my case version 3.31 of google maps API worked out. 当我尝试将地图平移到特定的标记位置然后放大时会发生这种情况。在我的情况下,制定了Google Maps API版本3.31。

Looks like the issue you describe only happens with the experimental version of the API. 看来您所描述的问题仅在API的实验版本中发生。

By not specifying a version number in your API call, you will get the experimental version, which is probably not the best idea, especially for applications in production. 通过在API调用中未指定版本号,您将获得实验性版本,这可能不是最好的主意,尤其是对于生产中的应用程序。

Try to specify the version in your API call to get the release version, for example: 尝试在API调用中指定版本以获取发行版本,例如:

//maps.googleapis.com/maps/api/js?v=3&key=YOUR_API_KEY

Please refer to this link to know more about versioning and the best practices encouraged by Google. 请参考此链接以了解有关版本控制和Google鼓励的最佳做法的更多信息。

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

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