how to display TMS layer in Open Layers 3? - javascript

I want to display tms layer in ol3.
var layer = 'india:Indian_gdp_119';
var projection_epsg_no = '4326';
var map = new ol.Map({
target: 'map',
view: new ol.View({
center: ol.proj.transform([77.39907,28.58920], 'EPSG:4326', 'EPSG:3857'),
zoom:2 }),
layers: [new ol.layer.VectorTile({
style:simpleStyle,
source: new ol.source.VectorTile({
tilePixelRatio:1 ,
tileGrid: ol.tilegrid.createXYZ({maxZoom: 19}),
format: new ol.format.MVT(),
url: 'http://localhost/geoserver/gwc/service/tms/1.0.0/' + layer +
'#EPSG%3A'+projection_epsg_no+'#pbf/{z}/{x}/{-y}.pbf' }) })]});
In geoserver and ol3 pbf format is not working,getting same error in geoserver and ol3.
Error::
http://localhost/geoserver/gwc/service/tms/1.0.0/india:Indian_gdp_119#EPSG%3A4326#pbf/2/1/1.pbf 404 (Not Found)
Is there any mistake in this code.Thanks

Related

VectorLayer not showing with OpenLayers

I'm basically trying to reproduce the center example in my own JsFiddle
const geoJSON = {};
const features = new ol.format.GeoJSON().readFeatures(geoJSON);
const vectorSource = new ol.source.Vector({
features: features,
});
const style = new ol.style.Style({});
const vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: style,
});
let olView = new ol.View({
center: ol.proj.fromLonLat([6.6339863, 46.5193823]),
zoom: 4,
});
let olLayers = [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
vectorLayer
];
new ol.Map({
layers: olLayers,
view: olView,
target: 'app',
});
I must be missing somethig obvious, as the vector layer isn't showing in my fiddle.
If you pan down you will see the vector for Switzerland is in the wrong place
If using readFeatures you need to specify the feature projection so the geojson Lon/Lat coordinates are transformed to view projection
const features = new ol.format.GeoJSON().readFeatures(geoJSON, {featureProjection: olView.getProjection()});
https://jsfiddle.net/apowe4gj/

Getting typeError after adding layer to map

I go tthis type of error:
"Cannot read property 'closure_uid_521373967' of null".
This occures after i make source that contain no features yet.
var vectorSource = new ol.source.Vector({
});
after that i make a Layer
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
and then i init the map:
var map = new ol.Map({
layers: [new ol.layer.Tile({ source: new ol.source.OSM({url:"http://tile.openstreetmap.org/{z}/{x}/{y}.png"}) }), vectorLayer],
target: document.getElementById('map'),
view: new ol.View({
center: [0, 0],
zoom: 3
})
});
its seems to work fine in my home when it is simple as here , but when i try to make it work in my job , that the scanario is kind a bit more complicated , i lack that type of error that i mention before.
Please view this Snippet.
It is a good idea to separate layer definition from map instantiation. The latter makes things very convenient, but it makes things difficult for debugging.
With that in mind, I defined an OSM Layer and your Vector layer and added both to a map.
You don't need to use getElementById. You can simply supply the id of the div as the target of the map.
I'm not able to replicate your TypeError. My suspicion is that it came from the addition of the unnecessary getElementById, though.
Can you post a link to the code that's not working?
// layers
var vectorSource = new ol.source.Vector({
});
var vectorLayer = new ol.layer.Vector({
source: new ol.source.Vector({
url: vectorSource,
})
});
var osmLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var map = new ol.Map({
layers: [osmLayer, vectorLayer],
target: 'map',
view: new ol.View({
center: [0, 0],
zoom: 3
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.6.0/ol.js"></script>
<body>
<div id='map'>
</div>
</body>

Pass object arrays to template

I will try rephrase mine question from this post.
I'm using openlayers3 to display random building markers on the map. You can see it in this JSFIDDLE example.
This is the code for the map
/* Create the map */
// setting up coordinates for map to display
var city = ol.proj.transform([-73.920935,40.780229], 'EPSG:4326', 'EPSG:3857');
// setting up openlayer3 view
var view = new ol.View({
center: city,
zoom: 13
});
// Create the map
var map = new ol.Map({
target: 'map',
renderer: 'canvas',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: view
});
// Setup markers
var markers = new ol.layer.Vector({
tittle: 'City Apratments',
source: new ol.source.Vector({
features: [
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.927870, 40.763633])),
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.917356, 40.763958])),
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.915530, 40.779665])),
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.916045, 40.779372])),
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.919682, 40.777365])),
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.908980, 40.776013])),
}),
new ol.Feature({
geometry: new ol.geom.Point(ol.proj.fromLonLat([-73.917356, 40.763958])),
})
]
}),
style: new ol.style.Style({
image: new ol.style.Icon({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixel',
opacity: 0.75,
src: 'http://openlayers.org/en/v3.12.1/examples/data/icon.png',
})
})
});
map.addLayer(markers);
I need to display some data about the building (street address, picture of building, more info, etc, something like you can see when you interact whit the google map, when you click on the map and you trigger off canvas menu whit some info about that place), so when you interact and click on the marker template will bi rendered and on it you will see all the data for that object, I need to be able to display different data for different marker building.
QUESTION:
You can see mine markers are displayed trough openlayer3 source.Vector, how can I pass this array of objects to django-view then django-template from this javascript code, how is this possible?
I want to avoid django-orm and do this from js --> django-view --> django-template.
I goal is to render a template when you click on the marker, so you can see some data about the building, furthermore I want to change this template whenever I want.
I would like to understand how can I do this, can someone help, thank you.

