【问题标题】:google maps fitBounds() is broken or..?谷歌地图 fitBounds() 坏了还是..?
【发布时间】:2012-07-23 07:57:03
【问题描述】:
<!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>

上面返回这个:

在开发测试期间偶然发现它,清理了该“错误”周围的所有内容,但它似乎确实是一个错误。

谁能解释一下?

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

【问题讨论】:

  • 顺便说一句,如果你调试你会注意到 bounds 得到错误的值。这是它的构造函数的问题。

标签: google-maps


【解决方案1】:

你的界限是无效的,南大于北,东大于西。如果将加号切换为减号,它将加载。我只会创建一个不带参数的LatLngBounds,然后改用bounds.extend

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

【讨论】:

  • 尽管如此,不知道extend()。非常感谢傻瓜!
猜你喜欢
  • 2016-06-16
  • 2017-10-09
  • 2012-01-15
  • 1970-01-01
  • 2012-05-03
  • 2011-12-31
  • 1970-01-01
  • 2012-06-19
  • 2014-12-14
相关资源
最近更新 更多