处理Bing地图的onchangeview事件

Som*_*ere 4 event-handling bing-maps

我正在尝试为bing地图处理onchangeview事件

在js intialize方法中,我有以下代码:

map = new Microsoft.Maps.Map(document.getElementById("mapviewer"), {
    credentials: bingMapsKey, 
    center : new Microsoft.Maps.Location(42.3508, -71.0717),
    zoom: 12
    });
//Microsoft.Maps.Events.addHandler(map, "onchangeview", handleChangeView);
Microsoft.Maps.Events.addHandler(map, "onclick", handleChangeView);
mapviewer.attachEvent("onchangeview", handleChangeView);
Run Code Online (Sandbox Code Playgroud)

我也有这个功能

function handleChangeView(e){

}

永远不会调用此函数,我不确定为什么处理程序设置正确.

我也不明白以下两行之间的区别以及我应该以某种方式附加事件

Microsoft.Maps.Events.addHandler(map, "onclick", handleChangeView);
mapviewer.attachEvent("onchangeview", handleChangeView);
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

wil*_*aks 6

许多事件在7中被重命名,我想你想使用" click "而不是" onclick "和" viewchangeend "而不是" onchangeview ".

根据您的需要,您可能更喜欢事件" viewchangestart "," viewchange "或" targetviewchanged ".


此外,您绝对应该使用" Microsoft.Maps.Events.addHandler "来设置7的侦听器,所有事件现在都使用Events对象.


例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <script src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0" type="text/javascript" charset="UTF-8"></script>
</head>
<body onload="init()">

<div id="map" style="position: relative; width: 800px; height: 350px;"></div>   


<script type="text/javascript">

/**
 * Map click event handler.
 */
function onclick(e){
    var map = this.target; //reference to the Map object from which it came
    //...
    var x = e.getX(); // horizontal position of the click
    var y = e.getY(); // vertical position of the click
    //...
    console.log('The map has been clicked');
}

/**
 * View change event handler.
 */
function onview(){
    var map = this.target; //reference to the Map object from which it came
    //...
    console.log('The view has changed');
}

/**
 * Load the map and setup event listeners
 */
function init(){
    var map = new Microsoft.Maps.Map(
        document.getElementById("map"),
        {
            credentials: "YOUR-BING-KEY",
            mapTypeId: Microsoft.Maps.MapTypeId.road
        }
    );
    Microsoft.Maps.Events.addHandler(map, "click", onclick);
    Microsoft.Maps.Events.addHandler(map, "viewchangeend", onview);
}

</script>


</body>
</html>
Run Code Online (Sandbox Code Playgroud)

事件列表(在页面底部):http://msdn.microsoft.com/en-us/library/gg427609.aspx