2014-01-30 95 views
1

我也在本地主機代碼中添加了此矢量,但未加載JSON文件。GeoJSON文件未在本地主機中正確加載

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 ] } } 
]} 
+0

檢查'url'財產。它看起來不正確。 – drnextgis

回答

3

添加矢量圖層:

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的每個功能(添加幾何):

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

}); 

添加地圖圖層protocol`對象的`

map.addLayer(geojson); 
相關問題