How to add the KML layer for Openlayers switcher? - javascript

how can i add a KML layer to the switcher by ol-ext Viglino?
When I add:
var vectorSource = new ol.layer.Vector({
source: new ol.source.Vector({
url: "IG1.kml",
format: new ol.format.KML()
})
The layer does not appear on the map.

To appear in the layer switcher the layer must have a title property
var vectorSource = new ol.layer.Vector({
title: "KML Layer",
source: new ol.source.Vector({
url: "IG1.kml",
format: new ol.format.KML()
})
})
If the layer is not appearing on the map check that .kml extension is enabled in your server MIME types.

Try :
import KML from 'ol/format/KML';
import VectorSource from 'ol/source/Vector';
import {Vector as VectorLayer} from 'ol/layer';
const vector = new VectorLayer({
source: new VectorSource({
url: 'IG1.kml',
format: new KML(),
}),
});

Related

How to load geojson file as a Tile Layer

I am trying to load the big GeoJSON file using the OpenLayers. The problem is after loading it then the complete map becomes slow to do any other operation. So my question is there anyway to load a geojson file as a Tile rather ?
Here is my code:
function addGEOJSONLayer() {
console.log("reached");
var url= 'http://172.61.25.51:8080/test/ab.json';
var vectorSource = new ol.source.Vector({
strategy: ol.loadingstrategy.bbox,
projection: 'EPSG:4326',
loader: function(extent, resolution, projection) {
$.ajax(url).then(function(response) {
console.log(response);
var features = new ol.format.GeoJSON().readFeatures(response);
vectorSource.addFeatures(features);
});
}
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: function (feature, resolution) {
return [new ol.style.Style({
stroke: new ol.style.Stroke({
width: 1,
color: feature.get('stroke')
}),
fill: new ol.style.Fill({
color: feature.get('fill'),
})
})];
}
});
var ext = vectorLayer.getSource().getExtent();
map.getView().fit(ext, map.getSize());
map.addLayer(vectorLayer);
}
Any suggestions will be of great help! Thanks

Add Layer to OpenLayers with Stamen "Burning Map"

as my title assert
I would like to add a new "Burning" level of type stamen to my OpenLayers map. With others layer i.e watercolor/toner atc, it work, but not with Burning.
My code for doing this is:
var stamenLayers = new ol.layer.Group({
title: 'Stamen',
layers: [
new ol.layer.Tile({
source: new ol.source.Stamen({
layer: 'burning' // toner terrain toner-lite watercolor <-- this work
})
}),
new ol.layer.Tile({
source: new ol.source.Stamen({
layer: 'terrain-labels'
})
})
]
});

Changing Source url of a XYZ layer and redrawing the layer/map?

I wanna change the url of my ol3 map source. I've tryed using things such as map.set or map.getlayers().set or whatever but I can't seem to find a way to access my source object. Here's the code:
function init() {
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
projection: 'PIXELS',
tileGrid: mapTileGrid,
url: loc
})
})
],
view: new ol.View({
projection: ol.proj.get('PIXELS'),
extent: mapExtent,
maxResolution: mapTileGrid.getResolution(0)
})
});
map.getView().fit(mapExtent, map.getSize());
console.log(map.get("layergroup").get("layers").get("url"));
map.get("layergroup").get("layers").set("url",loc);
}
What's a way to change the url property and reload the layer ?
I also tried using the setSource function as in the following answer : here
but it seems to not work (can't setSource of undefined).
try the following
function init() {
var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.XYZ({
projection: 'PIXELS',
tileGrid: mapTileGrid,
url: loc
})
})
],
view: new ol.View({
projection: ol.proj.get('PIXELS'),
extent: mapExtent,
maxResolution: mapTileGrid.getResolution(0)
})
});
map.getView().fit(mapExtent, map.getSize());
//get alll the layers exist on your map
var layers = map.getLayers();
//lets assume you want to set the url for the first layer found
layers[0].getSource().setUrl(loc);
}

OpenLayers-3 - What is the correct usage of source.clear()

I have a ol 3.10.1 map where the goal is to redraw the features of a layer dynamically. On the road to get there, I'm using the source.clear() function. The strange thing is that the source.clear() actually clear the features from the layer at the current zoom level, but while zooming in or out the features are still there. Am I using the source.clear() function the correct way? Please find bellow the code snippet which I'm using for testing purposes.
var image = new ol.style.Circle({
radius: 5,
fill: null,
stroke: new ol.style.Stroke({color: 'red', width: 1})
});
var styles = {
'Point': [new ol.style.Style({
image: image
})]};
var styleFunction = function(feature, resolution) {
return styles[feature.getGeometry().getType()];
};
var CITYClusterSource = new ol.source.Cluster({
source: new ol.source.Vector({
url: 'world_cities.json',
format: new ol.format.GeoJSON(),
projection: 'EPSG:3857'
}),
})
var CITYClusterLayer = new ol.layer.Vector({
source: CITYClusterSource,
style: styleFunction
});
setTimeout(function () { CITYClusterSource.clear(); }, 5000);
var map = new ol.Map({
target: 'map',
renderer: 'canvas',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
CITYClusterLayer
],
view: new ol.View({
center: ol.proj.transform([15.0, 45.0], 'EPSG:4326', 'EPSG:3857'),
zoom:3
})
});
I'm using the setTimout() function to have the features visible for some seconds, before they are supposed to be cleared.
Please advice.
UPDATE: http://jsfiddle.net/jonataswalker/ayewaz87/
I forgot, OL will load your features again and again, for each resolution. So if you want to remove once and for all, use a custom loader, see fiddle.
Your source is asynchronously loaded, so set a timeout when it's ready:
CITYClusterSource.once('change', function(evt){
if (CITYClusterSource.getState() === 'ready') {
// now the source is fully loaded
setTimeout(function () { CITYClusterSource.clear(); }, 5000);
}
});
Note the once method, you can use on instead of it.

How can i assign a logo to the vector source (GPX) in OpenLayers 3?

im learing OpenLayers 3 and i am trying to assign an png image to the logo attribute of the layer.Vector.source like this:
var vectorSpeedLimit40 = new ol.layer.Vector({
title: 'speedlimit40',
source: new ol.source.Vector({
url: 'gpx/Fotoboks_40.gpx',
format: new ol.format.GPX({
extraStyles: false
}),
logo: '/imgs/lc.png'
})
});
var rasterLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
layers: [rasterLayer, vectorSpeedLimit40],
target: document.getElementById('map-canvas'),
view: new ol.View({
center: [0, 0],
zoom: 3
})
});
Where i thought this would show instances of the png, it shows small blue circles instead like this:
I have checked, double checked, triple checked and the path is correct relative to the client.
What am i doing wrong? Thanks!
To assign a specific image to a vectorLayer with a GPX source you have to assign the image property a new ol.style.Icon with the src attribute to the image like this:
var vectorSource = new ol.source.Vector({
// Specify that the source is of type GPX
format: new ol.format.GPX(),
// Here you specify the path to the image file
url: 'gpx/source.gpx'
})
var vectorStyle = new ol.style.Style({
// Set the image attribute to a new ol.style.Icon
image: new ol.style.Icon(/** #type {olx.style.IconOptions} */ ({
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
scale: 0.2,
// Here you specify the path to the image file
src: 'imgs/image.png'
}))
})
var vectorLayer = new ol.layer.Vector({
// Here you specify the source and style attributes of the ol.layer.Vector to the ones that are made above
source: vectorSource,
style: vectorStyle
});
// Then add it to the map
map.addLayer(vectorLayer);

Categories