gpt4 book ai didi

javascript - 将SQL数据导入JS数组

转载 作者:行者123 更新时间:2023-12-03 11:44:01 24 4
gpt4 key购买 nike

我想显示一张显示标记数组的 map 。我使用 OSM 和 OpenLayers 库来做到这一点。如果我使用静态值,一切都会正常。但现在我想显示 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 表:

/image/OLZIc.png

谢谢:)

最佳答案

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

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

关于javascript - 将SQL数据导入JS数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26138416/

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