Import SQL Data into JS Array

二次信任 提交于 2019-12-13 06:30:24

问题


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?

This is my 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 table:

Thank you :)


回答1:


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%> ];


来源:https://stackoverflow.com/questions/26138416/import-sql-data-into-js-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!