Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
492 views
in Technique[技术] by (71.8m points)

javascript - Google maps auto-fit (auto center & autozoom) doesn't work

I have a problem with a map on which I've used the fitBounds map methods, which should give the proper zoom and center the map giving it a latlon array. here's the code:

<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>

The markers are correctly placed on the map, but I get the maximum zoom available:

enter image description here

Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I've updated your fiddle here: http://jsfiddle.net/KwayW/1/

It works now as expected.

Here's the full code (save this as test.html and open in browser):

<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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...