猿问

Bing 地图图钉单击事件处理程序仅适用于最后一个

我的应用程序生成了以下 Bing 地图 -

该绿色图钉添加到地图后红色图钉。每个图钉都有一个点击处理程序,可以打开一个信息框。我的问题是我点击的任何图钉,它只打开绿色图钉的信息框。


这是我的代码-


    var center = new Microsoft.Maps.Location(24.3636, 88.6241);

    var map = new Microsoft.Maps.Map(document.getElementById('myMap'), { center:center, zoom: 7 });


    var color; var description;


    @foreach (var item in Model.MapData)

    {

        <text>


        var location = new Microsoft.Maps.Location(@item.Latitude,@item.Longitude);

        var pushpin = new Microsoft.Maps.Pushpin(location, { color: color });


        map.entities.push(pushpin);


        var infobox = new Microsoft.Maps.Infobox(location, {

            title: '@item.DtwId',

            description: '@item.Desc',

            visible: false 

        });


        Microsoft.Maps.Events.addHandler(pushpin, 'click', function () {

            infobox.setOptions({ visible: true });

        });


        infobox.setMap(map);



        </text>

    }

我在这里错过了什么?


万千封印
浏览 174回答 1
1回答

慕桂英4014372

&nbsp;var location = new Microsoft.Maps.Location(@item.Latitude,@item.Longitude);&nbsp; &nbsp; &nbsp; &nbsp; var pushpin = new Microsoft.Maps.Pushpin(location, { color: color });&nbsp; &nbsp; &nbsp; &nbsp; pushpin.metadata = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;title: '@item.DtwId',&nbsp; &nbsp; &nbsp; &nbsp; description: '@item.Desc',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; franchiseNumber: 1&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; Microsoft.Maps.Events.addHandler(pushpin, 'click', pushpinClicked);&nbsp; &nbsp; &nbsp; &nbsp; map.entities.push(pushpin);&nbsp; &nbsp; function pushpinClicked(e) {&nbsp; &nbsp; &nbsp; &nbsp; var infobox = new Microsoft.Maps.Infobox(e.target.getLocation(), {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: e.target.metadata.title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: e.target.metadata.description,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; visible: true&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; infobox.setMap(map);
随时随地看视频慕课网APP
我要回答