我想向 Bing 地图添加一个图钉,当用户单击图钉时,应该会出现一个信息框。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script type="text/javascript">
function BingMap(){
var map = null;
var pinInfobox = null;
this.showMap = function(latitude, longitude){
var mapOptions = {
credentials: "mycredentials",
center: new Microsoft.Maps.Location(latitude, longitude),
mapTypeId: Microsoft.Maps.MapTypeId.aerial,
zoom: 18,
showScalebar: false
}
map = new Microsoft.Maps.Map(document.getElementById("map_canvas"), mapOptions);
Microsoft.Maps.Events.addHandler(map, 'click', function(event) {
var point = new Microsoft.Maps.Point(event.getX(), event.getY());
var loc = event.target.tryPixelToLocation(point);
that.addNewMarker(loc.latitude, loc.longitude);
});
};
this.addMarker = function(id, title, latitude, longitude){
var loc = new Microsoft.Maps.Location(latitude, longitude);
var pin = new Microsoft.Maps.Pushpin(loc);
pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
{title: title,
description: 'This pushpin is located at (0,0).',
visible: false,
offset: new Microsoft.Maps.Point(0,15)});
Microsoft.Maps.Events.addHandler(pin, 'click', function(event) {
pinInfobox.setOptions({ visible:true });
});
map.entities.push(pin);
map.entities.push(pinInfobox);
};
}
$(document).ready(function() {
var map = new BingMap();
map.showMap(50.364302, 7.559795);
map.addMarker("1", "title", 50.364302, 7.559795);
});
</script>
</head>
<body>
<div id='map_canvas' style="position:absolute; width:400px; height:400px;"></div>
</body>
</html>
我将代码放入我创建实例并调用 showMap 和 addMarker 方法的类中。不幸的是,没有调用显示信息框的事件。
仅当我将代码放在类方法中时才会出现此问题。以下页面上的代码有效:http: //msdn.microsoft.com/en-us/library/gg508987.aspx
我是否必须以某种方式更新 DOM 或其他东西?