Google Map FitBounds不以标记为中心

时间:2014-03-19 18:37:43

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

出于某种原因,我真的很挣扎于fitBounds。这是我用来渲染地图的功能,绘制一些标记并(希望)将地图围绕标记居中。一切都很好。标记位于正确的位置 - 唯一剩下的就是使地图居中。现在,它正把我带到海洋中间。

// locations is a json object coming from an ajax response
// Sample response: ["MyTown, State (12345)"]
renderMap: function(locations) {
        var that = this;
        var map;
        var bounds;
        var idle;
        var markerArray = [];
        var marker = {};
        var markerLatLng = {};
        var mapOptions = {
            zoom: 7
        };

        map = new google.maps.Map(document.getElementById('mapCanvas'), mapOptions);
        bounds = new google.maps.LatLngBounds();

        $.each(locations, function(key, val) {
            that.geoCodeAddress(val, function(geoCodeAddressResponse) {
                switch (geoCodeAddressResponse.status)
                {
                    case 'success': // Do work!
                        markerLatLng = {
                            lat: geoCodeAddressResponse.lat,
                            lng: geoCodeAddressResponse.lng
                        };

                        marker = new google.maps.Marker({
                            'map': map,
                            'clickable': true,
                            'position': markerLatLng,
                            'animation': google.maps.Animation.DROP
                        });

                        bounds.extend(marker.position); 
                        break;
                    case 'fail':  // Could not get latlng for location.
                        break;
                    default: // Epic fail.
                        break;
                }
            });
        });

        map.fitBounds(bounds);
    }

我一直在使用this jsFiddle作为参考。我为此行获得了相同的输出:marker.position

{k: 12.1234567, A: -34.12345678912345, toString: function, j: function, equals: function…}

(lat / lng只是示例数字)

感谢您的任何建议!

0 个答案:

没有答案