I have a GeoJSON from NaturalEarth for world state boundaries and I want to display it with only without backgroung. But also, I want to add a OSM layer as another baselayer option.
The code I am using is the following one, but when I check it, as OSM is in another projection, the center map does not work. However if I change the projection of "mapDiv" the one that does not work is the vector layer with the GeoJSON information.
<script type="text/javascript">
var lon = 5;
var lat = 40;
var zoom = 5;
var map;
function Initialize(){
// Map
map = new OpenLayers.Map('mapDiv', {
controls:[
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanZoomBar(),
new OpenLayers.Control.LayerSwitcher()]
} );
// OSM
var osmLayer = new OpenLayers.Layer.OSM("OSM");
map.addLayer(osmLayer);
// GeoJSON
var geojsonLayer = new OpenLayers.Layer.Vector("GeoJSON", {
isBaseLayer: true,
strategies: [new OpenLayers.Strategy.Fixed()],
protocol: new OpenLayers.Protocol.HTTP({
url: "world_0.json",
format: new OpenLayers.Format.GeoJSON()
})
});
map.addLayer(geojsonLayer);
// Options
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
}
</script>
I have try something similar to this example, but I continue having problems when switching between the layers.
The GeoJSON has been created with SHP files from NaturalEarth with ogr2ogr command.
ogr2ogr -f GeoJSON -s_srs EPSG:4326 -t_srs EPSG:900913 xxxx.json yyyy.shp. However, it will be great it someone could solve the problem without requiring the SRS change. – iblasi Jun 25 '15 at 18:26format: new OpenLayers.Format.GeoJSON()byformat: new OpenLayers.Format.GeoJSON({ 'internalProjection': new OpenLayers.Projection("EPSG:900913"), 'externalProjection': new OpenLayers.Projection("EPSG:4326") });– Germán Carrillo Jun 25 '15 at 19:31