gpt4 book ai didi

javascript - GeoJSON 文件未在我的本地主机中正确加载

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

我还在本地主机代码中添加了这个向量,但未加载 JSON 文件。

geojson_layer = new OpenLayers.Layer.Vector("features", {
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: myGeoJSON,
format: new OpenLayers.Format.GeoJSON()
})
});

我还添加了 JSON 文件:

{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "GMI_ADMIN": "GBR-SCT", "FIPS_CNTRY": "UK",
"CNTRY_NAME": "United Kingdom", "POP_RANK": 5, "ADMIN_NAME": "Scotland", "STATUS":
"Other", "PORT_ID": 32170, "CITY_NAME": "Dundee", "POP_CLASS": "100,000 to 250,000"
}, "geometry": { "type": "Point", "coordinates": [ -2.966700, 56.466702 ] } }
,
{ "type": "Feature", "properties": { "GMI_ADMIN": "GBR-SCT", "FIPS_CNTRY": "UK",
"CNTRY_NAME": "United Kingdom", "POP_RANK": 7, "ADMIN_NAME": "Scotland", "STATUS":
"Other", "PORT_ID": 33515, "CITY_NAME": "Hunterston", "POP_CLASS": "Less than 50,000" },
"geometry": { "type": "Point", "coordinates": [ -4.856786, 55.736744 ] } }
]}

最佳答案

添加矢量图层:

  geojson = new OpenLayers.Layer.Vector("features",, { 
styleMap: new OpenLayers.StyleMap({
'default': {
fillColor: '#659AB6',
fillOpacity: 0.6,
stroke: true,
strokeColor: '#3D5B6B',
strokeWidth: 3,
pointRadius: 5
}
}
) })

添加数据

data= {
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": { "GMI_ADMIN": "GBR-SCT", "FIPS_CNTRY": "UK",
"CNTRY_NAME": "United Kingdom", "POP_RANK": 5, "ADMIN_NAME": "Scotland", "STATUS":
"Other", "PORT_ID": 32170, "CITY_NAME": "Dundee", "POP_CLASS": "100,000 to 250,000"
}, "geometry": { "type": "Point", "coordinates": [ -2.966700, 56.466702 ] } }
,
{ "type": "Feature", "properties": { "GMI_ADMIN": "GBR-SCT", "FIPS_CNTRY": "UK",
"CNTRY_NAME": "United Kingdom", "POP_RANK": 7, "ADMIN_NAME": "Scotland", "STATUS":
"Other", "PORT_ID": 33515, "CITY_NAME": "Hunterston", "POP_CLASS": "Less than 50,000" },
"geometry": { "type": "Point", "coordinates": [ -4.856786, 55.736744 ] } }
]};

使用 jQuery Each 函数(添加几何):

$(data.features).each(function (index, key) {//jquery
geojsonfile = new OpenLayers.Format.GeoJSON
geojson.addFeatures([geojsonfile.parseFeature(data.features[index])]);

});

添加 map 层

  map.addLayer(geojson);

关于javascript - GeoJSON 文件未在我的本地主机中正确加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21448612/

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