how can i dynamically update a ol.Features geometry property ( coordinates ) in OpenLayers 3

Im just getting started with OpenLayers 3 and i am trying to dynamically update a Features geometry property with coordinates, obviously there is something that i am missing out because the Feature is not moving. Here is what i god so far:
Socket.IO
socket.on('mapData', function(mapData) {
if (mapisloaded) {
latLon = ol.proj.transform([mapData.lon, mapData.lat], 'EPSG:4326', 'EPSG:3857');
// Initiate latlong object with mapData
if (centerIsRequested) {
//Center map with mapData
};
// Update marker with latlong from mapData
};
});
OpenLayers 3 based on the Vector Icon Example
var latLon = ol.proj.transform([10.904108, 59.788187], 'EPSG:4326', 'EPSG:3857');
var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(latLon),
name: 'Null Island',
population: 4000,
rainfall: 500
});
var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** #type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'imgs/lc.png'
}))
});
iconFeature.setStyle(iconStyle);
var vectorSource = new ol.source.Vector({
features: [iconFeature]
});
var vectorLayer = new ol.layer.Vector({
source: vectorSource
});
var baseLayer = new ol.layer.Tile({
source: new ol.source.OSM()
});
var view = new ol.View({
center: latLon,
zoom: 18,
});
var map = new ol.Map({
target: 'map-canvas',
layers: [ baseLayer, vectorLayer ],
view: view
});
The changes are obviously not changing, but i know that magic does not exist it was just to put something down to start with.
How wouldi go forward accomplish this simple task? The only thing i want is the icon to update its position on the map when socket.io detects the new mapdata (mapData.lat, mapData.lon).
I have tried to dig into the different objects and read their properties both in the console and in the documentation, and i have searched here on Stackoverflow but sadly without luck. Do i hook into the iconFeature, or do i have to do this another way? Maybe something really easy is built in? Any help is much appreciated.
If you want to move an icon over the map, it's better you use an ol.Overlay for this. You can use marker.setPosition(coord) on each change.
A working fiddle. Click on map to change marker's position.

Using encoded polylines in OpenLayers 3

I am having trouble when trying to use encoded polylines in OpenLayers 3. I have an external source providing the encoded strings, but when I try to convert them into a feature and add them to a vector source, I get the following error:
Uncaught TypeError: Cannot read property 'ua' of undefined
Here is the current code I am using:
vectorSource = new ol.source.Vector();
var layers = [
new ol.layer.Tile({
name: "SKL Tile Server",
source: new ol.source.OSM({
url: "https://placeholder/osm/{z}/{x}/{y}.png",
crossOrigin: null
})
}),
new ol.layer.Vector({
name: "polylines",
source: vectorSource
})
];
map = new ol.Map({
layers: layers,
target: 'report_map',
view: new ol.View({
center: ol.proj.transform(
[-118.014670, 45.35724], 'EPSG:4326', 'EPSG:900913'),
zoom: 10
})
})
var addPolylineToMap = function (encoded_line, line_style) {
var line = new ol.format.Polyline().readGeometry({
source: encoded_line,
options: {
dataProjection: ol.proj.get('EPSG:4326'),
featureProjection: ol.proj.get('EPSG:900913')
}
});
line.setStyle(line_style);
vectorSource.addFeature(new ol.Feature(line));
return line;
};
Admittedly, I am quite new to OpenLayers 3--but I have searched extensively and can't seem to find any examples of using encoded polylines in OpenLayers 3.
Try this way:
var addPolylineToMap = function (encoded_line, line_style) {
var format = new ol.format.Polyline({
//factor: 1e6
});
var line = format.readGeometry(encoded_line, {
dataProjection: 'EPSG:4326',
featureProjection: 'EPSG:900913'
});
var feature = new ol.Feature({geometry: line});
feature.setStyle(line_style);
vectorSource.addFeature(feature);
return line;
};

Categories

Resources