为什么这个边界对象没有得到扩展?
在此 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在为每个标记创建新的边界。您应该在 for 循环之外创建边界。
You are creating an new bounds for each marker. You should create the bounds outside of the for loop.