谷歌地图fitBounds()损坏或..? [英] google maps fitBounds() is broken or..?

查看:34
本文介绍了谷歌地图fitBounds()损坏或..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<!DOCTYPE html>
<html>
<head>
<title>Google Maps JavaScript API v3 Example: Map Simple</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<meta charset="UTF-8">
<style type="text/css">
#map_canvas {
    width: 500px;
    height: 500px;
}
</style>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
<script type="text/javascript">
    var map;
    function initialize() {
        var myOptions = {
            zoom : 8,
            center : new google.maps.LatLng(-34.397, 150.644),
            mapTypeId : google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);

        var locationA = {
            latitude : 37.788081 + 0.0049,
            longitude : -83.001709 + 0.00019
        };
        var locationB = {
            latitude : 37.788081,
            longitude : -83.001709
        };

        var sw = new google.maps.LatLng(locationA.latitude, locationA.longitude);
        var ne = new google.maps.LatLng(locationB.latitude, locationB.longitude);

        var distance = google.maps.geometry.spherical.computeDistanceBetween(sw, ne);
        console.log("Distance between SW & NE: " + distance);

        var bounds = new google.maps.LatLngBounds(sw, ne);
        console.log(bounds);
        map.fitBounds(bounds);
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
    <div id="map_canvas"></div>
</body>
</html>

以上返回此:

在开发人员测试期间意外地找到了它,清除了该错误"周围的所有内容,但实际上,它似乎仍然是一个错误.

Got it by accident during dev tests, cleaned everything around that "bug" and still, it really seems to be a bug.

有人可以解释吗?

locationA.longitude 0.00019 设置为 0 确实显示了有效的地图,所以这就是问题所在.

Setting locationA.longitude's 0.00019 to 0 does show valid map so this is the very issue.

推荐答案

您的边界无效,南大于北,东大于西.如果将正号切换为负号,则将加载.我只会创建一个不带参数的 LatLngBounds ,并使用 bounds.extend 代替.

Your bounds is invalid, south is greater than north and east is greater than west. If you switch the pluses to minus it will load. I would just create a LatLngBounds with no arguments and use bounds.extend instead.

var bounds = new google.maps.LatLngBounds();
    bounds.extend(sw);
    bounds.extend(ne);

这篇关于谷歌地图fitBounds()损坏或..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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