event-handling - 处理 Bing map 的 onchangeview 事件

标签 event-handling bing-maps

我正在尝试处理 bing map 的 onchangeview 事件

在 js 初始化方法中,我有以下代码:

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);

我也有这个功能

函数 handleChangeView(e) {

}

这个函数永远不会被调用,我不知道为什么因为处理程序设置正确。

我也不明白以下两行之间的区别以及何时应该以一种或另一种方式附加事件
Microsoft.Maps.Events.addHandler(map, "onclick", handleChangeView);
mapviewer.attachEvent("onchangeview", handleChangeView);

有任何想法吗?

最佳答案

许多事件在 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>

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

关于event-handling - 处理 Bing map 的 onchangeview 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4259129/

相关文章:

google-maps - 是否可以使用Google Maps API或Bing Maps API获取地址的邻居?

c# - Bing 会将 map 的大小调整为坐标数组吗?

javascript - 为什么 Bing Maps 的 VEMap.Find 会悄无声息地失败?

xaml - 如何在 WP7 Bing Map 控件上放置多个圆形叠加层?

java - 为什么它没有显示我的标签?

c# - Java 等同于 C# 的委托(delegate)/事件 : Changing Event Invoked Code Dynamically

jquery - 有没有 jQuery .change 实时替代方案?

javascript - 如何设置首先触发的事件?

c# - 在 Bing map WPF 上居中和缩放

c# - 将标签绑定(bind)到变量