基于 OpenLayers JSON 的结果,带有弹出窗口

发布于 2024-12-08 11:52:34 字数 2141 浏览 1 评论 0原文

我编写了代码,以便在地图上获得黄色点,坐标和其他一些事实是从外部文件加载的。

这是资源文件

var sites = [{id:1269209,geometry:{ type:"Point",lat:1,lon: 1},properties:{siteName:"Yttern",parentId:1269209,siteType:2}}];

这是用于查看它们的脚本

var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
$.getScript('test.js',function(){
    for(var i = 0; i < sites.length; i++)
    {
        var site = sites[i];
        var feature = new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(site.geometry.lon,site.geometry.lat));
    vectorLayer.addFeatures(feature);
}
    map.addLayer(vectorLayer);

});

我关心的是如何使用 JSON 文件中的内容为每个标记制作一个可悬停的弹出窗口? 是否也可以仅当用户超过缩放级别 14 时才发出请求?


现在,我已经修复了缩放超过 10 时启动的函数,

map.events.register("moveend", null, function(){

      if(map.zoom >= 10)
      {
        var bounds = map.getExtent();
        var ne = new OpenLayers.LonLat(bounds.right,bounds.top).transform(map.getProjectionObject(),new OpenLayers.Projection("EPSG:4326"));
        var sw = new OpenLayers.LonLat(bounds.left,bounds.bottom).transform(map.getProjectionObject(),new OpenLayers.Projection("EPSG:4326"));
        var vectorLayer = new OpenLayers.Layer.Vector("Layer");
        $.getScript('ajax.php?a=markers&type=javascript&sw=('+sw.lon+','+sw.lat+')&ne=('+ne.lon+','+ne.lat+')',function(){
        //$.getScript('test.js',function(){
            for(var i = 0; i < sites.length; i++)
            {
                var site = sites[i];
                var latlon = new OpenLayers.LonLat(site.geo.lon,site.geo.lat);
                var feature = new OpenLayers.Feature.Vector(
                    new OpenLayers.Geometry.Point(latlon)
                );
                vectorLayer.addFeatures(feature);
            }
            map.addLayer(vectorLayer);
        });
        }
    });

但我仍然没有在地图上看到标记,有什么问题吗? Markers.php 又名 ajax.php?a=markers.... 的一个结果

var sites = [{siteId:'9',siteName:'Hårleby',geo:{lon:11.641452694427471,lat:58.15782686109065},fact:{parentSiteId:0,county:'Orust'}}]; 

可能是结果的投影吗?请帮忙。

I've made the code so that I get yellow points on my map, the coords and some other facts is loaded from a external file.

Here's the resource file

var sites = [{id:1269209,geometry:{ type:"Point",lat:1,lon: 1},properties:{siteName:"Yttern",parentId:1269209,siteType:2}}];

And here's the script for viewing them

var vectorLayer = new OpenLayers.Layer.Vector("Overlay");
$.getScript('test.js',function(){
    for(var i = 0; i < sites.length; i++)
    {
        var site = sites[i];
        var feature = new OpenLayers.Feature.Vector(
        new OpenLayers.Geometry.Point(site.geometry.lon,site.geometry.lat));
    vectorLayer.addFeatures(feature);
}
    map.addLayer(vectorLayer);

});

The thing I woundering about is how do I do to make a hoverable popup for each marker, with the content from the JSON file?
Is it also possible to make the request only when the user have passed zoom level 14?


Now, I've fixed the function that starts when zoom passed 10

map.events.register("moveend", null, function(){

      if(map.zoom >= 10)
      {
        var bounds = map.getExtent();
        var ne = new OpenLayers.LonLat(bounds.right,bounds.top).transform(map.getProjectionObject(),new OpenLayers.Projection("EPSG:4326"));
        var sw = new OpenLayers.LonLat(bounds.left,bounds.bottom).transform(map.getProjectionObject(),new OpenLayers.Projection("EPSG:4326"));
        var vectorLayer = new OpenLayers.Layer.Vector("Layer");
        $.getScript('ajax.php?a=markers&type=javascript&sw=('+sw.lon+','+sw.lat+')&ne=('+ne.lon+','+ne.lat+')',function(){
        //$.getScript('test.js',function(){
            for(var i = 0; i < sites.length; i++)
            {
                var site = sites[i];
                var latlon = new OpenLayers.LonLat(site.geo.lon,site.geo.lat);
                var feature = new OpenLayers.Feature.Vector(
                    new OpenLayers.Geometry.Point(latlon)
                );
                vectorLayer.addFeatures(feature);
            }
            map.addLayer(vectorLayer);
        });
        }
    });

but still I dont get the markers on the map, is something wrong?
One result from markers.php aka ajax.php?a=markers....

var sites = [{siteId:'9',siteName:'Hårleby',geo:{lon:11.641452694427471,lat:58.15782686109065},fact:{parentSiteId:0,county:'Orust'}}]; 

is it the projection on the result maybe? Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文