本文介绍了导入SQL数据到JS数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示的地图显示的标记的阵列。使用OSM用的OpenLayers库我'这样做。如果我使用静态值一切工作正常。但现在我想显示在一个SQL表标记。什么是获得数据并将其填充到JS数组的最佳方式?

I want to display a map that shows an array of markers. I'am using OSM with the OpenLayers Library to do that. If I use static values everything works fine. But now I want to display markers that are in a SQL table. What's the best way to get the data and fill it into the JS array?

这是我的code:

        <script>
        map = new OpenLayers.Map("mapdiv");
        map.addLayer(new OpenLayers.Layer.OSM());

        var size = new OpenLayers.Size(10,10);
        var icon = new OpenLayers.Icon('img/marker.gif', size);

        epsg4326 =  new OpenLayers.Projection("EPSG:4326"); //WGS 1984 projection
        projectTo = map.getProjectionObject(); //The map projection (Spherical Mercator)

        var lonLat = new OpenLayers.LonLat( 8.289166666666, 46.834444444444 ) .transform(epsg4326, projectTo);

        var zoom=8;
        map.setCenter (lonLat, zoom);

        // var markers = new OpenLayers.Layer.Markers( "Markers" );
        // map.addLayer(markers);

        // markers.addMarker(new OpenLayers.Marker(lonLat, icon));

        var vectorLayer = new OpenLayers.Layer.Vector("Overlay");

        // Define an array.
        var markers = [ 

        // SQL DATA NEEDS TO GO HERE! Formated like this: 
        //  [47.153339352283,8.51886974582752],
        //  [47.5047313406471,8.76598280071111],
        //  [47.3085363748528,8.598335445835]

        ];

        //Loop through the markers array
        for (var i=0; i<markers.length; i++) {

            var lon = markers[i][1];
            var lat = markers[i][0];

            var feature = new OpenLayers.Feature.Vector(
                    new OpenLayers.Geometry.Point( lon, lat ).transform(epsg4326, projectTo),
                    {description: "marker number " + i} ,
                    {externalGraphic: 'img/marker.gif', graphicHeight: 10, graphicWidth: 10, }
                );             
            vectorLayer.addFeatures(feature);
        }                        

        map.addLayer(vectorLayer);

    </script>

SQL表:

感谢您:)

推荐答案

好吧,我已经有了一个解决方案:
首先,我建在C#中的字符串一个StringBuilder并格式化得当我的JS数组。然后,我通过了串入JS:

Ok, I've got a solution:First I built the string in C# with a stringbuilder and formatted it properly for my JS-array. Then I passed that string into JS:

    var markers = [ <%=markers1%> ];

这篇关于导入SQL数据到JS数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:07