2010-11-23 5 views
4

をマッピングし、私は次のコードを持っているのJS intialize方法でマップハンドルが

をビンビンためonchangeviewイベントを処理しようとしている:

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

}

この関数は呼び出されません。なぜハンドラが正しく設定されているかわかりません。

私はまた、次の2行の違いを理解していないとするとき、私はイベント一つの方法または他の

Microsoft.Maps.Events.addHandler(map, "onclick", handleChangeView); 
mapviewer.attachEvent("onchangeview", handleChangeView); 

任意のアイデアを添付する必要がありますか?

答えて

6

多くのイベントが7に改名されました、私はあなたの代わりに "onclickの" と "viewchangeend" ではなく "onchangeview" のの "クリック" を使用したいと思います。

あなたがそれを必要とする内容に応じて、あなたは "viewchangestartの" イベントを好むことが、 "viewchange" またはしかし "をtargetviewchanged"。


また、あなたは間違いなくすべてのイベントは、イベントが現在オブジェクトを使用し、7とセットアップにリスナーを「Microsoft.Maps.Events.addHandler」を使用する必要があります。


例:(ページの一番下にある)イベントの

<!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

0

また、あなたが追加し、同時に新しいイベントを定義することができますそれ:

Microsoft.Maps.Events.addHandler(map, "click", function(args) { 
    ... 
}); 

argsの値は、使用しているオブジェクトとイベントに依存します。この場合、MouseEventArgsオブジェクト(http://msdn.microsoft.com/en-us/library/gg406731.aspx)です。