为什么这个边界对象没有得到扩展?

发布于 2025-01-06 23:41:33 字数 3103 浏览 2 评论 0原文

在此 JSON-P 回调中,我尝试通过迭代中的每个下载标记来扩展名为 bounds 的对象(之前声明)。在日志中,它确实被设置为边界对象,但 NE SW 是相同的:

ie $: 德 乙:18.031393699999967 d: 18.031393699999967 __proto__:德 是:他 电话:59.2933167 电话:59.2933167 __原型__:他 __proto__: 即

为什么?这让我发疯..

addStoreMarkers: function(data) {
            /** This is the function that the injected script automatically calls, it passes the markers data in the form 
                JSON (an array object) as an argument. **/

                //iterate over each instance in the data
                for (var i in data.markers) {

                    //set marker icon
                    var image = new google.maps.MarkerImage('images/'+ data.markers[i].icon  +'.png',
                        new google.maps.Size(32.0, 37.0),
                        new google.maps.Point(0, 0),
                        new google.maps.Point(16.0, 37.0)
                    );

                    //set icon shadow
                    var shadow = new google.maps.MarkerImage('images/shadow.png',
                        new google.maps.Size(51.0, 37.0),
                        new google.maps.Point(0, 0),
                        new google.maps.Point(16.0, 35.0) 
                    );

                    //add markers to map
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lgt),
                        map: map,
                        visible: false,
                        icon: image,
                        shadow: shadow
                    });

                    //add marker to markers array   
                    markers.push(marker);

                    //create the bounds representing all downloaded markers
                    bounds = new google.maps.LatLngBounds();
                    bounds.extend(marker.position);

                    //store infoBox data in markers array
                    markers[i].store = data.markers[i].store;
                    markers[i].distance = data.markers[i].distance;
                    markers[i].i = i;

                    //add click-listener to marker
                    google.maps.event.addListener(markers[i], 'click', function() {
                        //on click
                            //set parameter active (so it won't hide)
                            Map.setMarkerActive(this);
                            //call todisplay custom infoWindow function
                            Map.displayInfo(this);
                    });

                }

                console.log(bounds);

                //fitbounds if requested, else pan a bit to call updateView
                if(first){Map.fitBounds(5);first=false;} else {map.panBy(1,1);}

                //remove data from the DOM
                ejectDOM('#jsonp_ref');

                //notify
                Menu.setStatus('Klart!');

                //housekeeping
                delete data;

                console.log('add markers function complete ' + markers.length);
        },

In this JSON-P callback I'm trying to extend the object called bounds (declared earlier) by each downloaded marker in the iteration. In the log, it does get set as a bounds object, but the NE SW are the same:

ie
$: de
b: 18.031393699999967
d: 18.031393699999967
__proto__: de
Y: he
b: 59.2933167
d: 59.2933167
__proto__: he
__proto__: ie

Why?? This is driving me insane..

addStoreMarkers: function(data) {
            /** This is the function that the injected script automatically calls, it passes the markers data in the form 
                JSON (an array object) as an argument. **/

                //iterate over each instance in the data
                for (var i in data.markers) {

                    //set marker icon
                    var image = new google.maps.MarkerImage('images/'+ data.markers[i].icon  +'.png',
                        new google.maps.Size(32.0, 37.0),
                        new google.maps.Point(0, 0),
                        new google.maps.Point(16.0, 37.0)
                    );

                    //set icon shadow
                    var shadow = new google.maps.MarkerImage('images/shadow.png',
                        new google.maps.Size(51.0, 37.0),
                        new google.maps.Point(0, 0),
                        new google.maps.Point(16.0, 35.0) 
                    );

                    //add markers to map
                    var marker = new google.maps.Marker({
                        position: new google.maps.LatLng(data.markers[i].lat, data.markers[i].lgt),
                        map: map,
                        visible: false,
                        icon: image,
                        shadow: shadow
                    });

                    //add marker to markers array   
                    markers.push(marker);

                    //create the bounds representing all downloaded markers
                    bounds = new google.maps.LatLngBounds();
                    bounds.extend(marker.position);

                    //store infoBox data in markers array
                    markers[i].store = data.markers[i].store;
                    markers[i].distance = data.markers[i].distance;
                    markers[i].i = i;

                    //add click-listener to marker
                    google.maps.event.addListener(markers[i], 'click', function() {
                        //on click
                            //set parameter active (so it won't hide)
                            Map.setMarkerActive(this);
                            //call todisplay custom infoWindow function
                            Map.displayInfo(this);
                    });

                }

                console.log(bounds);

                //fitbounds if requested, else pan a bit to call updateView
                if(first){Map.fitBounds(5);first=false;} else {map.panBy(1,1);}

                //remove data from the DOM
                ejectDOM('#jsonp_ref');

                //notify
                Menu.setStatus('Klart!');

                //housekeeping
                delete data;

                console.log('add markers function complete ' + markers.length);
        },

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

往事风中埋 2025-01-13 23:41:33
//create the bounds representing all downloaded markers
bounds = new google.maps.LatLngBounds();
bounds.extend(marker.position);

您正在为每个标记创建新的边界。您应该在 for 循环之外创建边界。

//create the bounds representing all downloaded markers
bounds = new google.maps.LatLngBounds();
bounds.extend(marker.position);

You are creating an new bounds for each marker. You should create the bounds outside of the for loop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文