Open Layer 3 blank fields when dragging map, how to restrict it - javascript

I don't want that the users see the blank field when dragging the map. I want restrict it. I didn't find any great resolution.
my map code:
var map = new ol.Map({
layers: [new ol.layer.Tile({
source: new ol.source.MapQuest({ layer: 'osm' }) }), vectorLayer],
target: document.getElementById('map'),
controls: ol.control.defaults().extend([
new ol.control.ScaleLine(),
new ol.control.ZoomSlider()
]),
view: new ol.View({
center: [-6217890.205764902, -1910870.6048274133],
zoom: 3,
maxZoom: 20
})
});
Problem:

There are a couple of things that you could do:
Set a background color or image on your map container like in the examples to make the white gaps look nicer.
.map {
...
background: url(map-background.jpg) repeat;
}
Set the minZoom property on the view to prevent the map from being zoomed out too far. But on large screens users might still see gaps.
view: new ol.View({
...
minZoom: 4
})
Or restrict the view extent to the tile boundaries (ol.proj.get('EPSG:3857').getExtent()). This is not yet included in OpenLayers, but you'll find a work-around in this question.

Related

How do I automate view.animate in OpenLayers5?

I have a list of coordinates I want my code to pan through. I can place view.animate and do it once to pan from my current center to the first coordinate but not to keep cycling through the list. Any ideas? I got the idea from this https://openlayers.org/en/latest/examples/animation.html.
var coordinates = [london, moscow, istanbul, rome, bern]; (they have their coordinates)
var view = new ol.View({
center: istanbul,
zoom: 4
})
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})],
loadTilesWhileAnimating: true,
view: view
});
coordinates.forEach(function(element) {
setTimeout(view.animate({
center: element,
duration: 2000,
}), 10000);
});
You have a basic error in that setTimeout will be called repeatedly in quick succession, with each one calling view.animate after a 10000 ms delay - the map will become overloaded with animate requests.
Here is a working example using setInterval which ensures that panToNextlocation is called only once per 3000ms
https://stackblitz.com/edit/ol-animation-between-points

Image is displayed wrong on static image layer on top of osf layer

