我正在试验 Bing SDK 站点上的示例代码,但无法让 click 事件与我在 JS .each 循环中添加的图钉一起工作,该循环从 sql 服务器中提取经度、纬度坐标。我可以让图钉显示正常,如果我在循环中将它们的可见设置为 true,我可以让 InfoBox 正常显示,但是如果我在循环中将可见属性设置为 false,然后尝试将可见设置为 true图钉点击事件没有任何反应。请指教,谢谢。
<script type="text/javascript">
$(document).ready(function () {
var map = null;
var pinInfobox = null;
function LoadMap() {
map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: "Aq85qFzkJkdfauSc1nXq3IATrqgPGN77_MBhghypyJ94ygTElWHSHYv3WXYZT-3E"} );
}
function displayInfobox(e) {
defaultInfobox.setOptions({ visible: true });
}
LoadMap();
var url = "/Job/GetLocations";
$.getJSON(url, null, function (data) {
$.each(data, function (index, LocationData) {
var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), { text: '' });
pushpin.setLocation(new Microsoft.Maps.Location(LocationData.Latitude, LocationData.Longitude));
var Route = LocationData.PickupCity + ' to ' + LocationData.DeliveryCity;
var Details = 'Pays $' + LocationData.Payment + '<br>' + 'Pickup ' + LocationData.PickupHour + LocationData.PickupMinute + '<br>' + 'Deliver ' + LocationData.DeliveryHour + LocationData.DeliveryMinute;;
var Content = '<a href="http://www.google.com">Google</a>';
var infoboxOptions = { title: Route, description: Details, htmlContent: Content };
var defaultInfobox = new Microsoft.Maps.Infobox(new Microsoft.Maps.Location(LocationData.Latitude, LocationData.Longitude), infoboxOptions);
Microsoft.Maps.Events.addHandler(pushpin, 'click', displayInfobox);
map.entities.push(pushpin);
map.entities.push(defaultInfobox);
});
map.setView({ zoom: 7, center: new Microsoft.Maps.Location(34.470639154314995, -118.55275198817253) });
});
});
</script>