I'm trying to have some coordinates giving through KML to project some polygons onto a map, and while the polygons is projected perfectly, no matter what I change in the kml nothing happens and I have to refresh the page anyways.
Here's my code :
var polygons;
map = new ol.Map({
render: 'canvas',
layers: [raster, vector],
target: 'map',
interactions: interactions,
view: view
});
var styling = new ol.style.Style({
fill: new ol.style.Fill({
color: [225, 85, 83, .4]
}),
stroke: new ol.style.Stroke({
color: [135, 13, 39, .8],
width: 2,
lineCap: 'round'
})
});
function testpolygons() {
var new_source = new ol.source.Vector({
url: 'pages/Coordinates.php',
format: new ol.format.KML({
extractStyles: false,
extractAttributes: false
})
});
var new_layer = new ol.layer.Vector({
source: new_source,
style: styling
});
map.addLayer(new_layer);
new_source.once('change', function() {
if (polygons) {
map.removeLayer(polygons);
}
polygons= new_layer;
});
if (polygons!== undefined)
polygons.setVisible(false);
}
testpolygons();
And this is what's my coordinates page has :
test 1 irrelativeToGround 10.416666666667,40.375 10.583333333333,40.891666666667 10.25,40.891666666667 10.25,40.058333333333 10.583333333333,40.058333333333 test 1 irrelativeToGround 10.25,40.391666666667 10.145833333333,40.491666666667 10.4,40.575 10.504166666667,40.475
And source code just for good measure :
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>test</name>
<Polygon>
<extrude>1</extrude>
<altitudeMode>irrelativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
10.416666666667,40.375 10.583333333333,40.891666666667 10.25,40.891666666667 10.25,40.058333333333 10.583333333333,40.058333333333
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
<Placemark>
<name>test</name>
<Polygon>
<extrude>1</extrude>
<altitudeMode>irrelativeToGround</altitudeMode>
<outerBoundaryIs>
<LinearRing>
<coordinates>
10.25,40.391666666667 10.145833333333,40.491666666667 10.4,40.575 10.504166666667,40.475
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
I've also tried adding "header('Content-type: application/vnd.google-earth.kml+xml');"
turning it into a KML file but to no effect, the "new_source.once('change', function(){})"
event doesn't update the polygons no matter how many polygons i add or how many coordinates I change, and I can't figure out why.
EDIT: Now this is embarrassing, seems all i had to do was to remove
if (polygons!== undefined)
polygons.setVisible(false);
And insert the "testpolygons();"
line into a setInterval.
Only took me a week and a half, to figure out.
Now the only thing is that even if source does not change is still calls the function, but otherwise it works as it should.