将SQL数据导入JS数组

时间:2014-10-01 09:41:06

标签: javascript sql arrays openlayers openstreetmap

我想显示一个显示标记数组的地图。我正在使用OpenMayers Library的OSM来做到这一点。如果我使用静态值,一切正常。但现在我想显示SQL表中的标记。获取数据并将其填入JS数组的最佳方法是什么?

这是我的代码:

        <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表:

http://i.stack.imgur.com/OLZIc.png

谢谢:)

1 个答案:

答案 0 :(得分:1)

好的,我有一个解决方案: 首先,我使用stringbuilder在C#中构建了字符串,并为我的JS数组正确格式化了它。然后我将该字符串传递给JS:

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