gpt4 book ai didi

javascript - Bing map 的信息框

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:05:17 24 4
gpt4 key购买 nike

我目前正在开发一个 bing map 应用程序,但我似乎无法在用户单击图钉时显示信息框。一直在关注SDK,应该是差不多的。这是我所拥有的:

// ***********************************
// Function creates a single pin
// and returns a reference to the pin

function createMapPin(latLong, sName) {
var pinImg = "imagespins/Good.png";


var pin = new Microsoft.Maps.Pushpin(latLong, {
icon: pinImg,
anchor: new Microsoft.Maps.Point(8, 8),
draggable: true,
width: 48,
height: 48
});

pin.title = sName;


pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
{ title: 'My Pushpin',
description: 'This pushpin is located at (0,0).',
visible: false,
offset: new Microsoft.Maps.Point(0, 15)
});



// Add handlers for the pushpin events
Microsoft.Maps.Events.addHandler(pin, 'click', displayInfobox);
// Hide the infobox when the map is moved.
Microsoft.Maps.Events.addHandler(map, 'viewchange', hideInfobox);

map.entities.push(pin);
map.entities.push(pinInfobox);

return pin;
}

function displayInfobox(e) {
pinInfobox.setOptions({ visible: true });
}


function hideInfobox(e) {
pinInfobox.setOptions({ visible: false });
}

图钉成功创建并添加到 map ,当用户点击图钉时,它确实进入了 dispayInfoBox 方法,但消息框似乎没有出现。

非常感谢任何建议。谢谢!

最佳答案

你的代码有效,我试过了,只要你只调用一次createMapPin()。但是根据您的代码判断,您的意图是多次调用 createMapPin(),这将导致 infobox 停止像您认为的那样运行。原因是这一行:

pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
{ title: 'My Pushpin',
description: 'This pushpin is located at (0,0).',
visible: false,
offset: new Microsoft.Maps.Point(0, 15)
});

由于您没有通过 var 关键字将 pinInfobox 声明为局部变量,因此它隐式声明为全局变量。我不认为这是你的意图,因为一旦你再次调用 createMapPin()pinInfobox 因为它是一个全局变量,将被覆盖。所以实际上,即使您继续创建新的信息框,它也只有一个实例,并且它只是不断地被分配新的值。因此,如果您单击图钉,一个信息框可能会在屏幕外的某个地方弹出,您看不到它。我相信您的意图是让每个图钉关联一个信息框?为此,您需要将信息框声明为局部变量,如下所示:

var pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
{ title: 'My Pushpin',
description: 'This pushpin is located at (0,0).',
visible: false,
offset: new Microsoft.Maps.Point(0, 15)
});

您还需要修改事件处理程序以与本地版本的信息框对象交互,而不仅仅是单个全局变量。最简单的方法是在 createMapPin() 函数的范围内将事件处理程序声明为匿名函数,并使用函数绑定(bind)覆盖 this 关键字的含义:

Microsoft.Maps.Events.addHandler(pin, 'click', function (e) {
this.setOptions({ visible: true });
} .bind(pinInfobox));
Microsoft.Maps.Events.addHandler(map, 'viewchange', function(e) {
this.setOptions({ visible: false });
}.bind(pinInfobox));

这是您更新后的代码:

function createMapPin(latLong, sName) {
var pinImg = "imagespins/Good.png";

var pin = new Microsoft.Maps.Pushpin(latLong, {
icon: pinImg,
anchor: new Microsoft.Maps.Point(8, 8),
draggable: true,
width: 48,
height: 48
});

pin.title = sName;

var pinInfobox = new Microsoft.Maps.Infobox(pin.getLocation(),
{
title: 'My Pushpin',
description: 'This pushpin is located at (0,0).',
visible: false,
offset: new Microsoft.Maps.Point(0, 15)
});

// Add handlers for the pushpin events
Microsoft.Maps.Events.addHandler(pin, 'click', function(e) {
this.setOptions({ visible: true });
}.bind(pinInfobox));
// Hide the infobox when the map is moved.
Microsoft.Maps.Events.addHandler(map, 'viewchange', function(e) {
this.setOptions({ visible: false });
}.bind(pinInfobox));
map.entities.push(pin);
map.entities.push(pinInfobox);
return pin;
}

关于javascript - Bing map 的信息框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10048259/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com