【问题标题】:Google maps auto-fit (auto center & autozoom) doesn't work谷歌地图自动调整(自动居中和自动缩放)不起作用
【发布时间】:2012-09-03 05:51:54
【问题描述】:

我在使用fitBounds 地图方法的地图上遇到问题,它应该提供适当的缩放并将地图居中并为其提供一个纬度数组。这是代码:

<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false">
</script>


<div id="map_canvas">

<script type="text/javascript">
    var map;
    var bounds = new google.maps.LatLngBounds(null);

    function initialize() {
        var mapOptions = {       

            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);



        <?php
        foreach ($this->getProductCollection() as $_e):
            $event = Mage::getModel('catalog/product')->load($_e->getId());
            ?>
                var loc = new google.maps.LatLng("<?php echo $event->getEventLocationLatitude(); ?>","<?php echo $event->getEventLocationLongitude(); ?>");
                 bounds.extend(loc);
                addMarker(loc, '<?php echo $event->getName(); ?>', "active");
                bounds.extend(loc);

        <?php endforeach; ?>


        map.fitBounds(bounds);
        map.panToBounds(bounds);


    }


function addMarker(location, name, active) {          
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        title: name,
        status: active
    });

}



jQuery.noConflict();

jQuery(document).ready(function(){
    initialize();
});
</script>

标记已正确放置在地图上,但我得到了可用的最大缩放:

有什么帮助吗?

【问题讨论】:

  • panToBounds 调用有点多余,但我想这只是试图让它工作。你也调用 bounds.extend() 两次,但它不应该再次成为问题。当您检查 bounds 时,它是否包含正确的值?
  • 忽略php,只看浏览器看到的。在浏览器中“查看源代码”,然后发布该代码。

标签: javascript google-maps google-maps-api-3 google-maps-markers


【解决方案1】:

我在这里更新了你的小提琴:http://jsfiddle.net/KwayW/1/

它现在按预期工作。

这是完整的代码(另存为 test.html 并在浏览器中打开):

<style>#map_canvas { width:500px; height: 400px; }</style>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>

<div id="map_canvas"></div>

<script>
var map;
var markersArray = [];
var image = 'img/';
var bounds = new google.maps.LatLngBounds();
var loc;

function init(){
    var mapOptions = { mapTypeId: google.maps.MapTypeId.ROADMAP };
    map =  new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    loc = new google.maps.LatLng("45.478294","9.123949");
    bounds.extend(loc);
    addMarker(loc, 'Event A', "active");

    loc = new google.maps.LatLng("50.83417876788752","4.298325777053833");
    bounds.extend(loc);
    addMarker(loc, 'Event B', "active");

    loc = new google.maps.LatLng("41.3887035","2.1807378");
    bounds.extend(loc);
    addMarker(loc, 'Event C', "active");

    map.fitBounds(bounds);
    map.panToBounds(bounds);    
}

function addMarker(location, name, active) {          
    var marker = new google.maps.Marker({
        position: location,
        map: map,
        title: name,
        status: active
    });
}

$(function(){ init(); });
</script>

【讨论】:

  • 我已经纠正了你的小提琴(它没有显示地图),它按预期工作。
  • 好吧,我试过你的代码,但它在我的机器上本地不起作用,结果相同。代码与原始代码有什么不同?
  • 优秀的代码,在我的项目中提供了帮助,并且工作得很好!谢谢
猜你喜欢
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
  • 2011-04-23
  • 2012-06-29
  • 2013-09-25
相关资源
最近更新 更多