I have the following very simple code:
#map_center = [-32.951106, -60.669952]
#map = new ol.Map({
target: 'map-canvas',
layers: [new ol.layer.Tile({source: new ol.source.OSM()})],
view: new ol.View({
center: #map_center,
zoom: 5
})
})
It's in coffeescript, but you will get the idea. The problem is, the map does not center at all. It gets stuck in [0, 0]
Am I doing something wrong?
By default the view's projection is Web Mercator (EPSG:3857). This means the view center's coordinates should be expressed in that projection.
If you have latitudes and longitudes you can use the ol.proj.transform function to transform the latitudes longitudes to Web Mercator coordinates. For example:
var view = new ol.View({
zoom: 4,
center: ol.proj.transform([-60, -32], 'EPSG:4326', 'EPSG:3857')
});
Related
I have my map object created like this:
new ol.Map({
...
view: new ol.View({
center: ol.proj.transform([15,49], 'EPSG:4326', 'EPSG:3857'),
zoom: 10,
minZoom: 7,
maxZoom: 18,
extent: ol.proj.transformExtent([11.8, 48.4, 19.2, 51.2], 'EPSG:4326', 'EPSG:3857')
})
});
Let's say I have a big polygon "A" ( see the picture below ). I have a smaller polygon "B", which sticks to one side of the polygon "A". An external system calculates polygon "B" coordinates using WGS coordinates - points "pt1" and "pt2" are positioned on connection between point "pt3" and "pt4". When I draw both polygon on my map, points "pt1" and "pt2" are not positioned on connection between point "pt3" and "pt4". I think, it's because OpenLayers connect points "pt3" and "pt4" by direct line. This connection is straight line on the globe, so on my map, it should be curve. And that's why, I think, the polygon "B" is not aligned with the polygon "A" although it is on the globe. Is there a way, how to fix this?
I want to limit rendering of a vector layer to a bounding box, but the extent property on ol.layer.Vector does not change the geographic bounds of what is rendered in the vector layer.
Here is a simple example:
var base = new ol.layer.Vector({
source: new ol.source.Vector({
format: new ol.format.TopoJSON(),
url: 'https://gist.githubusercontent.com/quizzicol/7068241/raw/4f6ced3a2412a48141843bee07f6ae2034eea21b/world110.json'
}),
extent: [-180, 0, 180, 70]
});
var map = new ol.Map({
layers: [base],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 1
})
});
And here it is in action: http://codepen.io/mikeskaug/pen/XKOGLN. I expected this to limit rendering to objects in the northern hemisphere, yet the entire world is still rendered.
What am I missing? I'm using OpenLayers 3.17.1
I just want to convert the default projection of an Openlayers 3.9.0 from the default EPSG:3857 to EPSG:4326.
So I edited a basic code like
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var center = [-1.812, 52.443];
var proj = new ol.proj.Projection({
code: 'EPSG:4326',
units: 'm'
});
var view = new ol.View({
center: center,
zoom: 6,
projection:proj
});
var map = new ol.Map({
loadTilesWhileAnimating: false,
loadTilesWhileInteracting:false,
target: 'map',
layers: [layer],
view: view
});
If center is like var center = [-1.812, 52.443]; it does not go in the UK, as is should be, it goes in the center of the map.
If I do like var center = new ol.geom.Point(-1.812, 52.443); I see no map at all. What am I missing here?
Thanks
You have two issues:
You should not instantiate the EPSG:4326 projection by yourself, it's done by OpenLayers 3. You get the projection by calling ol.proj.get('EPSG:4326').
The ol.source.OSM source loads it's tiles from services that only support EPSG:3857. Since it's a XYZ-based tilesource, you might actually get the map working (if the tilecoords are valid), but the layer will not be positioned correctly and still be in EPSG:3857. You can use EPSG:4326 as the view projection, but then you have to use a background map that supports it.
A working demo can be found in the official examples.
OL does not currently transform tiles, but that is being worked.
https://github.com/openlayers/ol3/issues/3785
I'm using Openlayer 3.5 and load an OSM map "EPSG:3857".
var extent = [116.826673, 4.854776, 126.748593, 18.697146];
var philiExtent = ol.extent.applyTransform(extent, ol.proj.getTransform("EPSG:4326", "EPSG:3857"));
var view = new ol.View({
center: ol.proj.transform([121.787633, 11.775961], 'EPSG:4326', 'EPSG:3857'),
zoom: 0,
extent: philiExtent,
resolutions: [2560, 1280, 640, 320, 160, 80, 40, 20, 10, 5, 2.5, 1.2, 0.6],
});
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map'
});
But my features from webService are in "EPSG:4326"
function showData(data) {
var format = new ol.format.WKT();
var feature;
$.each(data, function (i, link) {
feature = format.readFeature(link.geom);
wktTraffic.addFeature(feature);
})
console.log('done load map');
}
So how I make the map be on 4326 or the new feature be on 3857.
I prefer first option.
Check out the FAQ section: http://openlayers.org/en/master/doc/faq.html#how-do-i-change-the-projection-of-my-map-
How do I change the projection of my map?
There is a good chance that you want to change the default projection of OpenLayers to something more appropriate for your region or your specific data.
The projection of your map can be set through the view-property. Here are some examples:
// OpenLayers comes with support for the World Geodetic System 1984, EPSG:4326:
var map = new ol.Map({
view: new ol.View({
projection: 'EPSG:4326'
// other view properties like map center etc.
})
// other properties for your map like layers etc.
});
// To use other projections, you have to register the projection in OpenLayers:
//
// By default OpenLayers does not know about the EPSG:21781 (Swiss) projection.
// So we create a projection instance for EPSG:21781 and pass it to
// ol.proj.addProjection to make it available to the library for lookup by its
// code.
var swissProjection = new ol.proj.Projection({
code: 'EPSG:21781',
// The extent is used to determine zoom level 0. Recommended values for a
// projection's validity extent can be found at http://epsg.io/.
extent: [485869.5728, 76443.1884, 837076.5648, 299941.7864],
units: 'm'
});
ol.proj.addProjection(swissProjection);
// we can now use the projection:
var map = new ol.Map({
view: new ol.View({
projection: swissProjection
// other view properties like map center etc.
})
// other properties for your map like layers etc.
});
We recommend to lookup parameters of your projection (like the validity extent) over at epsg.io.
To reproject your features to EPSG:3857, you can set the options dataProjection and featureProjection when parsing the features from the WKT string. See also ol.format.WKT#readFeature
var format = new ol.format.WKT();
var feature;
$.each(data, function (i, link) {
feature = format.readFeature(link.geom, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:3857'
});
wktTraffic.addFeature(feature);
})
I am trying to make web map application with openlayers 3. I have a problem which i want to fix.
I am loading OSM layer as my base map of the application. But the problem with the OSM layer is that it shows whole world and I can pan my map all around the world.
I want my application to be fixed in certain part. I have map extent set but still it doesn't work.
I am using minZoom but it doesn't help.
Is there any other way to fix this prolem?
var centerpos = [84.2, 28.2];
var newpos = ol.proj.transform(centerpos,'EPSG:4326','EPSG:900913');
var baseLayerOSM = new ol.layer.Tile({
source: new ol.source.MapQuest({
layer: 'osm'
}),
isBaseLayer:true
});
var map = new ol.Map({
layers: [baseLayerOSM],
target: 'map',
controls: [new CustomControl()],
view: new ol.View({
extent:[80.05844110726194,26.34796712822462,88.2015218371264,30.44742963310623],
projection : 'EPSG:900913', // OSM projection
center : newpos,
minZoom:7,
zoom: 7
})
});
This is my code.
AJ
Basically your example should work, but I think you forgot to transform your extent to EPSG:900913 / EPSG:3857 as well.
var maxExtent = [80.05844110726194,26.34796712822462,88.2015218371264,30.44742963310623];
var map = new ol.Map({
layers: [baseLayerOSM],
target: 'map',
controls: [new CustomControl()],
view: new ol.View({
extent: ol.proj.transformExtent(maxExtent, 'EPSG:4326', 'EPSG:900913'),
projection : 'EPSG:900913', // OSM projection
center : newpos,
minZoom:7,
zoom: 7
})
});