Thursday, 21 April 2016

openlayers 2 - Show a kml (which uses longitude and latitude EPSG:4326) in "google/OSM" projection (EPSG:3857) in open street map


I am showing a kml layer, over OSM. The points in the kml are latitude/longitude (EPSG:4326).



I don't have to convert any points or anything, it all just works. Great!


Now, the client also wants to be able to see the kml by itself (without any background map).


If I try to set the kml as the base layer, the country (England) looks squashed (at least, it is squashed compared to OSM). This doesn't surprise me, but I need it to look exactly the same regardless if OSM is the base layer or not.


Here is what I have at the moment:


var lon = 0;
var lat = 54;
var zoom = 6;
var kmlPath = 'E92000001.kml';
var map;
var projection4326 = new OpenLayers.Projection('EPSG:4326');

var projectionGoogle = new OpenLayers.Projection('EPSG:3857');

function init(){
map = new OpenLayers.Map({
projection: projectionGoogle,
div: "map",
layers: [
new OpenLayers.Layer.Vector("KML", {
strategies: [new OpenLayers.Strategy.Fixed()],
isBaseLayer: true,

//sphericalMercator: true,
projection: projection4326,
protocol: new OpenLayers.Protocol.HTTP({
url: kmlPath,
format: new OpenLayers.Format.KML()
})
})
]
});


map.setCenter(new OpenLayers.LonLat(lon, lat).transform(projection4326, projectionGoogle), zoom);

console.log(map.baseLayer.projection);
}

I have tried variations on this (setting the map projection to 3857, and the kml's to 3857 as well) but usually the kml doesn't render at all, or renders off screen (and still squashed).




No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...