gpt4 book ai didi

javascript - 在加载 map 时打开一个弹出窗口

转载 作者:行者123 更新时间:2023-12-03 12:27:06 26 4
gpt4 key购买 nike

我用 OpenLayers 创建了一张 map 。一切都很好!
但我需要在 map 加载时默认打开一个弹出窗口,但我不知道如何打开。

由于我动态创建和销毁弹出窗口,因此我尝试模拟图钉上的单击,但没有成功。

大家有什么想法吗?

代码

function createMap() {
var options = {
theme: null
};
map = new OpenLayers.Map('map', options);
map.addLayer(new OpenLayers.Layer.OSM());
epsg4326 = new OpenLayers.Projection('EPSG:4326');
projectTo = map.getProjectionObject();
var lonLat = new OpenLayers.LonLat(mark.longitude, mark.latitude).transform(epsg4326, projectTo);
var zoom = 15;
map.setCenter(lonLat, zoom);
var vectorLayer = new OpenLayers.Layer.Vector('Overlay');

for(x=0; x<stores.length; x++) {
var feature = new OpenLayers.Feature.Vector(
new OpenLayers.Geometry.Point(stores[x].lon, stores[x].lat).transform(epsg4326, projectTo),
{description: stores[x].desc},
{externalGraphic: 'marker.png', graphicHeight: 40, graphicWidth: 20, graphicXOffset: -10, graphicYOffset: -20}
);
vectorLayer.addFeatures(feature);
map.addLayer(vectorLayer);
};

var controls = {
selector: new OpenLayers.Control.SelectFeature(vectorLayer, {
onSelect: createPopup,
onUnselect: destroyPopup
})
};

map.addControl(controls['selector']);
controls['selector'].activate();
};

function createPopup(feature) {
feature.popup = new OpenLayers.Popup(
'pop',
feature.geometry.getBounds().getCenterLonLat(),
null,
'<div>' + feature.attributes.description + '</div>',
null,
false,
function() {
controls['selector'].unselectAll();
}
);
map.addPopup(feature.popup);
}

function destroyPopup(feature) {
feature.popup.destroy();
feature.popup = null;
}

Stores 是 ajax 调用返回的值,其中包含带有纬度、经度、地址、电话等的商店列表。

最佳答案

您需要从 OpenLayers.Layer.Vector (layerVector,在您的情况下)中选择最接近 map 中心的功能,然后创建一个弹出窗口并将弹出窗口添加到该功能,例如,

//select feature closest to center of map, ie, 
//find features[i] of Layer.Vector's features array.
var feature=layerVector.features[i];
addPopup(feature);

function addPopup(feature){

var popup = new OpenLayers.Popup.FramedCloud(
'pop',
feature.geometry.getBounds().getCenterLonLat(),
null,
'<div>' + feature.attributes.description + '</div>',
null,
true, //add a close box
null
);

feature.popup = popup;
map.addPopup(popup);
}

如果我正确理解了这个问题,这无论如何都是一种可能性。有一个很好的例子http://openlayers.org/dev/examples/light-basic.html显示了如何在事件监听器弹出窗口上添加鼠标,这使用相同的逻辑,但也已选择添加弹出窗口的功能。

关于javascript - 在加载 map 时打开一个弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24146114/

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