本文介绍了我的localhost中未正确加载GeoJSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我还在本地主机代码中添加了此向量,但未加载JSON文件。
I have also added this vector in my local host code but JSON file is not loaded.
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文件:
I have also added JSON file:
{
"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每个函数(添加几何):
use jQuery Each Function (add geometry):
$(data.features).each(function (index, key) {//jquery
geojsonfile = new OpenLayers.Format.GeoJSON
geojson.addFeatures([geojsonfile.parseFeature(data.features[index])]);
});
添加地图图层
map.addLayer(geojson);
这篇关于我的localhost中未正确加载GeoJSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!