I followed the example from the openlayers book to add a static image on top of an osf layer. It works, but my image (256x256) is displayed as a rectangle. I tried around with the coordinates for the projection and checked out other posts here on, but I can't get my head around it why the image is not displayed as a square:
// Create map
var map = new ol.Map({
target: 'map', // The DOM element that will contains the map
renderer: 'canvas', // Force the renderer to be used
layers: [
// Add a new Tile layer getting tiles from OpenStreetMap source
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
// Create a view centered on the specified location and zoom level
view: new ol.View({
center: ol.proj.transform([16.3725, 48.208889], 'EPSG:4326', 'EPSG:3857'),
zoom: 4
})
});
// Create an image layer
var imageLayer = new ol.layer.Image({
opacity: 0.75,
source: new ol.source.ImageStatic({
attributions: [
new ol.Attribution({
html: ''
})
],
url: 'https://placebear.com/256/256',
imageSize: [256, 256],
projection: map.getView().getProjection(),
imageExtent: ol.extent.applyTransform(
[0, 0, 16.3725, 48.208889], ol.proj.getTransform("EPSG:4326", "EPSG:3857"))
})
});
map.addLayer(imageLayer);
ol.source.ImageStatic was made to put a georeferenced image (e.g. a scan of a historic map) on a map. Is this what you have in mind? If you just want to display an image and anchor it to a location on the map, you'd better use ol.Overlay or an ol.layer.Vector with a feature with an ol.style.Icon.
That said, your image will only be displayed as square if the imageExtent set on your ol.source.ImageStatic results in a square on the projected map view.

openlayers 3: don't zoom in outside the image

I'm using openlayers to create a map of a body. It shows the mri-scan of a person en on there are a lot of markers.
Totally zoomed out, there's a lot of whitespace (or in my case black space as the background is black) I've set up openlayers with the extent parameter, so users are not able to pan further than the image is big. However when the user hovers over the whitespace around it, and scrolls to zoom, the image is zoomed into based on the position of the cursor, so the body is out of view. Once the user starts panning again, the extent comes in to play and jumps back to the edge of the image.
Is there a way to disable zooming in/out when the cursor is outside the extent? Or if that is not possible, how can I trigger a move/pan so it jumps automatically to the edge of the image?
EDIT 14/12/2016: Created a JSFiddle to illustrate the issue.
And the code that is running there:
var pbm = {};
pbm.vars = {};
pbm.vars.extent = [0, 0, 1536, 5120];
pbm.vars.projection = new ol.proj.Projection({
code: 'xkcd-image',
units: 'pixels',
extent: pbm.vars.extent
});
/**
* initMap
* --------
* triggered by pbm.parseCases!
* outputs the bodymap to the #body-div
* this functions needs all the other ol-elements to work!
*/
pbm.initMap = function(){
pbm.map = new ol.Map({
layers: [
new ol.layer.Image({
source: new ol.source.ImageStatic({
url: 'https://s3-eu-west-1.amazonaws.com/norvell-philips/bodymap-static-hiress.jpg',
projection: pbm.vars.projection,
imageExtent: pbm.vars.extent
})
})
],
target: 'body',//Div#body
view: new ol.View({
projection: pbm.vars.projection,
center: ol.extent.getCenter(pbm.vars.extent),
zoom: 2,
maxZoom: 5,
minZoom: 1,
extent: pbm.vars.extent
}),
renderer: 'canvas',
controls: ol.control.defaults({attribution:false, rotate: false, zoom:false}),
interactions: pbm.vars.interactions
});
}
$(document).ready(function(){
pbm.initMap();
});
This is a bug that was recently fixed. The first release to contain the fix will be v3.21.0. The ticket with links to the fixes is #5824.

OpenLayers 3.13: issue with bindTo by linking two views

I'm new in using OpenLayers. I'm trying to do some exercises with version 3.0 and 3.13.
I have to link two views: the second map respond to changes in the first map, but zoomed out three times; when the first map is panned or zoomed, the second map should center on the same location and stay zoomed out in three levels.
I'm using the following code that works quite well on version 3.0, but not on v3.13: the console prints Uncaught TypeError: view2.bindTo is not a function.
In another example I use map2.bindTo('view', map); on v3.13, without any issue. What is the difference?
EDIT
I'm wrong, I obtain the same issue.
There is no bindTo anymore (see the comment by Jonatas Walker for details).
var layer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var london = ol.proj.transform([-0.12755, 51.507222], 'EPSG:4326', 'EPSG:3857');
var view = new ol.View({
center: london,
zoom: 6,
});
var view2 = new ol.View({
center: london,
zoom: 3,
});
var map = new ol.Map({
target: 'map1',
layers: [layer],
view: view,
//renderer: 'dom'
});
var map2 = new ol.Map({
target: 'map2',
layers: [layer],
controls: new ol.Collection(),
interactions: new ol.Collection(),
view: view2
});
view2.bindTo('center', view);
view.on('change:resolution', function(){
var zoom = this.getZoom();
if (zoom >= 3 && zoom <= 18)
view2.setZoom(this.getZoom()-3);
else view2.setZoom(this.getZoom());
});
Since PR #3472 there's no bindTo method and you can achieve this center binding with something like:
view.on('change:center', function(evt){
view2.setCenter(view.getCenter());
});

Openlayers3 with custom tile server

I've got a problem with the latest openlayers3 beta. I'm trying to use custom tile server using xyz layer. The thing is the tiles are re not rendered for some reason. Using firebug I can see that the tile requests are send and the images are fetched succesfuly, though they do not show up...
Everything works in chrome however.
var baseLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://xx.xx.xx.xx:33333/osm/{z}/{x}/{y}.png'
})
});
var map = new ol.Map({
controls: ol.control.defaults().extend([
]),
target: 'map',
layers: [baseLayer],
view: new ol.View2D({
center: ol.proj.transform([21.999529, 50.041682], 'EPSG:4326', 'EPSG:3857'),
zoom: 13
})
});
Can you try putting a minus before your tile url's y value?
var baseLayer = new ol.layer.Tile({
source: new ol.source.XYZ({
url: 'http://xx.xx.xx.xx:33333/osm/{z}/{x}/{-y}.png'
})
});
EDIT: Wow... Super old question... My bad, but maybe someone can use this answer anyway....

Categories

Resources