I am setting up a website using Openlayers and GeoJSON files. When I get the GeoJSON file using the code below it work as expected.
url: "./file.geojson",
But if I try and use the full domain call, see below, it does not work. Why?
url: "http://www.domain.com/file.geojson",
I need to use the full domain call because I am trying to test it locally on my computer. Is there a way to get the file locally from my computer?
<!DOCTYPE html>
var lon = -80.233, lat = 40.665, zoom = 15, epsg4326 = new OpenLayers.Projection('EPSG:4326'), epsg900913 = new OpenLayers.Projection('EPSG:900913'); var arrayAerial; var select;
function init(){
var map = new OpenLayers.Map('map', {
units: 'm',
numZoomLevels: 19,
controls: [
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.ScaleLine(),
new OpenLayers.Control.MousePosition(),
new OpenLayers.Control.LayerSwitcher(),
//new OpenLayers.Control.OverviewMap(),
new OpenLayers.Control.PanZoomBar()
],
projection: epsg900913,
displayProjection: epsg4326 //Is used for displaying coordinates in appropriate CRS by MousePosition control
});
arrayOSM = ["http://otile1.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg",
"http://otile2.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg",
"http://otile3.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg",
"http://otile4.mqcdn.com/tiles/1.0.0/map/${z}/${x}/${y}.jpg"];
arrayAerial = ["http://otile1.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg",
"http://otile2.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg",
"http://otile3.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg",
"http://otile4.mqcdn.com/tiles/1.0.0/sat/${z}/${x}/${y}.jpg"];
baseOSM = new OpenLayers.Layer.OSM("MapQuest-OSM Tiles", arrayOSM);
baseAerial = new OpenLayers.Layer.OSM("MapQuest Open Aerial Tiles", arrayAerial);
var sanpipe_layer = new OpenLayers.Layer.Vector("San Sewer", {
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: 5,
fillColor: "#2da725",
fillOpacity: 0.8,
strokeColor: "#2da725",
strokeWidth: 3,
strokeOpacity: 0.8 } ),
}),
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./sanpipe.geojson",
format: new OpenLayers.Format.GeoJSON()
})
});
var mh_layer = new OpenLayers.Layer.Vector("Manholes", {
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: 5,
fillColor: "#9de24f",
strokeColor: "#0d4cd6",
strokeWidth: 1,
strokeOpacity: 0.8 } ),
}),
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./mh.geojson",
format: new OpenLayers.Format.GeoJSON({
extractAttributes: true
})
})
});
var waterline_layer = new OpenLayers.Layer.Vector("waterline", {
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: 5,
fillColor: "#00FF01",
fillOpacity: 0.8,
strokeColor: "#0d4cd6",
strokeWidth: 3,
strokeOpacity: 0.8 } ),
}),
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./waterline.geojson",
format: new OpenLayers.Format.GeoJSON()
})
});
var valves_layer = new OpenLayers.Layer.Vector("Valves", {
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: 3,
fillColor: "#fdf700",
strokeColor: "#0d4cd6",
strokeWidth: 1,
strokeOpacity: 0.8 } ),
}),
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./valves.geojson",
format: new OpenLayers.Format.GeoJSON()
})
});
var hyd_layer = new OpenLayers.Layer.Vector("Hydrants", {
styleMap: new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
pointRadius: 5,
fillColor: "#d81417",
strokeColor: "#0d4cd6",
strokeWidth: 1,
strokeOpacity: 0.8 } ),
}),
projection: epsg4326,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "./hyd.geojson",
format: new OpenLayers.Format.GeoJSON()
})
});
map.addLayer(baseOSM);
map.addLayer(baseAerial);
map.addLayer(sanpipe_layer);
map.addLayer(mh_layer);
map.addLayer(waterline_layer);
map.addLayer(valves_layer);
map.addLayer(hyd_layer);
select = new OpenLayers.Control.SelectFeature(mh_layer);
mh_layer.events.on({
"featureselected": onFeatureSelect,
"featureunselected": onFeatureUnselect
});
map.addControl(select);
select.activate();
map.setCenter(new OpenLayers.LonLat(lon, lat).transform(epsg4326, epsg900913), zoom);
}
function onPopupClose(evt) {
select.unselectAll();
}
function onFeatureSelect(event) {
var feature = event.feature;
popup = new OpenLayers.Popup.FramedCloud("chicken",
feature.geometry.getBounds().getCenterLonLat(),
null,
"<h2>"+feature.attributes.STRUCT_ID + "</h2>",
null, true, onPopupClose);
feature.popup = popup;
map.addPopup(popup);
}
function onFeatureUnselect(event) {
var feature = event.feature;
if(feature.popup) {
map.removePopup(feature.popup);
feature.popup.destroy();
delete feature.popup;
}
}
</script>